Hdu4415 Assassin's Creed

Source: Internet
Author: User

This is obviously greedy. First, when reading data, we divide the enemy into two types: knife-free and knife-free. At the beginning, there were two greedy decisions, only kill people without a knife and kill all people with a knife. Then compare the two decisions and select the optimal solution.

The first type of decision is easy to find, so we will not talk about it. We will focus on the second type. For a person with a knife, you must first select the person with the least blood to kill, and then get his knife. If we continue to kill a person with a knife with this knife, then we can certainly kill all the people with knives, and then use the obtained knives to continue killing those without knives. Of course, if we can kill all the remaining people in this way, that is to say, we do not need to kill them with our own knives. This is undoubtedly the best solution, but what if I cannot kill all the people with the obtained knife? In this case, we will continue to be greedy. Since some people must use their own knives to kill, we should choose to kill with less blood, therefore, we need to combine the people with knives and those without knives, select the people with the smallest blood volume (that is, those who cannot be killed with the obtained knives), and then kill them with their own knives, after solving this problem, other people will surely be able to finish it (as we can see from the above discussion ). After analysis, this question can be completely solved. The code is detailed below.

 

 

# Include <iostream> # include <cstdio> # include <algorithm> using namespace STD; const int maxn = 100005; int A [maxn], B [maxn]; // A [I] records the blood volume of a person with a knife, and B [I] records the blood volume of a person without a knife, int n, m; int cnt1, cnt2; // int sword for the number of people with and without a knife; // records the number of knives int main () {int T, I, j, X, Y; int ans, left; int ans1, left1; // scanf ("% d", & T); for (I = 1; I <= T; I ++) {scanf ("% d", & N, & M); cnt1 = cnt2 = sword = 0; For (j = 1; j <= N; j ++) {scanf ("% d", & X, & Y); If (y) {sword + = y; A [cnt1 ++] = x;} else B [cnt2 ++] = x;} Sort (A, A + cnt1); sort (B, B + cnt2 ); // only kill the person without a knife. j = 0; ans = 0; left = m; while (left> = 0 & J <cnt2) {If (left> = B [J]) {left-= B [J ++]; ans ++;} else break ;} // If (M> = A [0]) {If (sword> = N-1) if (sword> = N-1) if (ANS <n) {ans = N; left = m-A [0];} else if (ANS = N & left <m-A [0]) left = m-A [0]; printf ("case % d: % d \ n", I, ANS, M-left); continue ;} int K = n-1-sword; // also need to use your own knife to kill the number of people // continue here greedy: Since in any case this K individual must kill, // We should combine the products with and without a knife, and select the minimum blood volume to kill int * temp = new int [k]; X = 1; y = 0; j = 0; A [cnt1] = B [cnt2] = 100000000; while (j <k) {if (a [x] <B [y]) temp [J] = A [x ++]; else temp [J] = B [y ++]; j ++;} J = 0; ans1 = 1 + sword; left1 = m-A [0]; while (j <K & left1> = 0) {If (ans1 = N) break; if (left1> = temp [J]) {left1-= temp [J ++]; ans1 ++;} else break;} If (ans1> ans) {ans = ans1; left = left1;} else if (ans1 = ans & left <left1) Left = left1; Delete [] temp;} printf ("case % d: % d \ n ", I, ANS, M-left);} // system (" pause "); Return 0 ;}

 

 

 

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.