https://atcoder.jp/contests/abc166/tasks/abc166_f
F - Three Variables Game
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
atcoder.jp
세개의 숫자 A, B, C와 N개의 이벤트(AB, AC, BC중에 하나)들이 주어진다. 각 주어진 이벤트의 두개 알파벳중에 하나에 1을 더해주면 다른 하나는 1을 빼주면 된다. N번의 이벤트들을 실행하는중에 A, B, C 중에 하나가 음수가 된다면 실패이고, N번 모두 성공하면 각각의 이벤트마다 1더해준 알파벳들을 출력하면 되는 문제이다.
조심해야할 부분이 있는데, 예를 들어 이벤트 AB가 주어졌고, 현재 숫자들이 A = 1, B = 1, C = 0일때이다. 두 숫자가 같으니까 A 혹은 B 아무데나 1을 더해줘도 된다고 생각할수도 있으나, 다음 이벤트가 무엇이냐에 따라 어디에 1을 더해주는지가 중요해진다. 예를 들어 다음 이벤트가 AC라고 하자. A에서 1을 빼고 B에 1을 더해준다면 A = 0, B = 2, C = 0이 되고 이벤트 AC는 실행할수 없으므로 실패이다. 하지만 반대로 A에 1을 더해주고 B에서 1을 뺀다면 A = 2, B = 0, C = 0이되고 이벤트 AC는 실행할수 있게 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c;
string s[100001];
char ans[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> a >> b >> c;
for(int i = 0; i < n; i++) cin >> s[i];
for(int i = 0; i < n; i++) {
if(s[i] == "AB") {
if(a == 0 && b == 0) {
cout << "No" << '\n';
return 0;
}
else if(a == 1 && b == 1 && c == 0 && i != n && s[i + 1] == "AC") {
b--;
a++;
ans[i] = 'A';
}
else if(a == 1 && b == 1 && c == 0 && i != n && s[i + 1] == "BC") {
a--;
b++;
ans[i] = 'B';
}
else if(a > b) {
a--;
b++;
ans[i] = 'B';
}
else {
a++;
b--;
ans[i] = 'A';
}
}
if(s[i] == "AC") {
if(a == 0 && c == 0) {
cout << "No" << '\n';
return 0;
}
else if(a == 1 && c == 1 && b == 0 && i != n && s[i + 1] == "AB") {
c--;
a++;
ans[i] = 'A';
}
else if(a == 1 && c == 1 && b == 0 && i != n && s[i + 1] == "BC") {
a--;
c++;
ans[i] = 'C';
}
else if(a > c) {
a--;
c++;
ans[i] = 'C';
}
else {
a++;
c--;
ans[i] = 'A';
}
}
if(s[i] == "BC") {
if(b == 0 && c == 0) {
cout << "No" << '\n';
return 0;
}
else if(b == 1 && c == 1 && a == 0 && i != n && s[i + 1] == "AB") {
c--;
b++;
ans[i] = 'B';
}
else if(b == 1 && c == 1 && a == 0 && i != n && s[i + 1] == "AC") {
b--;
c++;
ans[i] = 'C';
}
else if(b > c) {
b--;
c++;
ans[i] = 'C';
}
else {
b++;
c--;
ans[i] = 'B';
}
}
}
cout << "Yes" << '\n';
for(int i = 0; i < n; i++)
cout << ans[i] << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'AtCoder > ABC' 카테고리의 다른 글
166E This Message Will Self-Destruct in 5s (0) | 2020.05.04 |
---|