您的位置:首页 > 大数据 > 人工智能

POJ 2709 Painter 贪心详解

2017-08-18 12:44 274 查看
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

题意:

商店卖画画颜料。任意取三种每种各Xml的颜料可以混合出Xml的灰色颜料(注意不是3Xml),商店不单独卖灰色,并且每次每种颜料卖50ML,输入你需要N种颜料,以及每种所需的体积,以及需要灰色颜料的体积G,求最少从商店买几次颜料。

思路:先找到需要最多体积的那个颜料,用它除以50向上取整加给sum,再用它的值减数组中每个元素,得到剩余的颜料值,用这些来配置灰色,我们要1ml 1ml的配置,每次都排序,取最多的三种配1ml。如果最后剩下的不够三种,灰色还没配置够,就再去商店买一次,所有数组元素加50,sum加1。当灰色配置够了,输出sum的值即为结果。

注:为什么1ml 1 ml配置灰色:

比如现在有5种颜料,每种3ml,即 3 3 3 3 3,如果直接拿3个配,只能配出来3ml灰色,剩下3 3 0 0 0

如果1ml 1ml配,就是

2 2 2 3 3

2 2 1 2 2

0 1 1 2 2

0 0 0 1 2

可配4ml

代码如下:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int N,a[13],G,g;//g为配置出的灰色体积
int sum;
void make(void)//调用一次配出1ml灰色
{
sort(a,a+N);//先排序
int i;
for(i=N-3;a[i]!=0&&i>=0&&g!=G;i++)//每次取最多的三个配灰色
{
a[i]--;
}
g++;
}
int ins(void)//函数检查数组内用光的颜料有几种
{
int i,s=0;
for(i=0;i<N;i++)
{
if(a[i]==0)
s++;
}
return s;
}
void add(void)//如果颜料不够配1ml灰色了,再去商店买一次
{
int i;
for(i=0;i<N;i++)
a[i]+=50;
sum++;
}
void solve(void)
{
int i,t=0,temp;
for(i=0;i<N;i++)
{
if(a[i]>a[t])
t=i;
}
sum+=a[t]/50;
if(a[t]%50)
sum++;
temp=sum*50;
for(i=0;i<N;i++)
a[i]=temp-a[i];
//上面表示先找到所需体积最大的颜料,然后sum+=该颜料体积/50向上取整,然后更新数组,数组内表示剩余体积
while(g!=G)
{
if(ins()>=(N-2))//不够配置1ml就再去商店买
add();
make();
}
cout<<sum<<endl;
}
int main(void)
{
int i;
while(cin>>N&&N)
{
for(i=0;i<N;i++)
cin>>a[i];
cin>>G;
g=0;sum=0;
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj acm