답안 #91029

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91029 2018-12-25T16:31:22 Z choikiwon parentrises (BOI18_parentrises) C++17
100 / 100
267 ms 145232 KB
#include<bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

string S, ans;

void main2() {
    cin >> S;

    int cnt = 0, open = 0;
    int mx1 = 0;
    int mx2 = 0;
    for(int i = 0; i < S.size(); i++) {
        if(S[i] == '(') cnt++, open++;
        else cnt--;
        mx1 = max(mx1, -cnt);
        if(-cnt > open) {
            cout << "impossible\n";
            return;
        }
    }
    cnt = open = 0;
    for(int i = (int)S.size() - 1; i >= 0; i--) {
        if(S[i] == ')') cnt++, open++;
        else cnt--;
        mx2 = max(mx2, -cnt);
        if(-cnt > open) {
            cout << "impossible\n";
            return;
        }
    }

    ans.clear();
    ans.resize(S.size());
    for(int i = 0; i < S.size(); i++) {
        if(mx1 && S[i] == '(') ans[i] = 'G', mx1--;
    }
    for(int i = (int)S.size() - 1; i >= 0; i--) {
        if(mx2 && S[i] == ')') ans[i] = 'G', mx2--;
    }

    int rg = 0;
    int bg = 0;
    for(int i = 0; i < S.size(); i++) {
        if(ans[i] == 'G') {
            if(S[i] == '(') rg++, bg++;
            else rg--, bg--;
        }
        else {
           if(S[i] == '(') {
                if(rg < bg) ans[i] = 'R', rg++;
                else ans[i] = 'B', bg++;
            }
            else {
                if(rg < bg) ans[i] = 'B', bg--;
                else ans[i] = 'R', rg--;
            }
        }
    }

    cout << ans << endl;
}

int cc[333][333][333];
int dp(int n, int open, int close) {
    int &ret = cc[n][open][close];
    if(ret != -1) return ret;
    if(n == 0) return ret = close == 0;

    ret = 0;
    ret += dp(n - 1, open + 2, close + 1);
    ret %= mod;
    if(open) {
        ret += dp(n - 1, open - 1, max(0, close - 2));
        ret %= mod;
    }
    return ret;
}

void main3() {
    int n; cin >> n;
    cout << dp(n, 0, 0) << endl;
}

int P, TC;
int main() {
    std::ios::sync_with_stdio(false);

    cin >> P >> TC;
    if(P == 1) {
        while(TC--) main2();
    }
    if(P == 2) {
        memset(cc, -1, sizeof(cc));
        while(TC--) main3();
    }
}

Compilation message

parentrises.cpp: In function 'void main2()':
parentrises.cpp:14:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < S.size(); i++) {
                    ~~^~~~~~~~~~
parentrises.cpp:36:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < S.size(); i++) {
                    ~~^~~~~~~~~~
parentrises.cpp:45:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < S.size(); i++) {
                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 408 KB Output is correct
4 Correct 2 ms 408 KB Output is correct
5 Correct 2 ms 408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 620 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 620 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 2 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 624 KB Output is correct
15 Correct 2 ms 640 KB Output is correct
16 Correct 13 ms 784 KB Output is correct
17 Correct 4 ms 940 KB Output is correct
18 Correct 3 ms 940 KB Output is correct
19 Correct 3 ms 940 KB Output is correct
20 Correct 4 ms 940 KB Output is correct
21 Correct 99 ms 1324 KB Output is correct
22 Correct 23 ms 3656 KB Output is correct
23 Correct 13 ms 3656 KB Output is correct
24 Correct 18 ms 3656 KB Output is correct
25 Correct 23 ms 3656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 145168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 145168 KB Output is correct
2 Correct 117 ms 145228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 145168 KB Output is correct
2 Correct 117 ms 145228 KB Output is correct
3 Correct 267 ms 145232 KB Output is correct