HDU4415 Assassin ' s Creed

Source: Internet
Author: User

The main idea: there are n individuals, each with x, y two values. X is for the score he gets, the score and the number of m;y to kill him, and how many extra you can kill and not count toward the total.

What is the maximum number of kills, and the smallest score.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The final 40 minutes of the exam to think of the greedy, thinking too messy no time code caused 20.

Direct Positive Solution: Elegant Greed

If Y is placed in a set of 0 and the rest in B set, then the positive solution can only have two cases:

1. Take only from A.

2. Take all B, and then remove the smallest from small to large, then merge B into a and take from a.

Why is that so?

The first thing we can know is that if you take a B, you can bring out all B.

In this way, you can imagine that BI is built into a tree:

B1->b2->b3

\

>b4->b5

\

>b6

Let's say it's a tree.

Suppose it takes 100 bucks to kill B2 and a a2 to 200 bucks.

So we can kill B2 first, spend 100, and then B1 one less son, in putting A2 in, is:

B2->b3

B1->a2

\

>b4->b5

\

>b6

This is to spend 100 dollars to kill A2.

We also found that B1 cannot be replaced because it does not have a father.

So the positive solution:

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineN 100050#definell Long Longintt,n,ac,bc;ll ax[n],bx[n],by[n],m;BOOLCMP (ll x,ll y) {returnx<y;}intCS;intMain () {scanf ("%d",&T);  while(t--) {CS++; printf ("Case %d:", CS); scanf ("%d%i64d",&n,&m); ll Sy=1; AC=bc=0;  for(intI=1; i<=n;i++) {BC++; scanf ("%i64d%i64d",&bx[bc],&BY[BC]); if(!BY[BC]) {ax[++ac]=BX[BC]; BC--; Continue; } sy+=BY[BC]; } sort (Ax+1, ax+1+ac,cmp); ll Ans1=0; ll Ans2=0;  for(intI=1; i<=ac;i++)        {            if(ans2+ax[i]<=m) {ans1++; Ans2+=Ax[i]; }Else            {                 Break; }        }        if(!BC) {printf ("%i64d%i64d\n", ANS1,ANS2); Continue; } sort (bx+1, bx+1+bc,cmp); ll Ans3=Sy; ll Ans4= bx[1]; if(ans4>m) {printf ("%i64d%i64d\n", ANS1,ANS2); Continue; }         for(intI=2; i<=bc;i++) {ax[++AC] =Bx[i]; } sort (Ax+1, ax+1+ac,cmp);  for(intI=1; i<=ac;i++)        {            if(ans3>=N) {ans3=N;  Break; }            if(ans4+ax[i]<=m) {ans3++; ANS4+=Ax[i]; }        }        if(ans1>ans3| | (ANS1==ANS3&AMP;&AMP;ANS2&LT;ANS4)) printf"%i64d%i64d\n", ANS1,ANS2); Elseprintf"%i64d%i64d\n", ANS3,ANS4); }    return 0;}

HDU4415 Assassin ' s Creed

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.