# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
263613 | TadijaSebez | parentrises (BOI18_parentrises) | C++11 | 270 ms | 223736 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 N=305;
const int mod=1e9+7;
int dp[N][N*2][N*2];
const int H=1000050;
char s[H],ans[H];
int main(){
int t;
scanf("%i",&t);
if(t==1){
int q;
scanf("%i",&q);
while(q--){
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++)ans[i]='B';
int cnt=0;
for(int i=1,bal=0;i<=n;i++){
if(s[i]=='(')bal++;
else bal--;
if(bal<0){
ans[i]='R';
cnt++;
bal=0;
}
}
for(int i=1;i<=n;i++){
if(cnt&&s[i]=='('){
ans[i]='G';
cnt--;
}
}
cnt=0;
for(int i=n,bal=0;i>=1;i--){
if(s[i]==')')bal++;
else bal--;
if(bal<0){
ans[i]='R';
cnt++;
bal=0;
}
}
for(int i=n;i>=1;i--){
if(cnt&&s[i]==')'){
ans[i]='G';
cnt--;
}
}
bool ok=1;
int b1=0,b2=0;
for(int i=1;i<=n;i++){
if(s[i]=='('){
if(ans[i]=='G')b1++,b2++;
else if(ans[i]=='B')b1++;
else b2++;
}
else{
if(ans[i]=='G')b1--,b2--;
else if(ans[i]=='B')b1--;
else b2--;
}
if(b1<0||b2<0)ok=0;
}
if(b1!=0||b2!=0)ok=0;
if(ok){
ans[n+1]=0;
printf("%s\n",ans+1);
}else printf("impossible\n");
}
}else{
dp[0][0][0]=1;
for(int i=0;i<N-1;i++){
for(int j=0;j<=2*i;j++){
for(int k=0;k<=2*i;k++){
dp[i+1][j+2][k+1]+=dp[i][j][k];
if(dp[i+1][j+2][k+1]>=mod)dp[i+1][j+2][k+1]-=mod;
if(j>0){
dp[i+1][j-1][max(k-2,0)]+=dp[i][j][k];
if(dp[i+1][j-1][max(k-2,0)]>=mod)dp[i+1][j-1][max(k-2,0)]-=mod;
}
}
}
}
int q;
scanf("%i",&q);
while(q--){
int n,ans=0;
scanf("%i",&n);
for(int j=0;j<=n*2;j++){
ans+=dp[n][j][0];
if(ans>=mod)ans-=mod;
}
printf("%i\n",ans);
}
}
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... |