Submission #871556

# Submission time Handle Problem Language Result Execution time Memory
871556 2023-11-11T05:40:37 Z resting Chorus (JOI23_chorus) C++17
0 / 100
268 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

struct segtree{
    int l, r, i; int m, b; int cnt;
    segtree* lc = 0, *rc = 0;
    segtree* getmem();
    segtree(int l, int r) : l(l), r(r), m(0), b(1e9), cnt(0){
        i = (l + r) / 2;
        if(l==r) return;
        lc = getmem(); *lc = segtree(l, i);
        rc = getmem(); *rc = segtree(i+1, r);
    };
    segtree() : segtree(-1, -1){};
    void u(int qm, int qb, int qcnt){
        if(qm * i + qb < m*i+b){
            swap(m, qm); swap(b, qb); swap(cnt, qcnt);
        }
        if(l == r) return;
        if(qm > m) lc->u(qm, qb, qcnt);
        else rc->u(qm, qb, qcnt);
    }
    pair<int, int> q(int qi){
        pair<int, int> cur = {m*qi+b, cnt};
        if(i == qi) return cur;
        if(qi < i) return min(cur, lc->q(qi));
        else return min(cur, rc->q(qi));
    }

} mem[(int)3e7]; int memsz = 0; 
segtree* segtree::getmem(){return &mem[memsz++];}
int n, k; 
int res = 0;
int cnt = 0;string s; 
vector<int> b;
void solve(int m){
    segtree ac(0, n);
    int sl = 0;
    ac.u(0, 0, 0);
    int cnta = 0;
    int cur = 0;
    int funnynumber = 0;
    int cntactive = 0;
    for(int i = 0; i < 2 * n; i++){
        if(s[i] == 'A'){
            if(cntactive>0) funnynumber -= cntactive;
            cntactive++;
            if(cntactive>0) funnynumber += b[cnta] - i - cntactive;
            cnta++;
            cur += sl;
            auto tmp = ac.q(cnta); 
            tmp.first += m; tmp.second++;
            if(cnta == n) {
                res = tmp.first + cur;
                cnt = tmp.second;
            }
            ac.u(-cnta, tmp.first + (cnta)*cnta+funnynumber, tmp.second);
        }
        else{
            cntactive--;
            sl++;
        }
    }
}
signed main(){
    cin >> n >> k;
    cin >> s;
    int l = -1, r = 1e12;
    int m = 0; 
    for(int i = 0; i < 2*n; i++){
        if(s[i] == 'B') b.push_back(i);
    }
    while(r - l > 1){
        m = (l+r)/2;
        solve(m);
        if(cnt <= k) r = m;
        else l = m;
    }
    solve(r);
    cout<<res-k*r<<endl;
}
# Verdict Execution time Memory Grader output
1 Runtime error 268 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 268 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 268 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 268 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 268 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -