POJ-3045-Cow Acrobats

写出式子,风险值就应该等于W总-Wnow-Snow,那么就应该为W总-(Wnow+Snow),然后可以看出要值最小,那么Wnow+Snow应该最大,所以按这个进行排序,然后最大的放最下面即可

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=1<<29;
const int maxn=5e4+100;
struct Cow
{
    int w;
    int s;
    bool operator < (const Cow &a)const
    {
        return w+s<a.w+a.s;
    }
}a[maxn];
int n;
long long sum;
int main()
{   
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].w,&a[i].s);
            sum+=a[i].w;
        }
        sort(a,a+n);
        long long ans=-inf;
        for(int i=n-1;i>=0;i--)
        {
            sum-=a[i].w;
            ans=max(ans,sum-a[i].s);
        }
        printf("%I64d\n",ans);
    } 
    return 0;
}


你可能感兴趣的:(贪心)