POJ 2709 Painter 贪心详解

本文介绍了一种算法,用于解决如何从商店购买最少次数的颜料套件以满足特定颜色及灰色颜料需求的问题。通过一系列步骤,包括计算所需颜料的最大体积、配置灰色颜料等,确保了最少购买次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值