P2878 [USACO07JAN]保护花朵Protecting the Flowers
题目描述
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.
有n头奶牛跑到FJ的花园里去吃花儿了,它们分别在距离牛圈T分钟处吃花儿,每分钟会吃掉D朵卡哇伊的花儿,(此处有点拗口,不要在意细节啊!),FJ现在要将它们给弄回牛圈,但是他每次只能弄一头回去,来回用时总共为2*T分钟,在这段时间内,其它的奶牛会继续吃FJ卡哇伊的花儿,速度保持不变,当然正在被赶回牛圈的奶牛就没口福了!现在要求以一种最棒的方法来尽可能的减少花儿的损失数量,求奶牛吃掉花儿的最少朵数!
输入输出格式
输入格式:
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
输出格式:
Line 1: A single integer that is the minimum number of destroyed flowers
输入输出样例
63 12 52 33 24 11 6
86
说明
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.
写了个智障的贪心,结果发现贪错了、、(似乎仅过了样例)
智障的认为现将吃的多的迁回去花的时间一定少,其实不然,如果我们有这样两头牛,一个时间为1吃的为5,另一个迁回去所需的时间为6,吃的速度为7,如果我们要把第二头牛先迁回去,在这12秒内另一头牛吃了12*5,而反之先迁第一头牛那么我们只被吃了2*7的花,显然第二种是更优的。
既然这样我们考虑一下怎么贪心更优
我们发现如果我们有2 4,7 5,1 4,这样的几头牛,我们最先迁回去的一定是2 4,然后是7 5,然后在是1 4我们可以发现一个规律,c/t最大的一定是我们先迁回去的。
根据每分钟吃的花和运输时间的比值从大到小运...
如果先运j,i牛产生的贡献是cow[i].d*cow[j].t,如果先运i,j牛产生的贡献是cow[j].d*cow[i].t
假设前者比后者小,则,cow[i].d*cow[j].t<cow[j].d*cow[i].t,那么先运j更优,
更优的条件是,cow[i].d/cow[i].t<cow[j].d/cow[j].t为贪心的公式
#include#include #include #include #include #define N 110000using namespace std;long long n,tot,ans;long long read(){ long long x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}struct Node{ long long t,c;}a[N];double cmp(Node a,Node b){ return a.c*1.0/a.t>b.c*1.0/b.t;}int main() { n=read(); for(int i=1;i<=n;i++) a[i].t=read(),a[i].c=read(),tot+=a[i].c; sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++) { tot-=a[i].c; ans+=2*a[i].t*tot; } printf("%lld",ans); return 0;}