# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
249422 | thebes | parentrises (BOI18_parentrises) | C++14 | 211 ms | 9348 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;
const int MN = 1e6+6, MM = 303, mod = 1e9+7;
int S, T, N, i, j, k, x, y, w[MN], cur, fl, ans[MM], dp[2][2*MM][2*MM];
string s; stack<int> go;
vector<pair<int,int>> qu;
int main(){
scanf("%d",&S);
if(S==1){
scanf("%d",&T);
while(T--){
cin >> s; N = (int)s.size();
for(i=0;i<N;i++) w[i]=1;
while(go.size()) go.pop();
cur = fl = 0;
for(i=0;i<N;i++){
if(s[i]=='('){
cur++;
go.push(i);
}
else if(cur) cur--;
else{
if(go.empty()){
fl=1;
break;
}
w[go.top()]=2;
go.pop();
}
}
while(go.size()) go.pop();
cur = 0;
for(i=N-1;i>=0;i--){
if(s[i]==')'){
cur++;
go.push(i);
}
else if(cur>=w[i]) cur-=w[i];
else{
int r = cur;
cur = 0;
while(r<w[i]){
if(go.empty()){
fl=1;
break;
}
r++;
w[go.top()]=2;
go.pop();
}
}
}
if(fl) printf("impossible\n");
else{
int hm[]={0,0};
for(i=0;i<N;i++){
if(w[i]==2) printf("G");
else{
int idx=s[i]=='('?0:1;
printf("%c","BR"[hm[idx]]), hm[idx]=!hm[idx];
}
}
printf("\n");
}
}
}
else{
scanf("%d",&T);
for(i=1;i<=T;i++){
scanf("%d",&x);
qu.push_back({x,i});
}
dp[0][0][0]=1;
for(i=1;i<MM;i++){
memset(dp[i&1],0,sizeof(dp[i&1]));
for(j=0;j<2*MM;j++){
for(k=0;k<2*MM;k++){
if(!dp[(i+1)&1][j][k]) continue;
dp[i&1][j+1][k+2]+=dp[(i+1)&1][j][k];
if(dp[i&1][j+1][k+2]>=mod) dp[i&1][j+1][k+2]-=mod;
int nj = max(0, j-2);
if(k){
dp[i&1][nj][k-1]+=dp[(i+1)&1][j][k];
if(dp[i&1][nj][k-1]>=mod) dp[i&1][nj][k-1]-=mod;
}
}
}
int sna = 0;
for(k=0;k<2*MM;k++){
sna += dp[i&1][0][k];
if(sna>=mod) sna-=mod;
}
for(auto v : qu){
if(v.first==i) ans[v.second]=sna;
}
}
for(i=1;i<=T;i++){
printf("%d\n",ans[i]);
}
}
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... |