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头牛在花丛里破坏花,他要把牛给带回家,这样花就能不被破坏了,
但是一次只能带一头牛,每头牛带到家里要花 ti 分钟(当然,从花丛到家再到花丛就是要花 2 * t i分钟了),每头牛每分钟要破坏 di 朵花,问农夫怎么带牛能使总的破坏的花总数最少,当然,农夫一开始在花丛。
输入:
第一行输入n。
随后的n行输入每头牛的 ti 和 di
输出:
输出最小的破坏总数
ll t,l,n,m,p,q,sum=0,ans=0;
string s;
struct node{
int t,d;
}a[maxn];
bool cmp(node x,node y){
return x.t*1.0/x.d < y.t*1.0/y.d;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].t>>a[i].d;
sum+=a[i].d;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
sum-=a[i].d;
ans+=2*a[i].t*sum;
}
cout<<ans<<endl;
return 0;
}
这题需要稍微考虑下了。
如果先带第i头牛,那就是 2 * ti * dj
如果先带第j头牛,那就是 2 * tj * di
假设先带第i头牛的方案更优,
那就是 ti* dj <tj *di
即 ti / di < tj / dj 注意乘上个1.0 这样才严谨
然后按照这样的cmp排序,
每次算ans+=2a[i].tsum;
sum是剩下的牛总共每分钟能吃多少