站点图标 陌路寒暄

贪心之Painter

贪心之Painter

时间: 1ms        内存:64M

描述:

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

提示:

参考答案(内存最优[1092]):

#include<stdio.h>
int main()
{
	int i,j,n[100],x,grey;
	int temp,p,sum;
	while(scanf("%d",&x)!=EOF&&x!=0)
	{
		for(i=0;i<x;i++)
			scanf("%d",&n[i]);
		scanf("%d",&grey);
	for(p=0;p<grey;p++)
	{
		for(i=0;i<x-1;i++)
		{
			for(j=i+1;j<x;j++)
			{
				if(n[i]>n[j])
				{	
					temp=n[i];
					n[i]=n[j];
					n[j]=temp;
				}
			}
		}
		n[0]++;
		n[1]++;
		n[2]++;
	}
	for(i=0;i<x-1;i++)
	{
		for(j=i+1;j<x;j++)
		{
			if(n[i]>n[j])
			{
				temp=n[i];
				n[i]=n[j];
				n[j]=temp;
			}
		}
	}
	if(n[x-1]%50==0)
		sum=n[x-1]/50;
	else
		sum=n[x-1]/50+1;
	printf("%d\n",sum);
	}
	return 0;
}
	

参考答案(时间最优[0]):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<iostream>
#include<algorithm>
using namespace std;

bool myfunction (int i,int j) {
    return (i>j);
}

int main(){
    int n, p_num[13], gray_num, k,i;
    while(cin>>n, n){
        for(i = 0; i < n; i++){
            cin>>p_num[i];
        }
        cin>>gray_num;
        sort(p_num, p_num + n, myfunction);
        if(p_num[0] % 50){
            k = p_num[0] / 50 + 1;
        } else {
            k = p_num[0] / 50;
        }
        for(i = 0; i < n; i++){
            p_num[i] = k * 50 - p_num[i];
        }
        while(gray_num != 0){
            sort(p_num, p_num + n, myfunction);
            if(p_num[2] <= 0){//排序后立刻判断
                k++;//加一个颜料盒
                for(i = 0; i < n; i++){
                    p_num[i] += 50;
                }
            }
            p_num[0]--;
            p_num[1]--;
            p_num[2]--;
            gray_num--;
        }
        cout<<k<<endl;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版