#include <bits/stdc++.h>
using namespace std;
bool check(string s, string a) {
int b0 = 0, b1 = 0;
for (int i = 0; i < s.size(); i++) {
if (a[i] != 'B')
b0 += (s[i] == '(') ? +1 : -1;
if (a[i] != 'R')
b1 += (s[i] == '(') ? +1 : -1;
if (b0 < 0 || b1 < 0)
return 0;
}
return b0 == 0 && b1 == 0;
}
void solve1() {
string s;
cin >> s;
int n = s.size();
string a(n, 'G');
int b0 = 0, b1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
a[i] = 'G';
b0++, b1++;
} else {
if (b0 >= b1) {
a[i] = 'R';
b0--;
} else {
a[i] = 'B';
b1--;
}
}
if (b0 < 0 || b1 < 0) {
cout << "impossible\n";
return;
}
}
for (int i = n - 1; i >= 0 && (b0 > 0 || b1 > 0); i--) {
if (s[i] == '(') {
if (b0 >= b1)
a[i] = 'B', b0--;
else
a[i] = 'R', b1--;
} else {
if (b0 > 0 && a[i] == 'B')
a[i] = 'G', b0--;
else if (b1 > 0 && a[i] == 'R')
a[i] = 'G', b1--;
}
assert(b0 >= 0 && b1 >= 0);
}
if (check(s, a))
cout << a << "\n";
else
cout << "impossible\n";
}
void solve2() {
vector<int> a({0,
0,
1,
2,
2,
6,
12,
18,
43,
86,
148,
326,
652,
1194,
2531,
5062,
9578,
19884,
39768,
76680,
157236,
314472,
613440,
1248198,
2496396,
4906266,
9932707,
19865414,
39237478,
79165646,
158331292});
int i;
cin >> i;
cout << a[i] << "\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int p, t;
cin >> p >> t;
while (t--) {
if (p == 1)
solve1();
else
solve2();
}
return 0;
}
Compilation message
parentrises.cpp: In function 'bool check(std::string, std::string)':
parentrises.cpp:6:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
784 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
712 KB |
Output is correct |
20 |
Correct |
2 ms |
784 KB |
Output is correct |
21 |
Correct |
20 ms |
1144 KB |
Output is correct |
22 |
Correct |
24 ms |
4512 KB |
Output is correct |
23 |
Correct |
12 ms |
1536 KB |
Output is correct |
24 |
Correct |
17 ms |
2472 KB |
Output is correct |
25 |
Correct |
21 ms |
4456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |