51nod 1099【贪心】

思路:

我们可以思考对于仅仅两个元素来说,A,B;

先选A的话是会  A.b+B.a;

先选B的话是会 B.b+A.a;

所以哪个小哪个就放前面;

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;

typedef long long LL;

const int N=1e5+10;

struct asd{
	LL a,b;
};
asd q[N];

bool cmp(asd x,asd y)
{
    if(x.b+y.a<x.a+y.b)
        return 1;
    return 0;
}

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%lld%lld",&q[i].a,&q[i].b);
	sort(q,q+n,cmp);

	LL p,ans,pre;
	p=ans=0;
	pre=0;
	for(int i=0;i<n;i++)
	{
		p=p+q[i].a;
		ans=max(ans,p);
		p=p-q[i].a+q[i].b;
	}
	printf("%lld\n",ans);
	return 0;
}



posted @ 2016-11-01 20:56  see_you_later  阅读(93)  评论(0编辑  收藏  举报