HDU 4221 greedy?

Source: Internet
Author: User

F-Greedy?Time limit:MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticeHDU 4221

Description

ISea is going to be crazy! Recently, he is assigned a lot of works to does, so many this can ' t imagine. Each task costs Ci time as least, and the worst news is, he must does this work no later than time di!
OMG, how could it is conceivable! After simple estimation, he discovers a fact if-a work was finished after Di, says Ti, he'll get a penalty ti-di. Though It may is impossible for him to finish every task before its deadline, he wants the maximum penalty of all the task s 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 the 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 is includes an integer N. Then N lines following, each line contains the 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 and then the smallest maximum penalty.

Sample Input

Sample Output

This problem is still a question of thought,
Try to change the order of disordered sequences and find the relationship between them.
The first thing to understand test instructions
The title means a total of n tasks
Each task has two of data
One is CI represents how long it takes to finish this thing.
Di means to complete this task before this time, otherwise there will be a penalty: Ti-di the number of this punishment means that I am done at this time, but more than di so this moment minus di is the penalty number
And everything must not be interrupted.
Thinking Analysis:
For both tasks, it is assumed that d1<d2, if equal, would have no effect on anyone.
Res1=max (0,C1-D1,C1+C2-D2);
Res1=max (0,C2-D2,C1+C2-D1);
It's obvious that the MAX function has a negative value of two in addition to 0, so that's why the res is assigned zero in the code.
And then C2-d2<c1+c2-d2<c1+c2-d1 and c1+c2-d1>c1-d1 but C1-d1 and the other two are temporarily unable to compare, but
Known by test instructions, Ci<di this must be set up, prove Ci-di<=0 Heng set up, then the title is to seek to exceed the maximum value of the minimum. Then less than 0 will not be the tube.
Because the smallest is zero, less than 0 for the moment whatever we are
Okay, the two missions.
Res12<=resii (where I means any value from 1 to 2 does not repeat any combination): 12,21
Then there are three tasks:
RES123&LT;=RESIII (where I means any value from 1 to 3 does not repeat any combination): 321,312,123,132,213,231
For four tasks
RES1234&LT;=RESIIII (where I means any value from 1 to 4 does not repeat any combination),......
。。。。。。。。
It can be inferred that the maximum penalty can be minimized by limiting the first time to being done.
Of course there are the simplest ideas, do not need to think so, but the same can be done, that is, if you limit the time, due to the problem of time, the less you deal with the earlier.

Your penalty number is likely to be greater, so, we should first deal with the earliest, so according to this idea, you can also ac this problem.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;typedef 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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.