結果

問題 No.2031 Colored Brackets
ユーザー dyktr_06dyktr_06
提出日時 2022-08-06 00:01:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 2,600 bytes
コンパイル時間 5,614 ms
コンパイル使用メモリ 234,164 KB
実行使用メモリ 38,932 KB
最終ジャッジ日時 2023-10-14 02:15:57
合計ジャッジ時間 8,821 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 18 ms
13,928 KB
testcase_05 AC 8 ms
7,552 KB
testcase_06 AC 11 ms
9,868 KB
testcase_07 AC 1 ms
4,356 KB
testcase_08 AC 30 ms
21,768 KB
testcase_09 AC 41 ms
27,684 KB
testcase_10 AC 53 ms
34,804 KB
testcase_11 AC 40 ms
26,716 KB
testcase_12 AC 62 ms
37,632 KB
testcase_13 AC 28 ms
19,776 KB
testcase_14 AC 64 ms
38,752 KB
testcase_15 AC 64 ms
38,800 KB
testcase_16 AC 63 ms
38,784 KB
testcase_17 AC 190 ms
38,932 KB
testcase_18 AC 38 ms
38,504 KB
testcase_19 AC 30 ms
31,364 KB
testcase_20 AC 19 ms
20,764 KB
testcase_21 AC 21 ms
21,848 KB
testcase_22 AC 22 ms
23,676 KB
testcase_23 AC 35 ms
36,108 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 3 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())
#define out(a) cout << a << "\n"

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef vector<string> vst;
typedef vector<double> vd;
typedef pair<long long, long long> P;

const long long INF = 0x1fffffffffffffff;
const long long MOD = 998244353;
const double PI = acos(-1);
 
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T, class U> inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; }
template<class T> inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; }

ll n;
string s;

void input(){
    cin >> n >> s;
}
 
void solve(){
    vector<vector<ll>> dp(n / 2 + 1, vector<ll>(n / 2 + 1));
    vector<vector<ll>> tmp(n / 2 + 1, vector<ll>(n / 2 + 1));
    ll l = 0, r = 0, currdiff = 0, prevdiff = 0;
    dp[0][0] = 1;
    vector<P> currP, prevP;
    rep(i, n){
        if(s[i] == '('){
            l++;
        }else{
            r++;
        }
        currdiff = l - r;
        for(int j = 0; j <= n / 2; j++){
            ll jj = j;
            if(s[i] == '('){
                jj++;
            }else{
                jj--;
            }
            if(jj < 0) continue;
            ll k1 = currdiff - jj, k2 = prevdiff - j;
            if(k1 < 0 || k2 < 0) continue;
            tmp[jj][k1] += dp[j][k2];
            tmp[jj][k1] %= MOD; 
            tmp[k1][jj] += dp[k2][j];
            tmp[k1][jj] %= MOD; 
            currP.push_back({jj, k1});
        }
        for(auto x : prevP){
            dp[x.first][x.second] = 0;
            dp[x.second][x.first] = 0;
        }
        for(auto x : currP){
            dp[x.first][x.second] = tmp[x.first][x.second];
            dp[x.second][x.first] = tmp[x.second][x.first];
        }
        for(auto x : currP){
            tmp[x.first][x.second] = 0;
            tmp[x.second][x.first] = 0;
        }
        prevP = currP;
        currP.clear();
        prevdiff = currdiff;
    }
    out(dp[0][0]);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    input();
    solve();
}
0