POJ3045Cow Acrobats

版权声明:我这么弱的蒟蒻,虽然博文不是很好,但也请标明转发地址喵! https://blog.csdn.net/ACerAndAKer/article/details/82746467

毒瘤贪心

我们假设我们现在正在处理这个塔中的某一个位置,处理完上一个位置后剩下的总重量为 W s u m ,然后考虑两头牛a和b,假如当前层的风险选a比b更优的话,就有 W s u m W a S a < W s u m W b S b ,移项之后得到 W a + S a > W b + S b ,所以我们按照这个排序,那么每层都是最优的,然后就在处理过程中取个max就好了,然后有一种非常友(du)好(liu)的情况就是n=1的时候,发现它头上没重量,风险是负数,所以我们ans初始值要等于-inf

代码

//By AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=50050;
int n;
struct cow{int w,s;}e[M];
inline int read()
{
    int x=0;char ch=getchar();
    while (ch>'9'||ch<'0') ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x;
}
inline bool cmp(cow x,cow y)
{
    return x.w+x.s>y.s+y.w;
}
signed main()
{
    n=read();int sum=0,ans=-2e9;
    for (int i=1;i<=n;i++) 
        e[i].w=read(),e[i].s=read(),sum+=e[i].w;
    sort(e+1,e+n+1,cmp);
    for (int i=1;i<=n;i++)
    sum-=e[i].w,ans=max(ans,sum-e[i].s);
    cout<<ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ACerAndAKer/article/details/82746467