【題解】Google Kick Start 2020 Round E High Buildings

【題目敘述】https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff47/00000000003bef73

#include <iostream>
using namespace std;

int t, Case, n, a, b, c;

int main() {
    cin >> t;
    while (t--){
        if (Case) cout << "\n";
        Case++;
        cout << "Case #" << Case << ":";
        cin >> n >> a >> b >> c;
        if (a+b-c > n){
            cout << " IMPOSSIBLE";
            continue;
        }
        if (n == 1){
            cout << " " << 1;
            continue;
        }
        if (n == 2){
            if (a == 1 && b == 2) cout << " " << 2 << " " << 1;
            else if (a == 2 && b == 1) cout << " " << 1 << " " << 2;
            else if (a == 2 && b == 2 && c == 2) cout << " " << 1 << " " << 1;
            else cout << " IMPOSSIBLE";
            continue;
        }
        a -= c;
        b -= c;
        n -= a+b+c;
        if (n && c == 1 && !a && !b){
            cout << " IMPOSSIBLE";
            continue;
        }
        for (int i = 0; i < a; i++){
            cout << " " << 2;
        }
        if (n && c == 1 && a){
            for (int i = 0; i < n; i++){
                cout << " " << 1;
            }
        }
        for (int i = 0; i < c-1; i++){
            cout << " " << 3;
        }
        if (c > 1){
            for (int i = 0; i < n; i++){
                cout << " " << 1;
            }
        }
        cout << " " << 3;
        if (n && c == 1 && !a){
            for (int i = 0; i < n; i++){
                cout << " " << 1;
            }
        }
        for (int i = 0; i < b; i++){
            cout << " " << 2;
        }
    }
}

分享本文 Share with friends