Submission #871563

# Submission time Handle Problem Language Result Execution time Memory
871563 2023-11-11T05:54:37 Z resting Chorus (JOI23_chorus) C++17
87 / 100
7000 ms 264968 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));
    }
    void clear(){
        m = 0; b = 1e9; cnt = 0;
        if(l==r)return;
        lc->clear();rc->clear();
    }
} mem[(int)4e6+5]; int memsz = 0; 
segtree* segtree::getmem(){return &mem[memsz++];}
int n, k; 
int res = 0;
int cnt = 0;string s; 
vector<int> b;
    segtree ac(0, 1e6);
void solve(int m){
    ac.clear();
    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 = 2e12;
    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 Correct 523 ms 250892 KB Output is correct
2 Correct 492 ms 250708 KB Output is correct
3 Correct 490 ms 250904 KB Output is correct
4 Correct 499 ms 250896 KB Output is correct
5 Correct 483 ms 250716 KB Output is correct
6 Correct 497 ms 250896 KB Output is correct
7 Correct 507 ms 250964 KB Output is correct
8 Correct 512 ms 250704 KB Output is correct
9 Correct 500 ms 250708 KB Output is correct
10 Correct 498 ms 250896 KB Output is correct
11 Correct 491 ms 250704 KB Output is correct
12 Correct 491 ms 250708 KB Output is correct
13 Correct 486 ms 250708 KB Output is correct
14 Correct 492 ms 250828 KB Output is correct
15 Correct 492 ms 250860 KB Output is correct
16 Correct 495 ms 250892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 250892 KB Output is correct
2 Correct 492 ms 250708 KB Output is correct
3 Correct 490 ms 250904 KB Output is correct
4 Correct 499 ms 250896 KB Output is correct
5 Correct 483 ms 250716 KB Output is correct
6 Correct 497 ms 250896 KB Output is correct
7 Correct 507 ms 250964 KB Output is correct
8 Correct 512 ms 250704 KB Output is correct
9 Correct 500 ms 250708 KB Output is correct
10 Correct 498 ms 250896 KB Output is correct
11 Correct 491 ms 250704 KB Output is correct
12 Correct 491 ms 250708 KB Output is correct
13 Correct 486 ms 250708 KB Output is correct
14 Correct 492 ms 250828 KB Output is correct
15 Correct 492 ms 250860 KB Output is correct
16 Correct 495 ms 250892 KB Output is correct
17 Correct 488 ms 250964 KB Output is correct
18 Correct 525 ms 251228 KB Output is correct
19 Correct 494 ms 250960 KB Output is correct
20 Correct 504 ms 250840 KB Output is correct
21 Correct 510 ms 250908 KB Output is correct
22 Correct 507 ms 250964 KB Output is correct
23 Correct 496 ms 250728 KB Output is correct
24 Correct 504 ms 250904 KB Output is correct
25 Correct 485 ms 250708 KB Output is correct
26 Correct 502 ms 250960 KB Output is correct
27 Correct 499 ms 250912 KB Output is correct
28 Correct 519 ms 250708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 250892 KB Output is correct
2 Correct 492 ms 250708 KB Output is correct
3 Correct 490 ms 250904 KB Output is correct
4 Correct 499 ms 250896 KB Output is correct
5 Correct 483 ms 250716 KB Output is correct
6 Correct 497 ms 250896 KB Output is correct
7 Correct 507 ms 250964 KB Output is correct
8 Correct 512 ms 250704 KB Output is correct
9 Correct 500 ms 250708 KB Output is correct
10 Correct 498 ms 250896 KB Output is correct
11 Correct 491 ms 250704 KB Output is correct
12 Correct 491 ms 250708 KB Output is correct
13 Correct 486 ms 250708 KB Output is correct
14 Correct 492 ms 250828 KB Output is correct
15 Correct 492 ms 250860 KB Output is correct
16 Correct 495 ms 250892 KB Output is correct
17 Correct 488 ms 250964 KB Output is correct
18 Correct 525 ms 251228 KB Output is correct
19 Correct 494 ms 250960 KB Output is correct
20 Correct 504 ms 250840 KB Output is correct
21 Correct 510 ms 250908 KB Output is correct
22 Correct 507 ms 250964 KB Output is correct
23 Correct 496 ms 250728 KB Output is correct
24 Correct 504 ms 250904 KB Output is correct
25 Correct 485 ms 250708 KB Output is correct
26 Correct 502 ms 250960 KB Output is correct
27 Correct 499 ms 250912 KB Output is correct
28 Correct 519 ms 250708 KB Output is correct
29 Correct 561 ms 251016 KB Output is correct
30 Correct 525 ms 251216 KB Output is correct
31 Correct 527 ms 250888 KB Output is correct
32 Correct 527 ms 251020 KB Output is correct
33 Correct 534 ms 251232 KB Output is correct
34 Correct 543 ms 251220 KB Output is correct
35 Correct 521 ms 251024 KB Output is correct
36 Correct 541 ms 251020 KB Output is correct
37 Correct 544 ms 251016 KB Output is correct
38 Correct 541 ms 251016 KB Output is correct
39 Correct 534 ms 251220 KB Output is correct
40 Correct 535 ms 251020 KB Output is correct
41 Correct 539 ms 250988 KB Output is correct
42 Correct 530 ms 251144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 250892 KB Output is correct
2 Correct 492 ms 250708 KB Output is correct
3 Correct 490 ms 250904 KB Output is correct
4 Correct 499 ms 250896 KB Output is correct
5 Correct 483 ms 250716 KB Output is correct
6 Correct 497 ms 250896 KB Output is correct
7 Correct 507 ms 250964 KB Output is correct
8 Correct 512 ms 250704 KB Output is correct
9 Correct 500 ms 250708 KB Output is correct
10 Correct 498 ms 250896 KB Output is correct
11 Correct 491 ms 250704 KB Output is correct
12 Correct 491 ms 250708 KB Output is correct
13 Correct 486 ms 250708 KB Output is correct
14 Correct 492 ms 250828 KB Output is correct
15 Correct 492 ms 250860 KB Output is correct
16 Correct 495 ms 250892 KB Output is correct
17 Correct 488 ms 250964 KB Output is correct
18 Correct 525 ms 251228 KB Output is correct
19 Correct 494 ms 250960 KB Output is correct
20 Correct 504 ms 250840 KB Output is correct
21 Correct 510 ms 250908 KB Output is correct
22 Correct 507 ms 250964 KB Output is correct
23 Correct 496 ms 250728 KB Output is correct
24 Correct 504 ms 250904 KB Output is correct
25 Correct 485 ms 250708 KB Output is correct
26 Correct 502 ms 250960 KB Output is correct
27 Correct 499 ms 250912 KB Output is correct
28 Correct 519 ms 250708 KB Output is correct
29 Correct 561 ms 251016 KB Output is correct
30 Correct 525 ms 251216 KB Output is correct
31 Correct 527 ms 250888 KB Output is correct
32 Correct 527 ms 251020 KB Output is correct
33 Correct 534 ms 251232 KB Output is correct
34 Correct 543 ms 251220 KB Output is correct
35 Correct 521 ms 251024 KB Output is correct
36 Correct 541 ms 251020 KB Output is correct
37 Correct 544 ms 251016 KB Output is correct
38 Correct 541 ms 251016 KB Output is correct
39 Correct 534 ms 251220 KB Output is correct
40 Correct 535 ms 251020 KB Output is correct
41 Correct 539 ms 250988 KB Output is correct
42 Correct 530 ms 251144 KB Output is correct
43 Correct 1052 ms 251684 KB Output is correct
44 Correct 1473 ms 252248 KB Output is correct
45 Correct 1438 ms 252060 KB Output is correct
46 Correct 1504 ms 252252 KB Output is correct
47 Correct 1385 ms 252252 KB Output is correct
48 Correct 1353 ms 252248 KB Output is correct
49 Correct 1315 ms 252252 KB Output is correct
50 Correct 1485 ms 252248 KB Output is correct
51 Correct 1434 ms 252248 KB Output is correct
52 Correct 1315 ms 252248 KB Output is correct
53 Correct 1349 ms 252248 KB Output is correct
54 Correct 1466 ms 252248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 250892 KB Output is correct
2 Correct 492 ms 250708 KB Output is correct
3 Correct 490 ms 250904 KB Output is correct
4 Correct 499 ms 250896 KB Output is correct
5 Correct 483 ms 250716 KB Output is correct
6 Correct 497 ms 250896 KB Output is correct
7 Correct 507 ms 250964 KB Output is correct
8 Correct 512 ms 250704 KB Output is correct
9 Correct 500 ms 250708 KB Output is correct
10 Correct 498 ms 250896 KB Output is correct
11 Correct 491 ms 250704 KB Output is correct
12 Correct 491 ms 250708 KB Output is correct
13 Correct 486 ms 250708 KB Output is correct
14 Correct 492 ms 250828 KB Output is correct
15 Correct 492 ms 250860 KB Output is correct
16 Correct 495 ms 250892 KB Output is correct
17 Correct 488 ms 250964 KB Output is correct
18 Correct 525 ms 251228 KB Output is correct
19 Correct 494 ms 250960 KB Output is correct
20 Correct 504 ms 250840 KB Output is correct
21 Correct 510 ms 250908 KB Output is correct
22 Correct 507 ms 250964 KB Output is correct
23 Correct 496 ms 250728 KB Output is correct
24 Correct 504 ms 250904 KB Output is correct
25 Correct 485 ms 250708 KB Output is correct
26 Correct 502 ms 250960 KB Output is correct
27 Correct 499 ms 250912 KB Output is correct
28 Correct 519 ms 250708 KB Output is correct
29 Correct 561 ms 251016 KB Output is correct
30 Correct 525 ms 251216 KB Output is correct
31 Correct 527 ms 250888 KB Output is correct
32 Correct 527 ms 251020 KB Output is correct
33 Correct 534 ms 251232 KB Output is correct
34 Correct 543 ms 251220 KB Output is correct
35 Correct 521 ms 251024 KB Output is correct
36 Correct 541 ms 251020 KB Output is correct
37 Correct 544 ms 251016 KB Output is correct
38 Correct 541 ms 251016 KB Output is correct
39 Correct 534 ms 251220 KB Output is correct
40 Correct 535 ms 251020 KB Output is correct
41 Correct 539 ms 250988 KB Output is correct
42 Correct 530 ms 251144 KB Output is correct
43 Correct 1052 ms 251684 KB Output is correct
44 Correct 1473 ms 252248 KB Output is correct
45 Correct 1438 ms 252060 KB Output is correct
46 Correct 1504 ms 252252 KB Output is correct
47 Correct 1385 ms 252252 KB Output is correct
48 Correct 1353 ms 252248 KB Output is correct
49 Correct 1315 ms 252252 KB Output is correct
50 Correct 1485 ms 252248 KB Output is correct
51 Correct 1434 ms 252248 KB Output is correct
52 Correct 1315 ms 252248 KB Output is correct
53 Correct 1349 ms 252248 KB Output is correct
54 Correct 1466 ms 252248 KB Output is correct
55 Correct 6474 ms 261480 KB Output is correct
56 Execution timed out 7099 ms 264968 KB Time limit exceeded
57 Halted 0 ms 0 KB -