BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花

来源:互联网 发布:php虚拟主机 编辑:程序博客网 时间:2024/05/16 15:16

贪心
因为交换这俩头牛的搬走顺序不会影响其他牛
所以只要排序就好了
下面的.y表示的是赶走这头牛需要的时间
.x则是这头牛单位时间吃掉的花
x.x×y.y>y.x×x.y时就是牛x放在前更优
因为赶走牛y的时间里x吃掉的花比赶走牛x时间里y吃掉的多
所以应该尽快赶走牛x


#include<set>#include<map>#include<cmath>#include<ctime>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#define g getchar()#define ll long long#define inf 0x3f3f3f3fusing namespace std;inline ll read(){    ll x=0,f=1;char ch=g;    for(;ch<'0'||ch>'9';ch=g)if(ch=='-')f=-1;    for(;ch>='0'&&ch<='9';ch=g)x=x*10+ch-'0';    return x*f;}inline void out(ll x){    int a[25],wei=0;    if(x<0)putchar('-'),x=-x;    for(;x;x/=10)a[++wei]=x%10;    if(wei==0){puts("0");return;}    for(int j=wei;j>=1;--j)putchar('0'+a[j]);    putchar('\n');}struct re{int x,y;}d[100001];int n;ll ans,t;bool cmp(re x,re y){    return x.x*y.y>y.x*x.y;}int main(){//  freopen("","r",stdin);//  freopen("","w",stdout);    n=read();    for(int i=1;i<=n;++i)    d[i].y=read(),d[i].x=read();    sort(d+1,d+1+n,cmp);    ans=0;t=0;    for(int i=1;i<=n;++i)    ans+=t*d[i].x,t+=d[i].y*2;    out(ans);    return 0;}
0 0
原创粉丝点击