Greedy?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 908 Accepted Submission(s): 284
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.
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
本题要求超时的最大时间最小,max(Ti-Di)最小。本题时间跨度比较大,不能有DP解决,需找到一个贪心策略。本题的策略为将任务按结束时间从小到大排序,所求答案就是其中(t-d)最大的那个。我的说明如下:
我们这么想,假设时间t时刻,在未完成任务中取两个两个任务,一个截止时间为d1,另一个为d2,d1<d2,若先选择d1,则最大超时时间为max(tmp+c2-d2,tmp+c2+c1-d1)=tmp+c2+c1-d1;若先选择d2,则最大超时时间为max(tmp+c1-d1,tmp+c2+c1-d2)<tmp+c2+c1-d1;
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX=100000+10;
struct Time
{
int s,e;
}T[MAX];
bool cmp(Time a,Time b)
{
return a.e<b.e;
}
int main()
{
int cas,i,n,tag=1;
__int64 ans;
cin>>cas;
while(cas--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&T[i].s,&T[i].e);
sort(T,T+n,cmp);
ans=0;
__int64 tmp=0;
for(i=0;i<n;i++)
{
tmp+=T[i].s;
//printf("i=%d tmp=%I64d T[%d]=%d\n",i,tmp,i,T[i].e);
if(tmp-T[i].e>ans)
ans=tmp-T[i].e;
}
printf("Case %d: %I64d\n",tag++,ans);
}
return 0;
}