当前位置: 代码迷 >> 综合 >> Protecting the Flowers POJ - 3262
  详细解决方案

Protecting the Flowers POJ - 3262

热度:52   发布时间:2023-12-07 00:25:27.0

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.

题意:
有n头牛,一头牛离开的时间(2*T),其他牛会破坏花朵;
求最少会破坏多少花

题解:
假如我们只考虑破坏花的效率D,把D最大排在前面,如果这头牛的时间也很长,破坏的花的数量 = 其他牛 * 时间,那么不一定使最少的。

假设最后只剩下两头牛a,b。
(1) 顺序是先a后b,破坏花的数量是 2 * Ta * Db;
(2) 顺序是先b后a,破坏花的数量是 2 * Tb * Da;
假设(1)是正确的顺序,即2 * Ta * Db < 2 * Tb * Da;
即Da / Ta > Db / Tb;
所以Da / Ta 越大, 顺序越靠前

#include <iostream>
#include <cmath>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
struct P {
    double t,d,td;
};
bool cmp (P a, P b) {
    return a.td > b.td;
}
P a[100110];
int main() {
    int n; long long  ans=0;cin>>n;for(int i = 0 ;i < n;i++) {
    cin >> a[i].t >> a[i].d;a[i].td=a[i].d/a[i].t;}sort(a, a+n, cmp);int t=a[0].t;for(int i=1 ; i<n ; i++){
    ans+=2*(t)*(a[i].d);t+=a[i].t;}cout<<ans<<endl;
}