您的位置:首页 > 其它

ACM ProblemK

2016-03-27 16:21 218 查看
我现在做的是编号为1010的试题,具体内容如下:

Problem K

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 47 Accepted Submission(s) : 18
[align=left]Problem Description[/align]
The local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if you mix X ml each of
any three different colors, you get X ml of gray. (The paints are thick and "airy", almost like cake frosting, and when you mix them together the volume doesn't increase, the paint just gets more dense.) None of the individual colors are gray; the only way
to get gray is by mixing exactly three distinct colors, but it doesn't matter which three. Your friend Emily is an elementary school teacher and every Friday she does a fingerpainting project with her class. Given the number of different colors needed, the
amount of each color, and the amount of gray, your job is to calculate the number of kits needed for her class.

[align=left]Input[/align]
The input consists of one or more test cases, followed by a line containing only zero that signals the end of the input. Each test case consists of a single line of five or more integers, which are separated by a space. The first
integer N is the number of different colors (3 <= N <= 12). Following that are N different nonnegative integers, each at most 1,000, that specify the amount of each color needed. Last is a nonnegative integer G <= 1,000 that specifies the amount of gray needed.
All quantities are in ml.<br>

[align=left]Output[/align]
For each test case, output the smallest number of fingerpainting kits sufficient to provide the required amounts of all the colors and gray. Note that all grays are considered equal, so in order to find the minimum number of kits
for a test case you may need to make grays using different combinations of three distinct colors.

[align=left]Sample Input[/align]

3 40 95 21 0
7 25 60 400 250 0 60 0 500
4 90 95 75 95 10
4 90 95 75 95 11
5 0 0 0 0 0 333
0


[align=left]Sample Output[/align]

2
8
2
3
4


简单题意:

每套颜料包3-12种颜料(没有灰色),每种颜料50ml。任意取3种颜料(每种颜料x ml)混合即可得到x ml的灰色颜料增加,给定一套颜料包内颜料种类的数量,每种颜色的总量和所需灰色颜料的量。计算至少使用多少套颜料包。

解题思路:

利用贪心算法对颜料按照容量降序排列,得到最大的颜料量,算出不算灰色时所需的颜料数,然后,计算除了自己所需的,还剩多少去配灰色颜料,再利用贪心算法进行降序排列,找到最大的三个数,如果第三个数大于0,则灰色颜料数和前三种颜料都减1,直到第三个数为0.

编写代码:

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

bool cmp(int a, int b)

{

return a > b;

}

int main()

{

vector<int> v;

int t;

while (cin >> t)

{

if(t == 0)

break;

else

{

int n, total;

for (int i=0; i<t; i++)

{

cin >> n;

v.push_back(n);

}

int grayNum;

cin >> grayNum;

sort(v.begin(), v.end(), cmp);

if (v[0] % 50 == 0)

total = v[0] / 50;

else

total = v[0] / 50 + 1;

for (int i=0; i<t; i++)

{

v[i] = total * 50 - v[i];

}

while(grayNum > 0)

{

sort(v.begin(), v.end(), cmp);

if (v[2] == 0)

{

total++;

for (int i=0; i<t; i++)

{

v[i] += 50;

}

}

else

{

grayNum--;

v[0]--;

v[1]--;

v[2]--;

}

}

cout << total << endl;

v.clear();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: