#include <bits/stdc++.h>
#define maxN 1000005
using namespace std;
int p,t,i,m,l,d,res[31];
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);
}
string resi(string s){
int i;
u.clear();
v.clear();
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()) ans="impossible";
return ans;
}
int main(){
std::ios_base::sync_with_stdio(false);
int a[30]={158331292,79165646,39237478,19865414,9932707,4906266,2496396,1248198,613440,314472,157236,76680,39768,19884,9578,5062,2531,1194,652,326,148,86,43,18,12,6,2,2,1,0};
cin>>p;
cin>>t;
if(p==1){
while(t--){
cin>>s;
cout<<resi(s)<<endl;
}
}
else{
int n;
while(t--){
cin>>n;
cout<<a[30-n]<<endl;
}
}
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 'std::__cxx11::string resi(std::__cxx11::string)':
parentrises.cpp:43:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<s.size();i++){
~^~~~~~~~~
parentrises.cpp:56:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<v.size();i++){
~^~~~~~~~~
parentrises.cpp:63:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<u.size();i++){
~^~~~~~~~~
parentrises.cpp:64: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 |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
18 ms |
512 KB |
Output is correct |
17 |
Correct |
7 ms |
1376 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
4 ms |
1104 KB |
Output is correct |
20 |
Correct |
5 ms |
1416 KB |
Output is correct |
21 |
Correct |
139 ms |
2236 KB |
Output is correct |
22 |
Correct |
31 ms |
10248 KB |
Output is correct |
23 |
Correct |
18 ms |
3028 KB |
Output is correct |
24 |
Correct |
28 ms |
5656 KB |
Output is correct |
25 |
Correct |
32 ms |
10248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |