# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
65171 | spencercompton | parentrises (BOI18_parentrises) | C++14 | 216 ms | 11540 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>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int p, cases;
cin >> p >> cases;
if(p==1){
for(int z = 1; z<=cases; z++){
string s;
cin >> s;
bool pos = true;
int open = 0;
int close = 0;
for(int i = 0; i<s.length(); i++){
if(s[i]=='('){
open++;
}
else{
close++;
}
if(close>2*open){
pos = false;
}
}
open = 0;
close = 0;
for(int i = s.length()-1; i>=0; i--){
if(s[i]=='('){
open++;
}
else{
close++;
}
if(open > 2*close){
pos = false;
}
}
int n = (int)s.length();
int color[n];
//G = 0, B = 1, R = 2
for(int i = 0; i<n; i++){
color[i] = 0;
}
open = 0;
close = 0;
vector<int> op;
vector<int> cl;
bool ok = true;
vector<int> op2;
vector<int> cl2;
int point = 0;
for(int i = 0; i<n; i++){
if(s[i]=='('){
op.push_back(i);
open++;
}
else{
cl.push_back(i);
close++;
if(close>open){
if(point+2 > cl.size()){
ok = false;
break;
}
cl2.push_back(cl[point++]);
cl2.push_back(cl[point++]);
close--;
}
}
}
int dif = open-close;
if(!ok || 2*dif>open){
assert(!pos);
cout << "impossible" << endl;
continue;
}
for(int i = 0; i<2*dif; i++){
op2.push_back(op.back());
op.pop_back();
}
assert(op2.size()%2==0 && cl2.size()%2==0);
for(int i = 0; i<op2.size(); i+=2){
color[op2[i]] = 1;
color[op2[i+1]] = 2;
}
for(int i = 0; i<cl2.size(); i+=2){
color[cl2[i]] = 1;
color[cl2[i+1]] = 2;
}
int bal1 = 0;
int bal2 = 0;
for(int i = 0; i<n; i++){
if(color[i]==0 || color[i]==1){
if(s[i]=='('){
bal1++;
}
else{
bal1--;
}
}
if(color[i]==0 || color[i]==2){
if(s[i]=='('){
bal2++;
}
else{
bal2--;
}
}
if(bal1<0 || bal2<0){
ok = false;
}
}
ok &= bal1==0 && bal2==0;
if(!ok){
assert(!pos);
cout << "impossible" << endl;
continue;
}
assert(ok);
for(int i = 0; i<n; i++){
if(color[i]==0){
cout << "G";
}
else if(color[i]==1){
cout << "B";
}
else{
cout << "R";
}
}
cout << endl;
}
}
else{
assert(false);
}
}
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... |