ACM Learning process-hdu4415 Assassin ' s Creed (greedy)

Source: Internet
Author: User

Problem Description

Ezio Auditore is a great master as an assassin. Now he had prowled in the enemies ' base successfully. He finds that's the only weapon he can use was his cuff sword and the sword have durability m. There is n enemies he wants to kill and killing each enemy needs Ai durability. Every time Ezio kills an enemy he can use the enemy's sword to kill any other Bi enemies without wasting his cuff sword ' s Durability. Then the enemy ' s sword would break. As a master, Ezio always want to do things perfectly. He decides to kill as many enemies as he can using the minimum durability cost.

Input

The first line contains an integer T, the number of test cases.
For each test case:
The first line contains the integers, above mentioned N and M (1<=n<=10^5, 1<=m<=10^9).
Next n lines, each line contains the integers Ai, Bi. (0<=ai<=10^9,0<=bi<=10).

Output

For each case, output ' Case x: ' (x is the case number starting from 1) followed by the number of the Enemiesezio can kill and the minimum durability cost.

Sampleinput

2

3 5

4 1

5 1

7 7

2 1

2 2

4 0

Sampleoutput

Case 1:3 4

Case 2:0 0

The main idea is to use M-point durability to kill, kill a person need AI endurance, but can be extra to kill bi individuals.

The topic requires the first to kill the most, followed by the least durability of consumption.

First of all think of the classification of people, a class B is 0, a Class B is not 0.

Because killing a B is not 0, you will be able to kill all B not 0, and you can kill B for 0.

So the first situation is separated: not to kill B is not 0 of the people;

This is the B for 0 of the greedy people, priority to kill the minimum durability can be.

Consider the second case: kill at least a person who is not 0 of a B.

The first premise, then, is to ensure that a person who kills B does not have a minimum of 0 durability.

Then you can kill all B's not 0 people. And you can kill B for 0 extra.

The last is to use the remaining durability to kill the remaining B 0 of the people.

But there is a situation. Even though B is not 0, it can be killed automatically, but if I manually kill a person who is not 0, you can kill a person with a B for 0. But maybe I'll use less endurance to kill a man manually.

As a result, it is possible to kill all B people who are not 0, but it is a priority to manually kill the less durable ones.

Conclusion: After killing the first person with a minimum endurance of 0, I manually kill from small to large with durability.

It is also important to note that, after killing people in turn, if the rest of the people can be killed automatically, I do not need to manually kill the person.

In the end, there are two cases where the most value is taken.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<Set>#include<utility>#include<queue>#include<vector>#defineLL Long Longusing namespaceStd;typedef pair<int,int>PII;Const intMAXN = 1e5+5;intN, M;structnode{intneed; intkill;} p[2][MAXN];inttop[2];intcan;BOOLcmpneedlest (node x, node Y) {returnX.need <y.need;}voidinput () {scanf ("%d%d", &n, &m); Can=0; top[0] = top[1] =0; intneed, kill;  for(inti =0; I < n; ++i) {scanf ("%d%d", &need, &kill); if(Kill = =0) {p[0][top[0]].need =need; p[0][top[0]].kill =0; top[0]++; }        Else{p[1][top[1]].need =need; p[1][top[1]].kill =kill; top[1]++; Can+=kill; }} sort (p[0], p[0]+top[0], cmpneedlest); Sort (p[1], p[1]+top[1], cmpneedlest);} PII Workone () {//p->p+top;    intnum =0, rest =m;  for(inti =0; I < top[0]; ++i) {if(Rest >= p[0][i].need) {num++; Rest-= p[0][i].need; }        Else             Break; }    returnPII (num, M-rest);} PII worktwo () {intrest = m, num =0; int  from[2];  from[0] = from[1] =0; if(top[1] ==0|| Rest < p[1][0].need)returnPii0,0); Rest-= p[1][0].need;  from[1]++; BOOLFlag;  for (;;) {        if(N-( from[0]+ from[1]) <=can) Break; Flag=false; if( from[1] < top[1] && p[0][ from[0]].need > p[1][ from[1]].need) {if(Rest >= p[1][ from[1]].need) {flag=true; Rest-= p[1][ from[1]].need;  from[1]++; Num++; }        }        Else if( from[0] < top[0])        {            if(Rest >= p[0][ from[0]].need) {flag=true; Rest-= p[0][ from[0]].need;  from[0]++; Num++; }        }        if(!flag) Break; }    returnPII (Min ( from[0]+ from[1]+can, N), M-rest);}voidWork () {PII one, both; One=Workone (); both=Worktwo (); if(One.first >two.first) printf ("%d%d\n", One.first, One.second); Else if(One.first = =two.first) printf ("%d%d\n", One.first, Min (One.second, two.second)); Elseprintf ("%d%d\n", Two.first, Two.second);}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);  for(intTimes =1; Times <= T; ++Times )        {input (); printf ("Case %d:", times);    Work (); }    return 0;}

ACM Learning process-hdu4415 Assassin ' s Creed (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.