题目意思:
每个颜料盒可能有3-12种颜色,其中每种颜色50ml。任意三种颜色(假设每种颜色Xml)可以混合出Xml的灰色。
现在给出所需颜色的种数N,给出N个值分别代表每种颜色所需量,最后给出需要的灰色的量。
问最少需要多少个颜料盒可以得到上述的颜色。
注意:假设数据为:4 90 95 75 95 10。也就是需要颜色数为4(蕴含着你取得颜料盒中只有四种颜色,
而不是每个颜料盒都有12个颜色的),分别需要90,95,75,95,灰色需要10。
本题要点:
1、用贪心,先把其他颜料弄到满足需求,拿出剩余的的颜料来合成灰色,
从颜料剩余ml最多的颜料优先合成,每次就合成1ml,如果不够,就增加颜料包,直到灰色满足需求
2、每次都用 sort 排序
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int grey, max_color;
int color[15]; //颜色的数量
int color_left[15];
void add_color() //增加颜料
{
for(int i = 0; i < n; ++i)
{
color_left[i] += 50;
}
}
bool cmp(int a, int b)
{
return a > b;
}
void solve()
{
int ans = max_color / 50;
if(max_color > ans * 50)
{
++ans;
}
for(int i = 0; i < n; ++i)
{
color_left[i] = ans * 50 - color[i];//剩余的颜料数量
}
int g = grey;
while(g--)
{
sort(color_left, color_left + n, cmp);
if(color_left[2] < 1)
{
add_color();
++ans;
}
color_left[0] -= 1, color_left[1] -= 1, color_left[2] -= 1;
}
printf("%d\n", ans);
}
int main()
{
while(scanf("%d", &n) != EOF && n)
{
max_color = -1;
for(int i = 0; i < n; ++i)
{
scanf("%d", &color[i]);
max_color = max(max_color, color[i]);
}
scanf("%d", &grey);
solve();
}
return 0;
}
/*
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
*/
/*
2
8
2
3
4
*/