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
题目难度不大,需要注意的是不要直接减50这样可能得不到最优解(这代码是我自己私下写的)
/*---------------------------------------------------------------------------------
* FileName:Painter.c
* author:doodlesomething@163.com
* date:10-29-2014
* version:1.0
* description:Painter POJ-2709 solved by Greedy Algorithm
* more:使用贪心算法解决即可,这里是选择最大的优先处理
-----------------------------------------------------------------------------------*/
#include <stdio.h>
#define N 20
void QuickSort(int arr[],int low,int high) {
int first,last,key;
if(low > high) {
return;
}
first = low;
last = high;
key = arr[first];
while(first < last) {
//从后往前找第一个比key大的
while(first < last && arr[last] <= key)
last--;
//移到低端
arr[first] = arr[last];
//从前往后找第一个比key小的
while(first < last && arr[first] >= key)
first++;
//移到高端
arr[last] = arr[first];
}
//记录枢纽位置
arr[first] = key;
QuickSort(arr,low,first - 1);
QuickSort(arr,first + 1,high);
}
int main() {
int n,i,max,total,grey;
int colors[N];
max = 0;
total = 0;
//i输入
printf("please enter n:");
scanf("%d",&n);
printf("please enter amount of different colors:");
for(i = 0; i < n; i++) {
scanf("%d,",&colors[i]);
if(max < colors[i])
max = colors[i];
}
printf("please enter the ammout of grey:");
scanf("%d",&grey);
//先算出除了灰色以外所需要的数目
if(max % 50) {
total = max / 50 + 1;
}
else {
total = max / 50;
}
//算出剩余用来配置灰色的量
for(i = 0; i < n ; i++) {
colors[i] = total * 50 - colors[i];
}
//配置灰色
while(grey > 0) {
//进行降序排序
QuickSort(colors,0,n - 1);
/*
理解这里是关键,由于colors数组的降序的,选取三种颜色来配色
则在这三种颜色中最先没有的肯定是第三种,注意这里是刚排完序
则第三种之后的颜色必定没有第三中多,则需要另外新开一个,并
更新各种颜色的剩余量
*/
if(colors[2] <= 0) {
total++;
for(i = 0; i < n; i++) {
colors[i] += 50;
}
}
//一毫升配置,相应减一
colors[0]--;
colors[1]--;
colors[2]--;
grey--;
}
printf("total:%d\n",total);
return 0;
}