分类:
2009-08-28 11:25:55
Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
2
8
2
3
4
题意:
给出需要几种颜料,和每种需要的数量,和灰色颜料需要的数量。要求的是至少需要买几套颜料才能满足要求。每套都包含所有需要的颜料,每种50ml,没有灰色颜料,灰色的需要用其他任意三种配成,而且体积不会增加,如用三种颜料,每种都是Xml,那么所配成的灰色颜料也是Xml。
思路:
用贪心,先找到需要的最多的那种颜料量max(灰色除外),算出不算灰色时所需要count套颜料。然后用count*50减去每种需要的颜料,就是除了各自所需要的外,还剩多少可以去配灰色。用qsort按从大到小排序。一个循环,看第三种颜料是不是等于0,而且灰色颜料是不是不等于0.如果是的话,说明不够配灰色了,每种要加50ml,count++。然后灰色颜料数减一,如果等于0了,说明灰色配够了,退出。否则前三种颜料数减一,再把所有从小到大排序。再循环回去。我本来不是一个一个减的,就像 0 0 0 0 0 333 这个是50 50 50 50 50 把前三种都减50,变成0 0 0 50 50,这样比较快,但显然不是最优的,可是又想不到其他方法,后来看到网上一篇解题报告正好讲到这个问题。然后才解决了。
|