POJ2709

Problem: Painter
Description: 买颜料,有n种需要的颜料,现在要买规格为50ml一瓶的一套颜料,一套颜料中有你所有需要的颜料。但是没有灰色的颜料,灰色的颜料需要任意三种颜料去搭配,但是搭配后体积不会变,也就是说,每种选用aml,出来的灰色还是aml,现在问你最少需要买多少套颜料。
Solution: 贪心。我们可以得到一个要买颜料的最大值,然后用进一法买这么多瓶颜料,这个时候,有些颜料就会有多出来的。我们用这些多出来的颜料来配置灰色。这个时候要注意,我们要用剩下的最多的三种配,每次配1ml(这个是为了最大程度地保证所买的颜料套数最少,我试了每次不配1ml,每次配最多的三种中的最少的那一种,过不去样例。每次配1ml能最大化利用资源,细分吗,仔细想一下就能明白),每次都进行排序。如果最多的三种中的最少的那一种为0,那么我们就要多买一套颜料,再进行配置。
Code(C++):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define MAX(a,b) ((a)>(b)? (a):(b))

const int M=12;

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

int main()
{
    int n;
    int need[M],rest[M],G;
    int tmp_max;
    while(scanf("%d",&n),n){
        memset(need,0,sizeof(need));
        tmp_max=0;
        for(int i=0;i<n;i++)
            scanf("%d",&need[i]),
            tmp_max=MAX(need[i],tmp_max);
        scanf("%d",&G);

        int ans=ceil(tmp_max*1.0/50);
        for(int i=0;i<n;i++)
            rest[i]=ans*50-need[i];

        while(G>0){
            qsort(rest,n,sizeof(rest[0]),cmp);
            if(!rest[n-3]){
                ++ans;
                for(int i=0;i<n;i++)
                    rest[i]+=50;
            }
            rest[n-1]--;
            rest[n-2]--;
            rest[n-3]--;
            G--;

        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(POJ2709)