Protecting the Flowers POJ - 3262

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.

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

Output

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

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

Hint

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.
 
百度翻译:

农夫约翰去砍了些木头,让牛(2≤n≤100000)像往常一样吃草。当他回来的时候,他惊恐地发现花园里的牛群正在吃他美丽的花。为了尽量减少随后的损失,FJ决定立即采取行动,把每头奶牛运回自己的谷仓。每头奶牛i都在离自己的谷仓1分钟(1≤ti≤2000000)的地方。此外,在等待运输时,她每分钟破坏DI(1≤DI≤100)花。不管他多么努力,FJ每次只能把一头牛运回谷仓。把奶牛一号移到谷仓需要2×ti分钟(ti到那里,ti返回)。FJ从花圃开始,把奶牛运到谷仓,然后走回花圃,不用额外的时间就可以到达下一头需要运输的奶牛。编写一个程序来确定FJ接收奶牛的顺序,这样就可以最小化被破坏的花的总数。

思路:按照ti*1.0/di 来排序能够使每次做出的选择都是损失最小的。可以在开始时累加出每只牛一分钟破坏花的和,在排序后求结果时累减。

 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 1000000007
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int n;
22 struct node
23 {
24     int t,d;
25 };
26 node no[100005];
27 bool px(node a,node b)
28 {
29     return ((a.t*1.0)/a.d)<((b.t*1.0)/b.d);
30 }
31 int main()
32 {
33     scanf("%d",&n);
34     ll sum=0;
35     for(int i=0;i<n;i++)
36     {
37         scanf("%d %d",&no[i].t,&no[i].d);
38         sum+=no[i].d;
39     }
40     sort(no,no+n,px);
41     ll ans=0;
42     for(int i=0;i<n-1;i++)
43     {
44         sum-=no[i].d;
45         ans+=2*no[i].t*sum;
46     }
47     cout<<ans<<endl;
48 }

猜你喜欢

转载自www.cnblogs.com/mzchuan/p/11209726.html