[USACO Open07] 保护花朵 --贪心

来源:互联网 发布:金融大数据 pdf 编辑:程序博客网 时间:2024/04/30 05:24
Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2*Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

 

FJ去砍柴了,留下N(2<=n<=100,000)头奶牛在牧场里像往常一样吃草。当他回来的时候,他惊恐的

发现牛们组队去他的花园里吃花了。为了将FJ花园的损失最小化,FJ决定采取快速的行动——把每头奶牛赶回她们自己的牛棚里。每头奶牛i所在的位置到她们

自己的牛棚要花费Ti(1<=Ti<=2,000,000)分钟,每头奶牛在花园中每分钟都会吃掉Di(1<=Di<=100)

朵花。

贴士:

1.FJ每次只能赶一头牛回牛棚。

2.因为FJ要一来一回,所以说他每赶一头牛回牛棚要花费2*Ti分钟。

3.FJ从花园中出发赶一头奶牛回牛棚,再从牛棚走到花园。

4.因为FJ会瞬移,所以说到花园之后不需要花多余的时间赶到下一头牛所在的地方。

求一个合理的顺序使FJ到花园损失的花朵数最小。

 

Input

  • Line 1: A single integer N
  • Lines 2..N + 1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics

 

输入:

第1行:一个单独的整数N。

第2~N+1行:两个由空格分开的整数,分别描述第i头奶牛的Ti和Di。

 

Output

  • Line 1: A single integer that is the minimum number of destroyed flowers

 

输出:

第1行:一个单独的整数,表示FJ损失的花的最小数目。

 

Sample Input

63 12 52 33 24 11 6

Sample Output

86

Output Details

FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.

样例解释:

FJ赶牛的顺序是6-2-3-4-1-5,当他赶牛6时他损失了24朵花,当他赶牛2时他损失了28朵花,当他赶牛3时他损失了16朵花,

当他赶牛4时他损失了12朵花,当他赶牛1时他损失了6朵花,当他赶牛5时他损失了0朵花。24+28+16+12+6+0=86。

 

 1 /* 2      贪心~  3      肯定要排序嘛  4     排序 考虑这个  5     两头牛 a  b 6     他们的位置 对后牵的牛 肯定没有影响 7     那 设后面的代价之和 为VAL  8     当前代价  先牵a  2*t[a]*(d[b]+VAL)+2*t[b]*VAL; 9               先牵b  2*t[b]*(d[a]+VAL)+2*t[a]*VAL;10     只需要比较 t[a]*d[b] 和 t[b]*d[a] 的 大小即可 11 */12 #include <algorithm>13 #include <ctype.h>14 #include <cstdio>15 16 #define LL unsigned long long17 18 const int MAXN=100010;19 20 int n;21 22 LL ans,t;23 24 struct node {25     int d,t;26     int id;27     bool operator < (const node b) const {28         return t*b.d<b.t*d;29     }30 };31 node v[MAXN];32 33 inline void read(int&x) {34     register char c=getchar();35     for(x=0;!isdigit(c);c=getchar());36     for(;isdigit(c);x=x*10+c-48,c=getchar());37 }38 39 int hh() {40     freopen("flowers.in","r",stdin);41     freopen("flowers.out","w",stdout);42     read(n);43     for(int i=1;i<=n;++i) read(v[i].d),read(v[i].t),v[i].id=i;44     std::sort(v+1,v+1+n);45     for(int i=1;i<=n;++i) {46         ans=ans+(LL)t*v[i].d;47         t+=2*v[i].t;48     }49     printf("%lld\n",ans);50     return 0;51 }52 53 int sb=hh();54 int main() {;}
题解