1 solutions

  • -1
    @ 2024-5-2 19:07:14
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll n, k, l, r, delta, ans, L, R, res, p[100005], dp[100005], s[100005], sum[100005], ps[100005];
    string str;
    stack<int> stk;
    void add(int pos) {
        if(str[pos] == '(') {
            if(p[pos] <= R) {
                if(str[p[pos] + 1] == '(' && p[p[pos] + 1] <= R) {
                    sum[pos] = sum[p[pos] + 1] + 1;
                    ps[pos] = ps[p[pos] + 1];
                    ps[ps[pos]] = pos;
                    ++sum[ps[pos]];
                }
                else sum[pos] = 1, ps[pos] = p[pos];
                ++sum[p[pos]];
                ps[p[pos]] = pos;
                res += sum[pos];
            }
        }
        if(str[pos] == ')') {
            if(p[pos] >= L) {
                if(str[p[pos] - 1] == ')' && p[p[pos] - 1] >= L) {
                    sum[pos] = sum[p[pos] - 1] + 1;
                    ps[pos] = ps[p[pos] - 1];
                    ++sum[ps[pos]];
                    ps[ps[pos]] = pos;
                }
                else sum[pos] = 1, ps[pos] = p[pos];
                ++sum[p[pos]];
                ps[p[pos]] = pos;
                res += sum[pos];
            }
        }
    }
    void del(int pos) {
        if(str[pos] == '(') {
            if(p[pos] <= R) {
                res -= sum[pos];
                if(str[p[pos] + 1] == '(' && p[p[pos] + 1] <= R) {
                    --sum[ps[pos]];
                    sum[p[pos] + 1] = sum[pos] - 1;
                    sum[p[p[pos] + 1]] = 1;
                    ps[p[p[pos] + 1]] = p[pos] + 1;
                    ps[ps[pos]] = p[pos] + 1;
                    ps[p[pos] + 1] = ps[pos];
                    ps[pos] = 0;
                }
                else sum[pos] = 0, ps[pos] = 0;
                --sum[p[pos]];
                ps[p[pos]] = 0;
            }
            sum[pos] = 0;
        }
        if(str[pos] == ')') {
            if(p[pos] >= L) {
                res -= sum[pos];
                if(str[p[pos] - 1] == ')' && p[p[pos] - 1] >= L) {
                    --sum[ps[pos]];
                    sum[p[pos] - 1] = sum[pos] - 1;
                    sum[p[p[pos] - 1]] = 1;
                    ps[p[p[pos] - 1]] = p[pos] - 1;
                    ps[ps[pos]] = p[pos] - 1;
                    ps[p[pos] - 1] = ps[pos];
                    ps[pos] = 0;
                }
                else sum[pos] = 0, ps[pos] = 0;
                --sum[p[pos]];
                ps[p[pos]] = 0;
            }
            sum[pos] = 0;
        }
    }
    ll calc(int l, int r) {
        while(L > l) add(--L);
        while(R < r) add(++R);
        while(L < l) del(L++);
        while(R > r) del(R--);
        return res;
    }
    void solve(int dl, int dr, int l, int r) {
        if(dl > dr || l > r) return;
        int mid = (dl + dr) >> 1, pos = l;
        ll mn = dp[l] + calc(l + 1, mid) + delta, tmp;
        for(int i = l; i <= r; ++i) {
            tmp = dp[i] + calc(i + 1, mid) + delta;
            if(tmp <= mn || (tmp == mn && s[i] > s[pos])) {
                pos = i, mn = tmp;
            }
        }
        if(mn < dp[mid] || (dp[mid] == mn && s[pos] + 1 > s[mid])) {
            dp[mid] = mn;
            s[mid] = s[pos] + 1;
        }
        solve(dl, mid - 1, l, pos);
        solve(mid + 1, dr, pos, r);
    }
    void CDQ(int l, int r) {
        if(l + 1 >= r) return;
        int mid = (l + r) >> 1;
        CDQ(l, mid);
        solve(mid, r - 1, l, mid - 1);
        CDQ(mid, r);
    }
    bool check() {
        fill(dp + 1, dp + 1 + n, 0x7f7f7f7f7f7f7f7f);
        dp[0] = 0;
        CDQ(0, n + 1);
        return s[n] >= k;
    }
    int main() {
        ios::sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
        cin >> n >> k >> str;
        str = '$' + str;
        for(int i = 1; i <= n; ++i) {
            if(str[i] == '(') p[i] = n + 1;
            else p[i] = 0;
        }
        for(int i = 1; i <= n; ++i) {
            if(str[i] == '(') stk.push(i);
            else if(!stk.empty()) {
                p[i] = stk.top();
                p[stk.top()] = i;
                stk.pop();
            }
        }
        L = 1, R = 0, res = 0;
        l = -10000000000ll, r = 10000000000ll;
        while(l <= r) delta = (l + r) >> 1, check()  (dp[n] - delta * k, delta + 1) : (delta - 1)
        cout << ans;
        cerr << '\n' << clock() << '\n';
        return 0;
    }
    
    • 1

    Information

    ID
    8502
    Time
    6000ms
    Memory
    512MiB
    Difficulty
    7
    Tags
    # Submissions
    3
    Accepted
    1
    Uploaded By