POJ 2706 Painter

Painter
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3157   Accepted: 1962

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

 

Emily 要买N种颜料,她可以到商店里面买,但是灰色是在商店里买不到的。但是可以用任意三种颜料配成(不包括灰色),现在给出Emily想要的N种颜色的量以及灰色的量,问Emily最少要买多少份颜料(每份颜料包括所有她想要的颜料(当然不包括灰色),每种颜料每瓶50ml)
思路:贪心,首先先把不是灰色的其它n种颜料的量先满足,得到相应的剩余的各种颜色的颜料的量;然后用这些量去匹配灰色的量,当然每次是取最大量的三种颜色的颜料,假如得到的量等于了所需要的灰色的量,就得到了结果,否则一直搞,当一些颜料的量为0,无法合成灰色的时候,结果再加一;
 1 #include <iostream>
 2 #include <string>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     int n;
 9     int i;
10     int a[15];
11     int t;
12     int num;
13     while (cin >> n && n)
14     {
15         for (i = 1; i <= n; i++) cin >> a[i];
16         cin >> t;
17         num = 0;
18         sort(a + 1, a + 1 + n);
19         int k = a[n] / 50;
20         if (k*50 < a[n]) k++;//先配自己的颜料
21         for (i = 1; i <= n; i++)
22         {
23             a[i] = k * 50 - a[i];
24         }
25         num += k;
26         sort(a + 1, a + 1 + n);//要排序切记
27         while (t--)
28         {
29             bool f = 0;
30             for (i = n; i >=n-2; i--)
31             {
32                 a[i]--;
33                 if (a[i] < 0 && f == 0)//如果不够了
34                 {
35                     f = 1;
36                 }
37             }
38             if (f)
39             {
40                 num++;//不够就加一套
41                 for (i = 1; i<=n;i++)
42                 {
43                     a[i] += 50;
44 
45                 }
46             }
47             sort(a + 1, a + 1 + n);//要排序
48         }
49         cout << num << endl;
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/caiyishuai/p/8424558.html