hdu4221 Greedy? _贪心

对n项任务按d由小到大排序,然后计算出其中的最大处罚即为所求

 

 

Greedy?

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2162    Accepted Submission(s): 733

 

 

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

 

 

 

 

 

/*
题意:完成一个任务的需要的时间为Ci,这个任务完成的最后期限为Di,如果完成时间Ti超过Di,
就得到Ti-Di的处罚。如何使完成所有任务后的最大处罚尽可能小 ,并输出最小的最大处罚。 
*/ 

/*
分析:假设只有两个任务1、2,且d1<d2,则有两种可能:
      1、先完成1,min_max=max{c1-d1,c1+c2-d2}
	  2、先完成2,min_max=max{c2-d2,c1+c2-d1}
	  由假设知c1+c2-d1>c1-d1,c1+c2-d1>c1+c2-d2,所以应先完成截止时间早的任务
	  
	考虑有3个任务,假设d1<d2<d3,min_max123为顺序完成任务1、2、3时3次处罚中的最大值
	同理分析可知:min_max123最小,min_max321最大
	推广到n项任务,同样成立
*/

//对n项任务按d由小到大排序,然后计算出其中的最大处罚即为所求

#include<cstdio>
#include<iostream>
#include<algorithm> 
using namespace std;

struct time
{
	int c,d;
}task[100010];

bool cmp(time a,time b)
{
	return a.d<b.d;
}

int main()
{
	int t;
	cin>>t;
	for(int k=1;k<=t;k++)
	{
		int n;
		cin>>n;
		for(int i=0;i<n;i++)  //此处用cin会超时 
		  scanf("%d%d",&task[i].c,&task[i].d);
		sort(task,task+n,cmp);
		long long min_max=0;   //结果可能很大,用long long来存 
		long long sum_time=0;
		for(int i=0;i<n;i++)
		{
			sum_time+=task[i].c;
			if((sum_time-task[i].d)>min_max)
			  min_max=sum_time-task[i].d;
		}
		printf("Case %d: %lld\n",k,min_max);
	}
	return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值