Poj 2709 painter (Greedy Algorithm]

Source: Internet
Author: User

[Original question link]

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2709

 

[Topic]

To configure N (3 <= n <= 12) colors and a certain amount of gray pigments, it can be seen that gray pigments can be obtained by mixing any three non-gray pigments with the same amount. The amount of pigments required for each color (including gray) is input, determine how many pigments are required (each pigment includes all colors except black, each color is 50 ml per bottle ).

 

[Solution ideas]

GreedyAlgorithm, The volume decreases by mL, and the required volume increases gradually, and the number of copies of the required color is obtained.

 

[Remarks]

Here, mixing is not necessarily a bottle-by-bottle mixture, but can be a mixture of any volume.

 

[SourceProgram]

[Code]

# Include <iostream>

# Include <algorithm>

# Include <string>

Using namespace STD;

 

Long colvol [20], remain [20];

 

Bool CMP (long a, long B) {return A> B ;}

 

Int main ()

{

Long colnum, gray, I, empty, in;

While (CIN> colnum, colnum)

{

For (I = 0; I <colnum; I ++) CIN> colvol [I];

Sort (colvol, colvol + colnum );

Cin> gray;

If (colvol [colNum-1] % 50) in = (colvol [colNum-1]/50 + 1) * 50;

Else In = colvol [colNum-1];

// The quantity to be added when the in is not black

For (I = 0; I <colnum; I ++) remain [I] = In-colvol [I]; // remain indicates the quantity of black colors that can be used for matching.

While (Gray) // if the required amount of Black is not 0, the loop continues.

{

Sort (remain, remain + colnum, CMP); // sort each time from high to low

If (! Remain [2]) // if the remaining color of the third digit has been used up, use another pigment.

{In + = 50; for (I = 0; I <colnum; I ++) remain [I] + = 50 ;}

Remain [0] --; remain [1] --; remain [2] --; gray --;

}

Cout <in/50 <Endl; // number of output bottles

}

Return 0;

}

[/Code]

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.