20111014

ACM 102 Ecological Bin Packing

移動的瓶子 + 沒移動的瓶子 = 總瓶數
換句話說,沒移動的瓶子越多,移動的瓶子就會越少。
排列組合共有6種情況:3 * 2 * 1。
分別計算每一種情況下,沒移動的瓶子數。
找到最大數之後,與總瓶數相減,即為最少移動瓶子數。
想說不要直接算移動瓶子數應該會比較快,有嗎?
我也不知道……
code可讀性應該很差……

保險一點用unsigned int才不會overload,
但既然AC就懶得改了。 -_-

/* ACM 102
 * mythnc
 * 2011/10/14 19:22:33   
 * run time: 0.028
 */
#include <stdio.h>

#define MAXARY 6  /* six permutations */
#define BIN    3

int findmax(int *);
void output(int *);

int main(void)
{
    int b, c, g, i;
    /* cmp counts bottles which don't have to move */
    /* each element to a permutation */
    int cmp[MAXARY]; 

    i = 0;
    while (scanf("%d %d %d", &b, &g, &c) == 3) {
        switch (i++) {
            case 0:
                cmp[0] = cmp[1] = b;
                cmp[2] = cmp[3] = c;
                cmp[4] = cmp[5] = g;
                break;
            case 1:
                cmp[0] += c;
                cmp[1] += g;
                cmp[2] += b;
                cmp[3] += g;
                cmp[4] += b;
                cmp[5] += c;
                break;
            case 2:
                cmp[0] += g;
                cmp[1] += c;
                cmp[2] += g;
                cmp[3] += b;
                cmp[4] += c;
                cmp[5] += b;
                /* sum of element 0, 3, 4 is the total bottles */
                output(cmp);
                i = 0;
        }
    }
    return 0;
}

/* findmax: find the max element of s */
int findmax(int *s)
{
    int i, max;

    for (max = i = 0; i < MAXARY; i++)
        if (s[i] > s[max])
            max = i;
    return max;
}

/* output: output result */
void output(int *s)
{
    switch(findmax(s)) {
        case 0:
            printf("BCG %d\n", s[3]+s[4]);
            break;
        case 1:
            printf("BGC %d\n", s[2]+s[5]);
            break;
        case 2:
            printf("CBG %d\n", s[1]+s[5]);
            break;
        case 3:
            printf("CGB %d\n", s[0]+s[4]);
            break;
        case 4:
            printf("GBC %d\n", s[0]+s[3]);
            break;
        case 5:
            printf("GCB %d\n", s[1]+s[2]);
    }
}

沒有留言: