YT04-贪心课后练习-1006—PAINTER(6.14日-烟台大学ACM预备队解题报告)

14 篇文章 0 订阅

2709  PAINTER—计141 吴胜男


Description

Thelocal toy store sells smallfingerpaintingkits with between three and twelve 50ml bottles of paint, each a differentcolor. The paints are bright and fun to work with, and have the useful propertythat 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 whenyou mix them together the volume doesn't increase, the paint just gets moredense.) None of the individual colors are gray; the only way to get gray is bymixing exactly three distinct colors, but it doesn't matter which three. Yourfriend Emily is an elementary school teacher and every Friday she does afingerpaintingproject with her class. Given the number of different colors needed, the amountof each color, and the amount of gray, your job is to calculate the number ofkits needed for her class.


¢Input

   The input consists of one or more test cases, followed by a linecontaining only zero that signals the end of the input. Each test case consistsof a single line of five or more integers, which are separated by a space. Thefirst integer N is the number of different colors (3 <= N <= 12).Following that are N different nonnegative integers, each at most 1,000, thatspecify 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 fingerpaintingkits 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 minimumnumber of kits for a test case you may need to make grays using differentcombinations 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。任意取3种颜料(每种颜料x ml)混合即可得到x ml的灰色颜料(根据油漆的化学性质,不同颜色的油漆混合,体积不会增加,只会使油漆更加粘稠)。给定一套颜料包内颜料种类的数量,每种颜色的总量和所需灰色颜料的量。计算至少使用多少套颜料包。

¢输入:

   输入一个或多个测试数据,最后输入0结束数据输入。

  每行测试数据包括5个或5个以上整数,用空格隔开。

  每行第一个整数是颜色种类的数量,接下来的整数是

  每种颜色需要的量(均小于1000ml),每行最后一个数代表所需灰色的量,单位为ml.


问题解决思路:

¢ 用贪心,先找到需要的最多的那种颜料量max(灰色除外),算出不算灰色时所需要count套颜料。
¢然后用count*50减去每种需要的颜料,就是除了各自所需要的外,还剩多少可以去配灰色。如果灰色颜料数不等于零,利用循环找出最大的三个数。如果三个数都大于0,则灰色颜料数和前三种颜料数减一。如果前三个数中有等于0的,说明不够配灰色了,每种要加50ml,count++。
¢最后输出颜料包的数量。
#include <iostream>
using namespace std;
int main()
{
    int colorNum;
    cin >> colorNum;
    while(colorNum!=0)
    {
        int grayNum;
        int* color=new int[colorNum];
        int kitsNum;
        for(int i=0; i<colorNum; i++)
            cin >> color[i];
        cin >> grayNum;
        int max=0;
        for(int i=0; i<colorNum; i++)
        {
            if(max < color[i])
                max = color[i];
        }
        kitsNum =max/50+(max%50==0?0:1);
        int temp=kitsNum*50;
        for(int i=0; i<colorNum; i++)
            color[i]=temp-color[i];
        while(grayNum!=0)
        {
            int max1=0,max2=1,max3=2;//最大的三个数的位置
            for(int i=3; i<colorNum; i++)
            {
                if(color[i] > color[max1])
                    max1 = i;
                else if(color[i] > color[max2])
                    max2 = i;
                else if(color[i] > color[max3])
                    max3 = i;
            }
            if(color[max1]>0 && color[max2]!=0 && color[max3]!=0)
            {
                color[max1]--;
                color[max2]--;
                color[max3]--;
                grayNum--;
            }
            else
            {
                kitsNum++;
                for(int i=0; i<colorNum; i++)
                    color[i]+=50;
            }
        }
        cout << kitsNum << endl;
        cin >> colorNum;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值