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. 
 

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
 
这道题目,依旧是一道思维题目,
尽量把无序的序列变为有序,寻找之间的关系
首先要理解题意
题目意思是一共有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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值