답안 #249422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249422 2020-07-15T01:11:54 Z thebes parentrises (BOI18_parentrises) C++14
100 / 100
211 ms 9348 KB
#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

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);
             ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 25 ms 512 KB Output is correct
17 Correct 11 ms 1280 KB Output is correct
18 Correct 11 ms 768 KB Output is correct
19 Correct 12 ms 896 KB Output is correct
20 Correct 12 ms 1280 KB Output is correct
21 Correct 211 ms 2168 KB Output is correct
22 Correct 122 ms 9324 KB Output is correct
23 Correct 96 ms 2808 KB Output is correct
24 Correct 101 ms 5580 KB Output is correct
25 Correct 120 ms 9348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 3200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 3200 KB Output is correct
2 Correct 119 ms 3320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 3200 KB Output is correct
2 Correct 119 ms 3320 KB Output is correct
3 Correct 112 ms 3320 KB Output is correct