2013
11-11

# Painter

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.

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.

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.

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


2
8
2
3
4


import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
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;
}

}

1. 漂亮。佩服。
P.S. unsigned 应该去掉。换行符是n 不是/n
还可以稍微优化一下，
int main() {
int m,n,ai,aj,bi,bj,ak,bk;
while (scanf("%d%d",&m,&n)!=EOF) {
ai = sqrt(m-1);
bi = sqrt(n-1);
aj = (m-ai*ai-1)>>1;
bj = (n-bi*bi-1)>>1;
ak = ((ai+1)*(ai+1)-m)>>1;
bk = ((bi+1)*(bi+1)-n)>>1;
printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
}
}

2. 嗯 分析得很到位，确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样：push时，比较要push的elem和辅助栈的栈顶，elem<=min.top()，则min.push(elem).否则只要push（elem）就好。在pop的时候，比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();}，否则{stack.pop();}.