1. 程式人生 > >HDU - 4221 Greedy? 貪心

HDU - 4221 Greedy? 貪心

iSea is going to be CRAZY! Recently, he was assigned a lot of works to do, so many that you can't imagine. Each task costs Ci time as least, and the worst news is, he must do this work no later than time Di!
OMG, how could it be conceivable! After simple estimation, he discovers a fact that if a work is finished after Di, says Ti, he will get a penalty Ti - Di. Though it may be impossible for him to finish every task before its deadline, he wants the maximum penalty of all the tasks to be as small as possible. He can finish those tasks at any order, and once a task begins, it can't be interrupted. All tasks should begin at integral times, and time begins from 0.

Input

The first line contains a single integer T, indicating the number of test cases.
Each test case includes an integer N. Then N lines following, each line contains two integers Ci and Di.

Technical Specification
1. 1 <= T <= 100
2. 1 <= N <= 100 000
3. 1 <= Ci, Di <= 1 000 000 000

Output

For each test case, output the case number first, then the smallest maximum penalty.

Sample Input

2
2
3 4
2 2
4
3 6
2 7
4 5
3 9

Sample Output

Case 1: 1
Case 2: 3

題意:給出每個任務至少要操作的時間和最晚截止時間,若在t時間完成,t>d,則懲罰為t-d,求所有的懲罰的最大值最小是多少

題解:最後完成所有任務的時間是確定的,要讓每個任務的懲罰儘可能小,當然要先做截止時間靠前的,因為若放在後面,完成的時間越靠後,懲罰越大,這樣最大值會更大,按截止時間排序,記錄下當前時間即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=110000;
typedef long long ll;
struct node{
	ll x;
	ll end;
}a[N];
bool cmp(node xx,node yy)
{
	return xx.end<yy.end;
}
int main()
{
	int T,nn=1;
	int n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i].x,&a[i].end);
		ll tm=0;
		sort(a+1,a+1+n,cmp);
		ll ans=0;
		for(int i=1;i<=n;i++)
		{
			tm+=a[i].x;
			if(tm>a[i].end)
			{
				ans=max(ans,tm-a[i].end);
			}
		}
		printf("Case %d: %lld\n",nn++,ans);
	}
	return 0;
}