http://poj.org/problem?id=2709
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
每个颜料盒可能有3-12种颜色,其中每种颜色50ml。任意三种颜色(假设每种颜色Xml)可以混合出Xml的灰色。现在给出所需颜色的种数N,给出N个值分别代表每种颜色所需量,最后给出需要的灰色的量。问最少需要多少个颜料盒可以得到上述的颜色。
注意:假设数据为:4 90 95 75 95 10。也就是需要颜色数为4(蕴含着你取得颜料盒中只有四种颜色,而不是每个颜料盒都有12个颜色的),分别需要90,95,75,95,灰色需要10。
int cmp(int a,int b)
{
return a > b;
}
int main()
{
int N,ans,gray,temp,i;
int color[15];
while(cin>>N && N)
{
for(i=0;i<N;i++)
cin>>color[i];
cin>>gray;
sort(color,color+N);//将所需颜色量从小到大排序
ans=0;
//计算除了灰色之外一共需要多少颜料盒
if(color[N-1]%50==0)
ans=color[N-1]/50;
else
ans=color[N-1]/50+1;
for(i=0;i<N;i++)
color[i]=50*ans-color[i];//剩余颜色量从大到小排序
temp=0;//合成灰色的量
while(temp<gray)
{
if(color[2]==0)//当三种颜色中的最小值等于0时,需要添加一个颜料盒
{
for(i=0;i<N;i++)
color[i]+=50;
ans=ans+1;
}
color[0]--;
color[1]--;
color[2]--;
temp++;
sort(color,color+N,cmp);//每次都将个个颜色的量从大到小排序
}
cout<<ans<<endl;
}
return 0;
}
用贪心来解决,先求出满足的普通色所需的最小盒数,然后把剩余颜料从大到小排列,那前三种每个取出1ml组成1ml的灰色(每次取1ml才能达到最优解)。
static
int
Gray =
0
;
public
static
void
main(String[] args) {
Scanner in =
new
Scanner(System.in);
while
(in.hasNext())
{
int
nCase = in.nextInt();
if
(nCase==
0
)
break
;
int
max =
0
;
Integer[] array =
new
Integer[nCase];
for
(
int
i=
0
;i<nCase;i++)
{
array[i]=in.nextInt();
if
(array[i]>max)
max = array[i];
}
Gray = in.nextInt();
System.out.println(getAns(array,max));
}
}
static
int
getAns(Integer[] array,
int
max)
{
int
count=
0
;
count+=max/
50
;
if
(max%
50
!=
0
)
count++;
for
(
int
i=
0
;i<array.length;i++)
{
array[i] = count*
50
-array[i];
}
while
(Gray>
0
)
{
Arrays.sort(array,Collections.reverseOrder());
if
(array[
2
]>
0
)
{
Gray--;
array[
0
]--;
array[
1
]--;
array[
2
]--;
}
else
{
count++;
for
(
int
i=
0
;i<array.length;i++)
array[i]+=
50
;
}
}
return
count;
}