Protecting the Flowers
Protecting the Flowers
https://ac.nowcoder.com/acm/problem/25043
题意:由n个奶牛,然后每个奶牛都有两个属性,让农夫牵会牛舍需要的时间,以及每分钟坏花的数量,然后问最少损失多少的花
题解:贪心
我们假设有两头奶牛,那么先牵那个回去
比如如果先牵第一个奶牛,那么最后的损失量是第二个奶牛的损失花的量乘以第一个奶牛回牛舍所需要的时间
同理先牵第二个奶牛,那么最后的损失量是第一个奶牛的损失花的量乘以第二个奶牛回牛舍所需要的时间
然后就按照上面的推论,排下序,然后贪个心
#include<cstdio>
#include<algorithm>
struct node{
int t,d;
}m[101000];
bool com(node a,node b){
return a.t*b.d<b.t*a.d;
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d%d",&m[i].t,&m[i].d);
std::sort(m,m+n,com);
long long ans=0,t=0;
for(int i=0;i<n;i++){
ans+=t*m[i].d;
t+=m[i].t*2;
}
printf("%lld\n",ans);
return 0;
}