Submission #578970

# Submission time Handle Problem Language Result Execution time Memory
578970 2022-06-18T08:57:40 Z FatihSolak parentrises (BOI18_parentrises) C++17
100 / 100
631 ms 7428 KB
#include <bits/stdc++.h>
#define N 305
using namespace std;
const int mod = 1e9 + 7;
int val[N];
int val2[N];
int ans[N];
void solve1(){
    string s;
    cin >> s;
    int n = s.size();
    vector<int> r,rr,points;
    string ans = "";
    int a = 0,b = 0;
    for(int i = 0;i<n;i++){
        ans += ".";
        if(s[i] == '(')a++;
        else b++;
        if(2*a < b){
            cout << "impossible" << endl;
            return;
        }
        if(s[i]  == '('){
            ans[i] = 'R';
            r.push_back(i);
            rr.push_back(i);
        }
        if(s[i] == ')'){
            if(rr.empty()){
                ans[r.back()] = 'G';
                ans[i] = 'B';
                r.pop_back();
                continue;
            }
            rr.pop_back();
            points.push_back(i);
            ans[i] = 'R';
        }
    }
    if(2*b < a){
        cout << "impossible" << endl;
        return;
    }
    for(int i = 0;i<rr.size();i++){
        ans[rr[i]] = 'B';
        ans[points[points.size() - rr.size() + i]] = 'G';
        if(rr[i] > points[points.size() - rr.size() + i]){
            cout << "impossible" << endl;
            return;
        }
    }
    cout << ans << endl;
}
void solve2(){
    int n;
    cin >> n;
    cout << (ans[n] + val[n])%mod  << endl;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef Local
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    for(int i = 0;i*3<N;i++){
        int a = i,b = 2*i;
        vector<vector<int>> dp(N,vector<int>(N,0));
        dp[0][0] = 1;
        for(int i = 0;i<N;i++){
            for(int j = 0;j<N;j++){
                bool ok = 1;
                if(2*(i+1) < j)
                    ok = 0;
                if(i + 1 > a)
                    ok = 0;
                if(ok){
                    dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod;
                }
                ok = 1;
                if(2*i < j+1)
                    ok = 0;
                if(j + 1 > b)
                    ok = 0;
                if(ok){
                    dp[i][j+1] = (dp[i][j+1] + dp[i][j])%mod;
                }
            }
        }
        //cout << dp[a][b] << endl;
        val[3*i] = dp[a][b];
    }
    //for(int a = 1;a<N;a++){
    //    for(int b = 1;b<2*a && b + a < N;b++){
    for(int num = -(N-1);num < 2*(N-1);num++){
        vector<vector<int>> dp(N,vector<int>(N,0));
        dp[0][1] = 1;
        for(int i = 0;i<N;i++){
            for(int j = 1;j<N;j++){
                bool ok = 1;
                if(i == 2*j)
                    ok = 0;
                if(num <= 2*i-j)
                    ok = 0;
                if(i + 1 == N)
                    ok = 0;
                if(ok){
                    dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod;
                }
                ok = 1;
                if(num <= 2*i-j)
                    ok = 0;
                if(j + 1 == N)
                    ok = 0;
                if(ok){
                    dp[i][j+1] = (dp[i][j+1] + dp[i][j])%mod;
                }

            }
        }
        for(int i = 0;i<N;i++){
            for(int j = 0;j<N;j++){
                if(2*i - j == num && i+j < N && 2*i > j && 2*j >= i){
                    val2[i+j] = (val2[i+j] + dp[i][j])%mod;
                }
            }
        }
    }
    for(int i = 1;i<N;i++){
        for(int j = 0;j<N;j++){
            ans[i+j] = (ans[i+j] + (long long)val2[i]*val[j])%mod;
        }
    }
    int type;
    cin >> type;
    int t=1;
    cin>>t;
    while(t--){
        if(type == 1)
            solve1();
        if(type ==2)
            solve2();
    }
    #ifdef Local
    cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}

Compilation message

parentrises.cpp: In function 'void solve1()':
parentrises.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int i = 0;i<rr.size();i++){
      |                   ~^~~~~~~~~~
parentrises.cpp: In function 'int32_t main()':
parentrises.cpp:131:32: warning: iteration 304 invokes undefined behavior [-Waggressive-loop-optimizations]
  131 |             ans[i+j] = (ans[i+j] + (long long)val2[i]*val[j])%mod;
      |                         ~~~~~~~^
parentrises.cpp:130:24: note: within this loop
  130 |         for(int j = 0;j<N;j++){
      |                        ^
# Verdict Execution time Memory Grader output
1 Correct 506 ms 724 KB Output is correct
2 Correct 546 ms 740 KB Output is correct
3 Correct 499 ms 812 KB Output is correct
4 Correct 499 ms 688 KB Output is correct
5 Correct 513 ms 1072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 524 ms 924 KB Output is correct
2 Correct 509 ms 692 KB Output is correct
3 Correct 512 ms 800 KB Output is correct
4 Correct 503 ms 692 KB Output is correct
5 Correct 514 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 524 ms 924 KB Output is correct
2 Correct 509 ms 692 KB Output is correct
3 Correct 512 ms 800 KB Output is correct
4 Correct 503 ms 692 KB Output is correct
5 Correct 514 ms 720 KB Output is correct
6 Correct 524 ms 852 KB Output is correct
7 Correct 527 ms 1180 KB Output is correct
8 Correct 521 ms 708 KB Output is correct
9 Correct 514 ms 800 KB Output is correct
10 Correct 516 ms 692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 524 ms 924 KB Output is correct
2 Correct 509 ms 692 KB Output is correct
3 Correct 512 ms 800 KB Output is correct
4 Correct 503 ms 692 KB Output is correct
5 Correct 514 ms 720 KB Output is correct
6 Correct 524 ms 852 KB Output is correct
7 Correct 527 ms 1180 KB Output is correct
8 Correct 521 ms 708 KB Output is correct
9 Correct 514 ms 800 KB Output is correct
10 Correct 516 ms 692 KB Output is correct
11 Correct 540 ms 692 KB Output is correct
12 Correct 539 ms 904 KB Output is correct
13 Correct 508 ms 740 KB Output is correct
14 Correct 540 ms 692 KB Output is correct
15 Correct 529 ms 692 KB Output is correct
16 Correct 539 ms 768 KB Output is correct
17 Correct 531 ms 1208 KB Output is correct
18 Correct 548 ms 1136 KB Output is correct
19 Correct 553 ms 840 KB Output is correct
20 Correct 520 ms 1388 KB Output is correct
21 Correct 631 ms 1384 KB Output is correct
22 Correct 571 ms 7316 KB Output is correct
23 Correct 538 ms 2188 KB Output is correct
24 Correct 569 ms 4004 KB Output is correct
25 Correct 570 ms 7428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 527 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 527 ms 808 KB Output is correct
2 Correct 525 ms 788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 527 ms 808 KB Output is correct
2 Correct 525 ms 788 KB Output is correct
3 Correct 516 ms 692 KB Output is correct