Submission #249422

#TimeUsernameProblemLanguageResultExecution timeMemory
249422thebesparentrises (BOI18_parentrises)C++14
100 / 100
211 ms9348 KiB
#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)

parentrises.cpp: In function 'int main()':
parentrises.cpp:10:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&S);
     ~~~~~^~~~~~~~~
parentrises.cpp:12:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&T);
         ~~~~~^~~~~~~~~
parentrises.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&T);
         ~~~~~^~~~~~~~~
parentrises.cpp:72:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d",&x);
             ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...