1634: [Usaco2007 Jan]Protecting the Flowers 护花

来源:互联网 发布:好用的急救面膜知乎 编辑:程序博客网 时间:2024/05/16 14:01

题目链接

题目大意:把n只奶牛送回牛棚,运送一头奶牛需要t[i]的时间,在运送的时候其他奶牛每分钟吃d[i]朵花(已经或正在被送回去的不吃),安排顺序,使得被吃的花尽量少

题解:调整法应用
对于相邻的x,y
x在y前面 吃的花的数量是d[y]*2*t[x],
y在x前面 吃的花的数量是d[x]*2*t[y],
若x在y前面更优(吃的花数量少),需满足d[y]*t[x]<d[x]*t[y]

我的收获:套路+1

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;#define ll long longint n;ll s,t;struct cow{    int w,v;}c[100005];bool cmp(cow a,cow b){    return a.w*b.v<b.w*a.v;}void init(){    cin>>n;    for(int i=1;i<=n;i++)    scanf("%d%d",&c[i].w,&c[i].v);    sort(c+1,c+1+n,cmp);}int main(){    init();    for(int i=1;i<=n;i++)    {        s+=c[i].v*t;        t+=c[i].w*2;    }    printf("%lld\n",s);    return 0;}
原创粉丝点击