Codevs3372 selector (easy)

Source: Internet
Author: User
Description Description

The teacher wants to select m from N students as a student overlord, but K of the students are equally powerful. If some of the students are chosen, and others are not, the students will protest. Therefore, the teacher would like to ask you to help him find out the number of students he should choose, so that he will not let the students protest, but as close as possible to the original M.

Train of Thought: Combine the persons with the same strength into a collection to count the number of elements in the collection. Then you can create something similar to the 01 backpack (boolean type) and find the optimal solution.

Code:
# Include <cstdio>

# Include <iostream>

Using namespace STD;

Int sum [30001] = {0}, Fa [30001] = {0}, V [30001] = {0 };

Bool visit [30001] = {0}, F [30001] = {false };

Int Rool (int x)

{

If (Fa [x]! = X) Fa [x] = Rool (Fa [x]);

Return Fa [x];

}

Int main ()

{

Int n, m, K, I, j, A, B, R1, R2, NN = 0, maxn, Minn;

Cin> N> m> K;

For (I = 1; I <= N; ++ I)

{

Fa [I] = I;

Sum [I] = 1;

}

For (I = 1; I <= K; ++ I)

{

Cin> A> B;

R1 = Rool ();

R2 = Rool (B );

If (R1! = R2)

{

Fa [R1] = R2;

Sum [R2] = sum [R2] + sum [R1];

}

}

For (I = 1; I <= N; ++ I)

{

R1 = Rool (I );

If (! Visit [R1])

{

++ Nn;

V [NN] = sum [R1];

Visit [R1] = true;

}

}

F [0] = true;

For (I = 1; I <= nn; ++ I)

For (j = N; j> = V [I]; -- J)

F [J] = f [J] | f [J-V [I];

Maxn = 0;

Minn = N;

For (I = m; I> = 1; -- I)

If (F [I])

{

Maxn = I;

Break;

}

For (I = m + 1; I <= N; ++ I)

If (F [I])

{

Minn = I;

Break;

}

If (m-maxn <Minn-m) cout <maxn <Endl;

If (m-maxn> Minn-m) cout <Minn <Endl;

If (m-maxn = Minn-m) cout <maxn <Endl;

}

(Code is so long that it is despised by the group... Wu Yu)

Codevs3372 selector (easy)

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.