Peng Lv

毋意,毋必,毋固,毋我。 言必行,行必果。

导航

{HDU}{4221}{Greedy}{Greedy}

贪心处理,关键是寻找题意中隐含的条件,并且证明贪心算法的正确性。输出一定用"%I64d"而不是"%lld"!

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <memory>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
#include <stack>
using namespace std;

const int MAXN = 100050;

#define CLR(x,y) memset(x,y,sizeof(x))
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))
#define rep(i,x,y) for(i=x;i<y;++i)

typedef struct{
	int c,d;
}Node;
bool operator<(const Node& a, const Node& b)
{
	return a.d < b.d;
}

int t,tt;
long long ans;
int n;
Node node[MAXN];

int work()
{
	int i,j,c,d;
	long long cur,tmp;
	scanf("%d",&n);
	rep(i,0,n)
		scanf("%d%d",&node[i].c,&node[i].d);
	sort(node,node+n);
	ans = 0;
	cur = 0;
	rep(i,0,n){
		cur += node[i].c;
		if( cur > node[i].d ) {
			tmp = cur - node[i].d;
			if( tmp > ans )
				ans = tmp;
		}
	}
	printf("Case %d: %I64d\n",t+1,ans);
	return 0;
}
int main()
{
	scanf("%d",&tt);
	rep(t,0,tt){
		work();
	}
	return 0;
}

 

posted on 2012-05-20 01:13  Lvpengms  阅读(304)  评论(0编辑  收藏  举报