【贪心】【模拟】[POJ2709][HDU2730]Painter

版权声明:本文为博主原创文章,转载请注明本页地址。 https://blog.csdn.net/C20180630/article/details/75043737

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种颜料;不但如此,你还需要一定数量的灰色颜料。杂货店从来不出售灰色颜料——也就是它不属于这N种之一。幸运的是,灰色颜料是比较好配置的,如果你取出三种不同颜色的颜料各x ml,混合起来就可以得到x ml的灰色颜料(注意不是3x)。
现在,你知道每种颜料各需要多少ml。你决定买尽可能少的“颜料套装”,来满足你需要的这N+1种颜料。那么你最少需要买多少个套装呢?

输入

输入包含若干组测试数据。每组数据一行:第一个数N, 3<=N<=12, 含义如上;接下来N+1个数,分别表示你需要的N+1种颜料的毫升数。最后一种是灰色。所有输入的毫升数<=1000.
注意:输入中不存在每个颜料套装的毫升数。由题意可知,每种各50ml,即一共50N ml

分析

这道题我第一想到的就是贪心,首先把各种颜料最少需要的套装数算出来,算出各种颜料在买了套装后剩余的量,然后排序。如果灰色颜料还需要,就1mL 1mL的把颜料混合成灰色。每次操作时要排序一遍(哈哈哈,没有超时)。如果排序后第三种颜料小于等于0,就说明前面的都小于等于0,所以就要再买一套套装,然后前三种颜料的量和灰色需要的量都减1mL。

源代码

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,a[15],ans;
bool cmp(int a,int b){return a>b;}
int main()
{
    while(~scanf("%d",&n)&&n){
        ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            ans=max(ans,(a[i]+49)/50);//算出最少要买多少套装
        }
        scanf("%d",&a[0]);
        sort(a+1,a+n+1);//排序
        for(int i=1;i<=n;i++)//算出各种颜色的剩余量
            a[i]=ans*50-a[i];
        while(a[0]>0){
            sort(a+1,a+n+1,cmp);//因为剩余量是从小到大的,所以要从小到大的排序
            if(a[3]<=0){//如果第三种颜色小于等于0,再买一套
            /*!注意:写成a[3]<0是错的*/
                ans++;
                for(int i=1;i<=n;i++)
                    a[i]+=50;
            }
            a[0]--;a[1]--;a[2]--;a[3]--;//每种颜料减1mL
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/C20180630/article/details/75043737