洛谷P2878 保护花朵

来源:互联网 发布:乐视无线显示网络异常 编辑:程序博客网 时间:2024/05/01 19:20

%lyl大佬
这是一道贪心题,具有明显的套路特征,即对于两个相邻的奶牛,互相交换位置对其他奶牛并无影响,所以我们就将总问题转化为子问题,即讨论两头奶牛时的情况。

我们可以设两头牛为x,y, 且先牵走x时吃的草比牵走y时的少,即x更优,
则 x.d*y.h*2

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;struct node{    int d,t;}a[100005];bool cmp(node a,node b){    return a.d*b.t>b.d*a.t;}int n;long long ans,T;int main(){    cin>>n;    for (int i=1;i<=n;i++)    scanf("%d%d",&a[i].t,&a[i].d);    sort(a+1,a+n+1,cmp);    for (int i=1;i<=n;i++){        ans+=T*a[i].d;        T+=2*a[i].t;    }    cout<<ans;    return 0;}