Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2424348
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类:

2010-01-20 13:17:19


Painter
Submit: 98   Accepted:59
Time Limit: 1000MS  Memory Limit: 65536K
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


Source
Mid-Central USA 2005

题意描述:
给定不同颜色的paint瓶子套装N个,套装中的每个瓶子的容量是50ml,每3种不同颜色的paint xml混合后可以得到xml灰色的paint,求最少需要的套装数量,满足给出的paint需求。

用了一个最stupid的方法,二分+模拟整个过程。每次选择剩余容量最大的三种染料来混合成灰色,直到不能混合为止。

这个题的数据量很小,不会出现TLE

我的代码


 

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

int need[20], high, low, N, G, color[20];

int cmp(const void *a, const void *b)
{
    int *pa = (int *)a;
    int *pb = (int *)b;
    return *pa - *pb;
}

bool available(int mid)
{
    int i, j = G;
    for (i=0 ; i<N ; i++)
    {
        color[i] = 50 * mid - need[i];
        if (color[i] < 0)
            return false;
    }
    
    while (j)
    {
        /*每次混合出1ml的灰色染料后都要重排序*/
        qsort(color, N, sizeof(int), cmp);
        /*没有3种可用的燃料了,退出*/
        if (color[N - 3] == 0)
            break;
        color[N - 1] -= 1;
        color[N - 2] -= 1;
        color[N - 3] -= 1;
        j--;
    }
    if (0 == j)
        return true;
    else
        return false;
}

int main(int argc, char *argv[])
{
    int i, mid, suc;

    while (cin >> N && N)
    {
        for (i=0 ; i<N ; i++)
            cin >> need[i];
        cin >> G;
        high = 100;
        low = 0;

        /*在0-100之间二分查找结果*/
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (available(mid))
            {
                high = mid - 1;
                suc = mid;
            }
            else
                low = mid + 1;
        }
        printf("%d\n", suc);
    }
}


下面这个是别人的代码,思路差不多,但是没有使用二分,效率更好:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void*,const void *);
int main()
{
    int n,i,gray;
    int a[13];
    memset(a,0,sizeof(a));
    while (scanf("%d",&n),n)
    {
        for (i=0;i<n;i++)
        {
            scanf("%d",a+i);
        }
        scanf("%d",&gray);
        qsort(a,n,sizeof(int),cmp);
        for (i=0;i<gray;i++)
        {
            a[0]++;
            a[1]++;
            a[2]++;
            qsort(a,n,sizeof(int),cmp);
        }

        /*用这个来判断满足条件的最小值*/
        printf("%d\n",a[n-1]%50==0?a[n-1]/50:a[n-1]/50+1);
        memset(a,0,sizeof(a));
    }
}
int cmp(const void*a,const void *b)
{
    return *(int*)a-*(int*)b;
}


阅读(923) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~