首页 > 代码库 > HDU 4221 Greedy?
HDU 4221 Greedy?
F - Greedy?
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
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.
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
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
尽量把无序的序列变为有序。寻找之间的关系
首先要理解题意
题目意思是一共同拥有n个任务
每一个任务都有两个数据
一个是ci代表着做完这件事情要多久
di表示在这个时刻前要完毕这个任务,否则会有惩处:ti-di这个惩处的数表示。我在这个时刻完毕了,可是超过了di那么这个时刻减去di就是惩处数
而且每件事情必须不被打断。
思路分析:
对于两个任务而言。事先如果d1<d2,如果是等于的话。随便是谁都没有影响。
res1=max(0,c1-d1,c1+c2-d2);
res1=max(0,c2-d2,c1+c2-d1);
非常明显max函数里面除了零以外的两个值都有可能是负数,那好,这就是代码里面将res赋值为零的原因
然后就是c2-d2<c1+c2-d2<c1+c2-d1以及c1+c2-d1>c1-d1可是c1-d1与其它两个临时无法比較,可是
由题意已知,ci<di这个一定是成立的,证明ci-di<=0恒成立。那么题目是求超过限度的最大值最小。
那么小于零的就不用管了。
由于最小的才是零,小于零临时无论我们的是
好,这个两个任务
res12<=resii(当中i表示1到2的随意值不反复随意组合):12,21
然后是三个任务:
res123<=resiii(当中i表示1到3的随意值不反复随意组合):321,312,123,132,213,231
对于四个任务
res1234<=resiiii(当中i表示1到4的随意值不反复随意组合),......
。。。。。。
。
。
如此可以判断出。将限制最前的时间先做完才可以将最大惩处数变为最小。
当然还有最简单的想法。不须要如此思考,却一样可以做出来。就是。假设你限制时间小的。因为时间问题。你越不早处理。
你的惩处数就有可能越大。如此,我们应该最先处理最早的,于是依照这个思路,也能够AC这道题目。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int maxn=100000+5; int T,n,k; struct dai { int ci,di; bool operator<(const dai&a) const { return di<a.di; } } daes[maxn]; int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d%d",&daes[i].ci,&daes[i].di); } sort(daes,daes+n),k=1; long long res=0,cur=0; for(int i=0; i<n; i++) { cur+=daes[i].ci; res=max(res,cur-daes[i].di); } printf("Case %d: %lld\n",k++,res); } return 0; }
HDU 4221 Greedy?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。