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&&ANS2<ANS4)) printf"%i64d%i64d\n", ANS1,ANS2); Elseprintf"%i64d%i64d\n", ANS3,ANS4); } return 0;}
HDU4415 Assassin ' s Creed