题目
Painter
Time Limit: 1000MS Memory Limit: 65536K
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
题目大意
一个玩具店卖颜料,每套颜料有N(3≤N≤12)种颜色,每种颜色50ml,玩具店从不卖灰色颜料,但你可以用任意三种不同的颜料配出灰色,配出的灰色的体积不是三种颜料体积之和,而是最少的那个颜料,现在给出你需要的N种颜料的体积和灰色的体积,求最少需要多少套颜料。
思路
看过题目,首先想到将除开灰色之外需要的最少套数算出来,然后判断用剩下的颜料能否配出足够的灰色。
这个思路……当然没有问题,但是怎么判断“能否配出足够的灰色”呢?
当然不能直接取剩余最多的3个颜料算,如果最多的三个颜料够配出灰色还好,如果不能配出灰色,后面的颜料也是可以用的。
例如有5盒颜料,编号1,2,3,4,5,其中1,2,3号剩余的最多,配出的灰色却还不够,所以我们想再买一盒,然后继续看1,2,3号,事实上,4,5号也是可以和1,2,3配对的,而且有可能需要的盒数更少,所以不能一直算第一次最大的三个。
那很好办,每次sort从大到小一次就可以了啊,但还有一个问题,每次都把sort后的1,2,3号颜料直接全部配成灰色吗?不是的,你看看样例4就知道了,虽然我并不知道样例4怎么算的,但肯定不是每次配最多的3个。
所以,我们索性1毫升1毫升的配每配1次sort1次,N只有12,也不用担心超时~
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int p[15],gray;
bool cmp(int a,int b){return a>b;}//自定义sort排序从大到小
int main()
{
while(1)
{
int N,tmax=-1;
scanf("%d",&N);
if(!N) return 0;
for(int i=1;i<=N;i++)
{
scanf("%d",&p[i]);
tmax=max(tmax,(p[i]+49)/50);//算出除开灰色最少需要多少套颜料,注意是上取整
}
scanf("%d",&gray);
for(int i=1;i<=N;i++)
p[i]=tmax*50-p[i];//将用去的颜料减去
while(gray>0)
{
sort(p+1,p+N+1,cmp);
if(p[3]<=0)//要配的是p[1],p[2],p[3],sort后p[3]是最小的,如果p[3]都不够的,肯定要再买一套
{
for(int i=1;i<=N;i++)
p[i]+=50;//所有颜料增加50ml
tmax++;//套数增加
}
p[1]--;p[2]--;p[3]--;
gray--;//每次只配1ml
}
printf("%d
",tmax);
}
}