bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】
因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小。按照a.t*b.f排降序,模拟着计算答案
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100005;
int n;
long long ans,sum;
struct qwe
{
int v,t;
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
return (double)a.v/a.t>=(double)b.v/b.t;
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i].t=read()*2,a[i].v=read(),sum+=a[i].v;
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
{
sum-=a[i].v;
ans+=sum*a[i].t;
}
printf("%lld\n",ans);
return 0;
}