Submission #96619

# Submission time Handle Problem Language Result Execution time Memory
96619 2019-02-10T13:09:39 Z josiftepe parentrises (BOI18_parentrises) C++14
100 / 100
239 ms 111492 KB
#include <bits/stdc++.h>

using namespace std;
int dp[305][305][305];
const int MOD = 1e9 + 7;
int rek(int at, int open, int closed){
    if(at == 0){
        if(closed == 0){
            return 1;
        }
        return 0;
    }
    if(dp[at][open][closed] != -1){
        return dp[at][open][closed];
    }
    int ret = 0;
    ret += rek(at - 1, open + 2, closed + 1);
    if(open > 0){
        ret += rek(at - 1, open - 1, max(closed - 2, 0));
    }
    return dp[at][open][closed] = ret % MOD;
}
void solve(string s){
    string ret = s;
    for(int i = 0; i < s.size(); i ++){
        ret[i] = '?';
    }
    stack<int> st;
    int cnt = 0;
    for(int i = 0; i < (int)s.size(); i ++){
        if(s[i] == '('){
            ++cnt;
            ret[i] = 'G';
        }
        else{
            if(cnt == 0){
                if(st.empty()) {
                    cout << "impossible\n";
                    return;
                }
                ret[st.top()] = 'R';
                ret[i] = 'B';
                st.pop();
            }
            else{
                cnt --;
                ret[i] = 'G';
                st.push(i);
            }
        }
    }
    stack<int> st2;
    cnt = 0;
    for(int i = (int)s.size() - 1; i >= 0; i --){
        if(s[i] == ')'){
            ++cnt;
        }
        else{
            if(cnt == 0){
                if(st2.empty()) {
                    cout << "impossible\n";
                    return;
                }
                ret[st2.top()] = 'B';
                ret[i] = 'R';
                st2.pop();
            }
            else{
                cnt --;
                st2.push(i);
            }
        }

    }
    cout << ret << endl;

}
int main()
{
    ios_base::sync_with_stdio(false);
    int t_case;
    cin >> t_case;
    if(t_case == 1){
        int t;
        cin >> t;
        string s;
        while(t --){
            cin >> s;
            solve(s);
        }

    }
    else if(t_case == 2){
        int t;
        cin >> t;
        memset(dp, -1, sizeof dp);
        while(t --){
            int n;
            cin >> n;
            cout << rek(n, 0, 0) << endl;
        }
    }
    return 0;
}

Compilation message

parentrises.cpp: In function 'void solve(std::__cxx11::string)':
parentrises.cpp:25:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < s.size(); i ++){
                    ~~^~~~~~~~~~
# 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 420 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 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 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 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 380 KB Output is correct
5 Correct 2 ms 376 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 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 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 380 KB Output is correct
5 Correct 2 ms 376 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 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 16 ms 556 KB Output is correct
17 Correct 5 ms 1272 KB Output is correct
18 Correct 3 ms 632 KB Output is correct
19 Correct 4 ms 760 KB Output is correct
20 Correct 4 ms 1272 KB Output is correct
21 Correct 107 ms 2140 KB Output is correct
22 Correct 25 ms 9360 KB Output is correct
23 Correct 13 ms 2668 KB Output is correct
24 Correct 22 ms 5124 KB Output is correct
25 Correct 25 ms 9360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 111492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 111492 KB Output is correct
2 Correct 92 ms 111352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 111492 KB Output is correct
2 Correct 92 ms 111352 KB Output is correct
3 Correct 239 ms 111352 KB Output is correct