Painter
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3181
Accepted: 1978

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
题意:当地玩具店卖绘画颜料,一套大约3-12盒,每盒50ml,且每盒的颜色都不一样。任意三种不同的颜料混合可以配出灰绿色,但是体积不会变,只是变得更浓密一点。你的朋友艾米是个老师,她需要几种颜料来上课,让你计算出最少要买多少套颜料。
输入第一个数N,表示需要颜料的种数;接着N个整数,代表每种颜料需要多少毫升(我用need数组来存);最后输入一个整数gray,表示额外还要多少毫升灰绿色颜料。由于每套到底多少盒并不确定,所以我们可以认为每一套就N盒。当N=0时结束。

分析:先抛除灰绿色颜料,我们可以计算出需要多少套(sum)颜料来满足所需的N种颜料,由N种颜料最多的可以计算出,所以用了排序。再在此基础上额外配置灰绿色颜料,此时sum的值可能会变。另外最优的贪心算法是一毫升一毫升的配置灰绿色颜料。


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int N,i;
    while(cin>>N&&N!=0)
    {
        int need[12],gray,maxn=0,sum=0;
        for(i=0;i<N;i++)
            cin>>need[i];
        cin>>gray;
        sort(need,need+N);
        maxn=need[N-1];
        if(maxn%50==0)
            sum=maxn/50;
        else
            sum=maxn/50+1;
        while(gray>0)   //模拟3种不同的颜料配置灰绿色的过程
        {
            gray--;
            need[0]=need[0]++;
            int s0=need[0]%50==0?need[0]/50:need[0]/50+1;  //need[0]加1后计算它的套数
            need[1]=need[1]++;
            int s1=need[1]%50==0?need[1]/50:need[1]/50+1;  //need[1]加1后计算它的套数
            need[2]=need[2]++;
            int s2=need[2]%50==0?need[2]/50:need[2]/50+1;  //need[2]加1后计算它的套数
            sum=s0>sum?s0:sum;  //看看套数sum变没变
            sum=s1>sum?s1:sum;
            sum=s2>sum?s2:sum;
            sort(need,need+N);
        }
        cout<<sum<<endl;
    }
    return 0;
}







posted on 2014-09-20 11:21  星斗万千  阅读(205)  评论(0编辑  收藏  举报