Return to Snippet

Revision: 44765
at April 18, 2011 12:15 by dirkchang


Initial Code
/*
 * ecological-bin.cpp
 *
 *  Created on: Feb 23, 2011
 *      Author: dirk
 */
 
#include <iostream>
using namespace std;
 
enum Sequence { BCG = 0, BGC = 1, CBG = 2, CGB = 3, GBC = 4, GCB = 5, NUMBER = 6 };
 
char const * const &get_str(Sequence const &s) {
    static char const * const str[] = { "BCG", "BGC", "CBG", "CGB", "GBC", "GCB" };
    return str[s];
}
 
int main(int, char**) {
    // bottles
    // B G C B G C B G C
    // 0 1 2 3 4 5 6 7 8
    long long bottles[9] = {0};
    long long m = 0, tmp = 0;
    Sequence index;
 
    while(cin >> bottles[0]) {
        for(int i = 1; i < 9; ++i) {
            cin >> bottles[i];
        }
 
        m = bottles[1] + bottles[2] + bottles[3] + bottles[4] + bottles[6] + bottles[8]; // BCG
        index = BCG;
 
        if((tmp=bottles[1] + bottles[2] + bottles[3] + bottles[5] + bottles[6] + bottles[7]) < m) {
            m = tmp; // BGC
            index = BGC;
        }
        if((tmp=bottles[0] + bottles[1] + bottles[4] + bottles[5] + bottles[6] + bottles[8]) < m) {
            m = tmp; // CBG
            index = CBG;
        }
        if((tmp=bottles[0] + bottles[1] + bottles[3] + bottles[5] + bottles[7] + bottles[8]) < m) {
            m = tmp; // CGB
            index = CGB;
        }
        if((tmp=bottles[0] + bottles[2] + bottles[4] + bottles[5] + bottles[6] + bottles[7]) < m) {
            m = tmp; // GBC
            index = GBC;
        }
        if((tmp=bottles[0] + bottles[2] + bottles[3] + bottles[4] + bottles[7] + bottles[8]) < m) {
            m = tmp; // GCB
            index = GCB;
        }
 
        cout << get_str(index) << ' ' << m << '\n';
    }
}

Initial URL


Initial Description


Initial Title
Ecological Bin Packing

Initial Tags


Initial Language
C++