Greedy?

Problem Description

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

Author

iSea@WHU

Source

首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛

Recommend

lcy

题意:给你机组数据其中为工作时间和不能超过的时间没回超过有惩罚求最少的最大惩罚率

思路:贪心,快排不能超过的时间然后一个个比较求最大惩罚就行了

代码:

#include<iostream>
#include<algorithm> 
using namespace std;
struct node{
	long long x,y;
}pp[1000009];
bool cmp(node a,node b)
{
	if(a.y == b.y)
	{
		return a.x<b.x; 
	} 	
	return a.y<b.y;
}
int main()
{
	long long t;
	scanf("%lld",&t);int g=1;
	while(t--)
	{
		long long n;
		scanf("%lld",&n);printf("Case %d: ",g);g++;
		for(long long i=0;i<n;++i)
		{
			scanf("%lld%lld",&pp[i].x,&pp[i].y);
		}
		sort(pp,pp+n,cmp);long long min1 = 0,t1=0,t2;
		for(long long i=0;i<n;++i)
		{
			t1+=pp[i].x;
			if(t1>pp[i].y)
			{
				min1 = max(min1,t1-pp[i].y);
			}	
		}
		printf("%lld\n",min1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43568078/article/details/89016506