# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
98567 | tdwn | parentrises (BOI18_parentrises) | C++17 | 3 ms | 412 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
string code;
string answ;
string fin;
void solvea() {
cin>>code;
answ = code;
stack<int>p1, p2;
for(int i=0;i<code.length();i++) {
if(code[i] == '(') {
p1.push(i);
}
else {
if(p1.size() != 0) {
answ[i] = 'B';
p2.push(p1.top());
answ[p1.top()] = 'B';
p1.pop();
}
else if(p2.size() != 0) {
answ[p2.top()] = 'G';
answ[i] = 'R';
p2.pop();
}
else {
cout<<"Impossible\n";
return;
}
}
}
while(p1.size() > 0) p1.pop();
while(p2.size() > 0) p2.pop();
for(int i=code.length()-1;i>=0;i--) {
if(code[i] == ')') {
p1.push(i);
}
else if(code[i] == '(' && answ[i] == '(') {
if(p1.size() != 0) {
if(answ[p1.top()] == 'R') answ[i] = 'B';
else answ[i] = 'R';
answ[p1.top()] = 'G';
p1.pop();
}
else {
cout<<"Impossible\n";
return;
}
}
}
int sb = 0, sr = 0;
for(int i=0;i<code.length();i++) {
if(answ[i] != 'G' && answ[i] != 'B' && answ[i] != 'R') {
cout<<"Impossible\n";
return;
}
if(code[i] == '(') {
if(answ[i] == 'B') sb++;
else if(answ[i] == 'R') sr++;
else {
sb++; sr++;
}
}
else {
if(answ[i] == 'B') sb--;
else if(answ[i] == 'R') sr--;
else {
sb--; sr--;
}
}
if(sb < 0 || sr < 0) {
cout<<"Impossible\n";
return;
}
}
if(sb != 0 || sr != 0) {
cout<<"Impossible\n";
return;
}
cout<<answ<<"\n";
return;
}
void solveb() {
cout<<"0\n";
}
int main() {
int P, T;
cin>>P;
cin>>T;
while(T--) {
if(P == 1) solvea();
else solveb();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |