Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3186 | Accepted: 1983 |
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
Source
题目,给你颜色的种类,然后给你需要的每种颜色的数量,还给你需要的灰度,任意三种颜色混合都能得到等量的灰度,问最少需要多少套颜色(每套颜色包含所有的颜色除了(灰度));
贪心,注意,对于最后一组测试数据,不能50 50 50 50 50 50先把前三种颜色混合这样是不对的,不知道为什么?应该是一毫升一毫升的混合,这样才是最优的。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 16
#define min3(a,b,c) min(min(a,b),c)
using namespace std;
int clor[N],sclor[N];///sclor[]是把所需颜色满足需求后剩下的每种颜色数量
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
int main()
{
int n,i;
int gray,ans,V;
while(~scanf("%d",&n)&&n)
{
for(i=0; i<n; i++)
{
scanf("%d",&clor[i]);
}
scanf("%d",&gray);
qsort(clor,n,sizeof(clor[0]),cmp);
if(gray||clor[0])
{
ans=1;
V=50;
}
else if(!clor[0]&&!gray)
{
ans=0;
V=0;
}
while(V<clor[0])///找到满足除灰度以外的颜色最小套数
{
ans++;
V=ans*50;
}
for(i=0; i<n; i++)
{
sclor[i]=V-clor[i];
}
qsort(sclor,n,sizeof(sclor[0]),cmp);
while(gray>0)
{
int total=0;
while(sclor[2])///寻找满足灰度的最小套数
{
int minc=1;
//minc=min3(sclor[0],sclor[1],sclor[2]);
total+=minc;
sclor[0]-=minc;
sclor[1]-=minc;
sclor[2]-=minc;
qsort(sclor,n,sizeof(sclor[0]),cmp);
}
gray-=total;
if(gray<=0)///如果灰度已经满足,退出循环
{
break;
}
for(i=0; i<n; i++)
sclor[i]+=50;
ans++;
}
cout<<ans<<endl;
}
return 0;
}