#include <bits/stdc++.h>
#define maxN 1000005
using namespace std;
int p,t,i,m,l,d;
string s,ans;
vector <int> v,u;
bool moze(int m){
int x;
x=m+v.size()-u.size();
if(x<=0) return true;
if(x>u.size() || v[m-1]>u[u.size()-x]) return false;
return true;
}
bool proveri(){
int r,b,i;
r=b=0;
for(i=0;i<s.size();i++){
if(s[i]=='('){
if(ans[i]=='G') {r++; b++;}
if(ans[i]=='R') r++;
if(ans[i]=='B') b++;
}
else{
if(ans[i]=='G') {r--; b--;}
if(ans[i]=='R') r--;
if(ans[i]=='B') b--;
if(r<0 || b<0) return false;
}
}
return (r==0 && b==0);
}
int main(){
std::ios_base::sync_with_stdio(false);
cin>>t;
cin>>t;
while(t--){
cin>>s;
for(i=0;i<s.size();i++){
if(s[i]=='(') v.push_back(i);
else u.push_back(i);
}
l=0;
d=v.size();
while(d>l){
m=(l+d)/2+1;
if(moze(m)) l=m;
else d=m-1;
}
ans=s;
d=l+v.size()-u.size();
for(i=0;i<v.size();i++){
if(i<l) ans[v[i]]='G';
else{
if((i-l)%2) ans[v[i]]='R';
else ans[v[i]]='B';
}
}
for(i=0;i<u.size();i++){
if(i>=u.size()-d) ans[u[i]]='G';
else{
if(i%2) ans[u[i]]='R';
else ans[u[i]]='B';
}
}
if(proveri()) cout<<ans<<endl;
else cout<<"impossible"<<endl;
v.clear();
u.clear();
}
return 0;
}
Compilation message
parentrises.cpp: In function 'bool moze(int)':
parentrises.cpp:16:5: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(x>u.size() || v[m-1]>u[u.size()-x]) return false;
~^~~~~~~~~
parentrises.cpp: In function 'bool proveri()':
parentrises.cpp:23:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<s.size();i++){
~^~~~~~~~~
parentrises.cpp: In function 'int main()':
parentrises.cpp:45:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<s.size();i++){
~^~~~~~~~~
parentrises.cpp:58:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<v.size();i++){
~^~~~~~~~~
parentrises.cpp:65:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<u.size();i++){
~^~~~~~~~~
parentrises.cpp:66:5: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i>=u.size()-d) ans[u[i]]='G';
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
15 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
1276 KB |
Output is correct |
18 |
Correct |
3 ms |
632 KB |
Output is correct |
19 |
Correct |
4 ms |
1016 KB |
Output is correct |
20 |
Correct |
4 ms |
1332 KB |
Output is correct |
21 |
Correct |
112 ms |
2044 KB |
Output is correct |
22 |
Correct |
27 ms |
8192 KB |
Output is correct |
23 |
Correct |
15 ms |
2908 KB |
Output is correct |
24 |
Correct |
22 ms |
5012 KB |
Output is correct |
25 |
Correct |
25 ms |
8200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Expected integer, but "impossible" found |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Expected integer, but "impossible" found |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Expected integer, but "impossible" found |