Cf 332 C greedy

Source: Internet
Author: User
Link: http://codeforces.com/problemset/problem/332/C


Reference: http://blog.csdn.net/cc_again/article/details/9471465


Question meaning:


There are n commands to be accepted through P, and a Chairman should select K of the P commands to be accepted.


Each task has two values: AI. BI. AI indicates that if the chairman accepts the command, her hair gets grayed out. BI indicates that if the chairman does not accept the command, the Member is not happy.


For the P commands passed, the chairman wants to minimize the unhappy value of members and, in the same case, minimize the gray amount of his hair.


Let you find out which p command can be used to make the chairman's hair gray the most. In the same case, the output will make the members unhappy.



Solution:

The key point is that the Chairman's idea is different from ours. If we first select the Chairman and we will not use the p-K Command, then the p-K Command must

Unhappy is the smallest, and then the K entries must be selected with the largest num (number of white hairs). In this way, many hairs are selected, and then the unselected p-K entries are updated.

After that, read the code:




/* Meaning: There are n commands, which should be accepted by P, and a Chairman should select K of them for acceptance. Each task has two values: AI. BI. AI indicates that if the chairman accepts the command, her hair gets grayed out. BI indicates that if the chairman does not accept the command, the Member is not happy. For the P commands passed, the chairman wants to minimize the unhappy value of members and, in the same case, minimize the gray amount of his hair. Let you find out which p command can be used to make the chairman's hair gray the most. In the same case, the output will make the members unhappy. * // Greedy # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define n 101000 struct stud {int num, unhappy; int POs, pp;} f [N], s [N]; int N, P, K, vis [N]; int ans [N]; int CMP1 (stud a, stud B) // according to the chairman's habits, the first choice is unhappy, and then drop the hair less {if (. unhappy = B. unhappy) return. num <B. num; return. unhappy> B. unhappy;} int cmp2 (stud a, stud B) // as per our wishes, choose more hair loss. Why is unhappy before? Please refer to CMP1, unhappy is above, // we can find The greater the minimum unhappy result, the greater the unhappy option for hair loss. {if (. num = B. num) return. unhappy> B. unhappy; return. num> B. num;} int main () {int I; while (~ Scanf ("% d", & N, & P, & K) {for (I = 1; I <= N; I ++) {scanf ("% d", & F [I]. num, & F [I]. unhappy); F [I]. pos = I;} Sort (F + 1, F + n + 1, CMP1); // in descending order of unhappiness, memcpy (S, F, F, sizeof (f); // remember, you must open another struct, not as below me, because after cmp2 is sorted, CMP1 may not be the sequence for (I = 1; I <= N; I ++) f [I]. pp = I; sort (F + 1, F + N-(p-k) + 1, cmp2 ); // leave the minimum number of p-K numbers to force the Chairman to select the former num. (If the chairman is forced temporarily, it will be updated later.) Int J = 1; for (I = 1; I <= K; I ++) {J = max (J, F [I]. pp); if (I = 1) printf ("% d", F [I]. pos); else printf ("% d ", F [I]. pos);} Sort (S + J + 1, S + n + 1, CMP1); for (I = J + 1; I <= J + P-K; I ++) printf ("% d", s [I]. pos); printf ("\ n");} return 0;} // Add an error code that allows me to write my own vomit blood. // No two arrays are opened, wa To dead/* # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define n 100005 struct stud {int hair, anger; int POs, pp;} f [N]; int N, P, K; int CMP1 (stud a, stud B) {if (. anger = B. anger) return. hair <B. hair; return. anger> B. ange R;} int cmp2 (stud a, stud B) {if (. hair = B. hair) return. anger> B. anger; return. hair> B. hair;} int main () {int N, I; while (~ Scanf ("% d", & N, & P, & K) {for (I = 1; I <= N; I ++) {scanf ("% d", & F [I]. hair, & F [I]. anger); F [I]. pos = I;} Sort (F + 1, F + n + 1, CMP1); for (I = 1; I <= N; I ++) f [I]. pp = I; sort (F + 1, F + n + 1-(p-k), cmp2); Int J = 1; for (I = 1; I <= K; I ++) {if (I = 1) printf ("% d", F [I]. pos); else printf ("% d", F [I]. pos); j = max (J, F [I]. pp);} Sort (F + 1, F + n + 1, CMP1); sort (F + J + 1, F + n + 1, CMP1 ); for (I = J + 1; I <= J + P-K; I ++) printf ("% d", F [I]. pos); printf ("\ n");} return 0 ;}*/




Related Article

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.