Submission #145715

# Submission time Handle Problem Language Result Execution time Memory
145715 2019-08-20T20:40:30 Z MKopchev parentrises (BOI18_parentrises) C++14
100 / 100
351 ms 216040 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e6+42,mod=1e9+7,LIM=302;
char output[nmax];
stack<int> idle;

int dp[LIM][LIM][LIM][2];
int rec(int pos,int all_open_brackets,int open_now,int mode)
{
    if(mode==0)
    {
        if(pos==0)return open_now==0;
        if(dp[pos][all_open_brackets][open_now][mode]!=-1)return dp[pos][all_open_brackets][open_now][mode];

        int ret=rec(pos-1,all_open_brackets+1,open_now+1,mode);//add '('
        //add ')'
        if(open_now)
            ret=ret+rec(pos-1,all_open_brackets,open_now-1,mode);
        else if(all_open_brackets)
        {
            //remain in mode 0
            ret=(ret+rec(pos-1,all_open_brackets-1,open_now,mode))%mod;
            //go to mode 1
            ret=(ret+rec(pos-1,0,0,1))%mod;
        }
        ret=ret%mod;
        dp[pos][all_open_brackets][open_now][mode]=ret;
        return ret;
    }
    //mode=1
    if(pos==0)return all_open_brackets==0;
    if(dp[pos][all_open_brackets][open_now][mode]!=-1)return dp[pos][all_open_brackets][open_now][mode];
    int ret=0;
    //add '('
    ret=ret+rec(pos-1,all_open_brackets+1,open_now+1,1);
    //add ')'
    if(open_now)
    {
        ret=ret+rec(pos-1,max(all_open_brackets-2,0),open_now-1,1);
    }
    ret=ret%mod;
    dp[pos][all_open_brackets][open_now][mode]=ret;
    return ret;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();

    int p,t;
    cin>>p>>t;

    if(p==1)
    {
        string s;
        for(int i=1;i<=t;i++)
        {
            cin>>s;
            int SZ=s.size();
            for(int j=0;j<SZ;j++)
                output[j]=-1;

            stack<int> open=idle;
            for(int j=0;j<SZ;j++)
            {
                if(s[j]=='(')open.push(j);
                else
                {
                    if(open.size())
                    {
                        output[open.top()]='R';
                        output[j]='R';
                        open.pop();
                    }
                }
            }

            bool ok=1;

            open=idle;
            for(int j=0;j<SZ;j++)
            {
                if(s[j]=='(')open.push(j);
                else
                {
                    if(output[j]==-1)
                    {
                        if(open.size()==0)ok=0;
                        else
                        {
                            output[j]='B';
                            output[open.top()]='G';
                            open.pop();
                        }
                    }
                }
            }

            open=idle;
            for(int j=SZ-1;j>=0;j--)
            {
                if(s[j]==')')open.push(j);
                else
                {
                    if(output[j]==-1)
                    {
                        if(open.size()==0)ok=0;
                        else
                        {
                            output[j]='B';
                            output[open.top()]='G';
                            open.pop();
                        }
                    }
                }
            }

            if(ok)
            {
                for(int j=0;j<SZ;j++)cout<<output[j];
                cout<<endl;
            }
            else cout<<"impossible"<<endl;
        }
    }
    else
    {
        memset(dp,-1,sizeof(dp));
        int n;
        for(int i=1;i<=t;i++)
        {
            cin>>n;
            cout<<rec(n,0,0,0)<<endl;
        }
    }
    return 0;
}
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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 372 KB Output is correct
# Verdict Execution time Memory 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 372 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 428 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory 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 372 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 428 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 4 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 380 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 32 ms 504 KB Output is correct
17 Correct 8 ms 888 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 8 ms 760 KB Output is correct
20 Correct 9 ms 888 KB Output is correct
21 Correct 236 ms 2168 KB Output is correct
22 Correct 69 ms 6416 KB Output is correct
23 Correct 47 ms 2488 KB Output is correct
24 Correct 56 ms 3692 KB Output is correct
25 Correct 64 ms 6292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 215928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 215928 KB Output is correct
2 Correct 177 ms 215932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 215928 KB Output is correct
2 Correct 177 ms 215932 KB Output is correct
3 Correct 351 ms 216040 KB Output is correct