H - Painter——往小了贪

杂货店出售一种由N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),组成的颜料套装。你现在需要使用这N种颜料;不但如此,你还需要一定数量的灰色颜料。杂货店从来不出售灰色颜料——也就是它不属于这N种之一。幸运的是,灰色颜料是比较好配置的,如果你取出三种不同颜色的颜料各x ml,混合起来就可以得到x ml的灰色颜料(注意不是3x)。

现在,你知道每种颜料各需要多少ml。你决定买尽可能少的“颜料套装”,来满足你需要的这N+1种颜料。那么你最少需要买多少个套装呢?

Input
输入包含若干组测试数据。每组数据一行:第一个数N, 3<=N<=12, 含义如上;接下来N+1个数,分别表示你需要的N+1种颜料的毫升数。最后一种是灰色。所有输入的毫升数<=1000.

注意:输入中不存在每个颜料套装的毫升数。由题意可知,每种各50ml,即一共50N ml

Output
每组数据输出一行,最少需要的套装数。

Sample Input
3 40 95 21 0

7 25 60 400 250 0 60 0 500

4 90 95 75 95 10

5 0 0 0 0 0 333

0

Sample Output
2

8

2

4

思路:
先满足基本颜色,剩下的部分,排序后选择前三一份一份地调成灰色(本来的想法是前三减去clor[2]的,但是最后才发觉变动太大,万一clor[0]刚减去1就比clor[4]小了呢(°ー°〃),所以还得一份一份)。每转化一次就要拍一次序(挺夸张的,还好原料的数量在1000以下,数组也才最大为12)
代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<list>
#include<iterator>
#include<stack>
#include <queue>
#include <cstdio>
#include<algorithm>
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
#define e 2.718281828459
#define INF 0x7fffffff
#pragma warning(disable:4996)
#define sf scanf
#define pf printf
const double pi = acos(-1.0);
const int MAX = 10005;
//#define  eps 1e-9;




bool cmp(int a, int b) {
    return a > b;
}
int main(void) {
    int n;
    int oth;
    int clor[13];
    while (sf("%d", &n) ,n) {
        int max = 0;
        for (int i = 0; i < n; i++) {
            sf("%d", &clor[i]);
            if (max < clor[i])
                max = clor[i];
        }
        sf("%d", &oth);
        int isfull = 0;//现有的灰色原料
        int cmin = max / 50;
        if (max % 50 != 0)
            cmin++;//所需最小数量的套装

        for (int i = 0; i < n; i++) {
            clor[i] = cmin * 50 - clor[i];
        }

        while (isfull < oth) {
            sort(clor, clor + n, cmp);
            while (clor[2] != 0&&isfull<oth) {//一直调配灰色原料,直到第三种原料没有
                isfull++;
                clor[0]--,clor[1]--,clor[2]--;//一份一份来,大了可能会出错的
                sort(clor, clor + n, cmp);
            }

            if (isfull < oth) {
                cmin++;
                for (int i = 0; i < n; i++)
                    clor[i] += 50;
            }
        }

        pf("%d\n", cmin);


    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/jiruqianlong123/article/details/81704419
h'h
8 H
9 H