BZOJ 1629: [Usaco2007 Demo]Cow Acrobats【排序】

1629: [Usaco2007 Demo]Cow Acrobats

【题目描述】
传送门

【题解】

这题其实很简单的,我们无非就考虑两种情况,i在j上,j在i上。
i在j上:WiSj
j在i上:WjSi
WiSj<WjSi
移项得:Wi+Si<Wj+Sj
按照这个进行排序就可以了。

代码如下

#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
int n;LL Ans=-1e9;
struct xcw{
    LL W,S;
    bool operator <(const xcw b)const{return W+S<b.W+b.S;}
}a[50005];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i].W,&a[i].S);
    sort(a+1,a+1+n);
    LL W=0;
    for(int i=1;i<=n;i++) Ans=max(W-a[i].S,Ans),W+=a[i].W;
    printf("%lld\n",Ans);
    return 0;
}
posted @ 2018-05-17 21:08  XSamsara  阅读(88)  评论(0编辑  收藏  举报