题意:
有n头牛,每头牛每分钟对花的伤害为D,每头牛被运回牛棚需要T分钟,求对花最少的伤害。
解题思路:
只需按照每头牛对花的伤害排序即可,伤害最大的先运回去。
程序:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 110000
using namespace std;
int d[N];
struct In
{
long long a;
long long b;
}cow[N];
bool cmp(In x , In y)
{
return x.a*y.b>=x.b*y.a;
}
int main()
{
int n,m,s,t,i,j;
long long ans;
while(scanf("%d",&t)!=EOF)
{
memset(d,0,sizeof(d));
for(i=0;i<t;i++)
{
cin>>cow[i].a>>cow[i].b; // 按照伤害,由小到大排序。
}
sort(cow,cow+t,cmp);
/* for(i=0;i<t;i++)
{
cout<<cow[i].a<<" "<<cow[i].b<<endl;;
}*/
d[0]=cow[0].b;
for(i=1;i<t;i++)
{
d[i]=d[i-1]+cow[i].b;
}
ans=0;
for(i=t-1;i>0;i--)
{
ans+=d[i-1]*cow[i].a*2; // 先把伤害大的运回去。
}
cout<<ans<<endl;
}
return 0;
}