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。
附录:
Painter
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
Sample Output
解题报告:
很容易想到的贪心算法。
先获得不需要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: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1319 | Accepted: 801 |
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
相关文章推荐
- poj 2709 贪心问题
- POJ 1328 Radar Installation【贪心 区间问题】
- POJ-1700 &&NYOJ 47 过河问题【贪心】
- 贪心问题 POJ 2393 Yogurt factory
- POJ 2976 Dropping tests 0/1分数规划问题 最大化平均值 贪心+二分
- POJ 1042 钓鱼问题 贪心枚举及动态规划
- poj 2709 贪心
- 字典序最小问题 前后删除加入 贪心poj3617
- POJ--2709:Painter (贪心)
- poj 1700 Crossing River 过河问题。贪心
- 贪心问题:区间覆盖 POJ 1328 Rader Installation
- poj 1083 hdu1050 Moving Tables 贪心问题
- POJ 1328 Radar Installation【贪心 区间问题】
- [ACM] POJ 1328 Radar Installation (贪心,区间选点问题)
- POJ 2431 Expedition (汽车加油问题,STL_优先队列+贪心) (挑战程序P76)
- poj 2709 贪心
- POJ—2709—Painter—【贪心】
- POJ1328 Radar Installation (区间选点问题,贪心)
- POJ 2709 Painter 贪心
- 贪心基础题(区间调度问题+POJ_3617+POJ_3069)