poj -3262 Protecting the Flowers (贪心)

http://poj.org/problem?id=3262

开始一直是理解错题意了!!导致不停wa。

这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛需要的时间和牛在花园每分钟吃多少花朵,问你怎么安排让损失最小。

这题单独按time和eat排序都不行,得按它们的比率来排,如果是选择eat/time  则从大到小排,time/eat则从小到大排,但是不会严格证明。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d\n", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("dout.txt", "w", stdout);
36 #define N 100005
37 using namespace std;
38 
39 struct point
40 {
41     int x,y;
42     double d;
43     bool operator <(const point &a) const
44     {
45         return d>a.d;
46     }
47 }p[100010];
48 int main()
49 {
50    //Read();
51    int n;
52    ll sum,t;
53    while(~scanf("%d",&n))
54    {
55        sum=t=0;
56        for(int i=0;i<n;i++)
57        {
58            scanf("%d%d",&p[i].x,&p[i].y);
59            p[i].d=p[i].y*1.0/p[i].x;
60            t+=p[i].y;  //这样处理 方便计算
61        }
62        sort(p,p+n);
63        //for(int i=0;i<n;i++) printf("%d %d\n",p[i].x,p[i].y);
64        for(int i=0;i<n;i++)
65        {
66            t-=p[i].y;
67            sum+=p[i].x*t*2;
68        }
69        printf("%lld\n",sum);
70    }
71     return 0;
72 }

 

转载于:https://www.cnblogs.com/nowandforever/p/4419896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值