YT04-貪心課後練習-1006—PAINTER(6.14日-煙臺大學ACM預備隊解題報告)

kewlgrl發表於2015-06-21

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;
    }
}


相關文章