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

POJ 2709-Painters 贪心问题

2010-08-15 21:32 281 查看
题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=2709



解题报告:



很容易想到的贪心算法。



先获得不需要gray色的时候,所需的kits数量,然后得到每个颜色剩下颜料的ml数,从剩下的颜色中,每次取体积最大的三种,每种取1ml,获得1ml的gray,如果颜色用完了,就kits+1,相应的每个颜色剩下的颜料的ml数加上50,再依次下去,直到获得了对应的gray。



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




附录:



Painter

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 1319Accepted: 801
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: