Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created March 2, 2019 06:59
Show Gist options
  • Save fpdjsns/b17b9b4ea98ef265a87bf7cd42165576 to your computer and use it in GitHub Desktop.
Save fpdjsns/b17b9b4ea98ef265a87bf7cd42165576 to your computer and use it in GitHub Desktop.
[Round 1B 2017] Problem B. Stable Neigh-bors : https://code.google.com/codejam/contest/8294486/dashboard#s=p1&a=1
//
// Created by fpdjsns
// Copyright © 2019 fpdjsns. All rights reserved.
//
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
/*
* 시간복잡도 : O(N)
*/
/*
R = RED
O = ORANGE = RED + YELLOW => BOB
Y = YELLOW
G = GREEN = YELLOW + BLUE => RGR
B = BLUE
V = VIOLET = RED + BLUE => YVY
*/
string solve(int N, int R, int O, int Y, int G, int B, int V) {
R -= G; Y -= V; B -= O;
int cnt = (R + Y + B) / 2;
if (cnt < R || cnt < Y || cnt < B) return "IMPOSSIBLE";
if (R < 0|| Y < 0 || B < 0) return "IMPOSSIBLE";
string answer = "";
cnt = (R + Y + B + 1) / 2;
vector<pair<int, int>> tmp(3);
tmp[0] = { R, 'R' }; tmp[1] = { Y, 'Y' }; tmp[2] = { B,'B' };
sort(tmp.begin(), tmp.end());
int tmpInd = 0;
for (int i = 0; i < cnt; i++) {
while (tmp[tmpInd].first <= 0) tmpInd++;
answer += tmp[tmpInd].second;
tmp[tmpInd].first--;
switch (tmp[tmpInd].second) {
case 'R': R--; break;
case 'Y': Y--; break;
case 'B': B--; break;
}
}
int ind = 1;
while(0 < R + Y + B) {
while (tmp[tmpInd].first <= 0) tmpInd++;
tmp[tmpInd].first--;
switch (tmp[tmpInd].second) {
case 'R': answer.insert(ind, "R"); R--; break;
case 'Y': answer.insert(ind, "Y"); Y--; break;
case 'B': answer.insert(ind, "B"); B--; break;
}
ind += 2;
}
// mix
ind = 0;
while (0 < G + V + O) {
if (answer.size() == 0) {
if (0 < G) { answer += "GR"; G--; }
else if (0 < V) { answer += "VY"; V--; }
else { answer += "OB"; O--; }
ind++;
continue;
}
if (answer.size() <= ind)
return "IMPOSSIBLE";
if (answer[ind] == 'R') {
if (0 < G) {
answer.insert(ind + 1, "GR");
G--; ind ++;
}
}
else if (answer[ind] == 'Y') {
if (0 < V) {
answer.insert(ind + 1, "VY");
V--; ind ++;
}
}
else if (answer[ind] == 'B') {
if (0 < O) {
answer.insert(ind + 1, "OB");
O--; ind ++;
}
}
ind++;
}
return answer;
}
int main() {
int T;
cin >> T;
for (int c = 1; c <= T; c++) {
int N, R, O, Y, G, B, V;
cin >> N >> R >> O >> Y >> G >> B >> V;
string ans = solve(N, R, O, Y, G, B, V);
printf("Case #%d: ", c);
cout << ans << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment