简单的贪心

来源:互联网 发布:蜂群算法代码 编辑:程序博客网 时间:2024/06/06 15:58
简单的贪心

Hint

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,后面有n个数据代表有n个除灰色以外的颜色,还有一个数据代表需要灰色颜料的数量。1组颜料是每种颜色50ML,不过买不到灰色的,所以需要你自己配,任意3种1ml的颜色可以配成1ml的灰色。求出最少的组数。

3:思路 找出最大的颜料数除以50,判断要不要进1。得到组数,然后看看是不是可以配出灰色,不够的话再加一组。 我觉得最一个样例给的特别好,333ml灰色的为什么是只需要4组那? 4*5*50/3=333。兑灰色的时候 要一毫升一毫升的配。这样可以将所有的颜料都用完,即可求出最少组数。

4:感想 只要想到需要一毫升一毫升的配就简单啦,思路还是最重要的。

#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;int a[13],b[13];int main(){    int n;    while(scanf("%d",&n)!=-1)    {        if(n==0)            break;        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        int m;        scanf("%d",&m);        sort(a,a+n);        int h;        h=a[n-1];        int sum=0;        if(h%50!=0)            sum=h/50+1;            else                sum=h/50;        int p=0;        for(int i=0; i<n; i++)        {            b[i]=50*sum-a[i];        }        while(m>0)        {            sort(b,b+n);            if(b[n-3]==0)            {                sum++;                for(int i=0; i<n; i++)                {                    b[i]=b[i]+50;                }            }            else            {                m--;                b[n-1]--;                b[n-2]--;                b[n-3]--;            }        }        printf("%d\n",sum);    }}

0 0