#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)4e6]; 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 = 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
250664 KB |
Output is correct |
2 |
Correct |
35 ms |
250708 KB |
Output is correct |
3 |
Correct |
34 ms |
250708 KB |
Output is correct |
4 |
Correct |
35 ms |
250896 KB |
Output is correct |
5 |
Correct |
35 ms |
250884 KB |
Output is correct |
6 |
Correct |
35 ms |
250704 KB |
Output is correct |
7 |
Correct |
34 ms |
250708 KB |
Output is correct |
8 |
Correct |
34 ms |
250708 KB |
Output is correct |
9 |
Correct |
35 ms |
250664 KB |
Output is correct |
10 |
Correct |
35 ms |
250716 KB |
Output is correct |
11 |
Correct |
35 ms |
250708 KB |
Output is correct |
12 |
Correct |
37 ms |
250700 KB |
Output is correct |
13 |
Correct |
35 ms |
250704 KB |
Output is correct |
14 |
Correct |
36 ms |
250708 KB |
Output is correct |
15 |
Correct |
35 ms |
250704 KB |
Output is correct |
16 |
Correct |
36 ms |
250704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
250664 KB |
Output is correct |
2 |
Correct |
35 ms |
250708 KB |
Output is correct |
3 |
Correct |
34 ms |
250708 KB |
Output is correct |
4 |
Correct |
35 ms |
250896 KB |
Output is correct |
5 |
Correct |
35 ms |
250884 KB |
Output is correct |
6 |
Correct |
35 ms |
250704 KB |
Output is correct |
7 |
Correct |
34 ms |
250708 KB |
Output is correct |
8 |
Correct |
34 ms |
250708 KB |
Output is correct |
9 |
Correct |
35 ms |
250664 KB |
Output is correct |
10 |
Correct |
35 ms |
250716 KB |
Output is correct |
11 |
Correct |
35 ms |
250708 KB |
Output is correct |
12 |
Correct |
37 ms |
250700 KB |
Output is correct |
13 |
Correct |
35 ms |
250704 KB |
Output is correct |
14 |
Correct |
36 ms |
250708 KB |
Output is correct |
15 |
Correct |
35 ms |
250704 KB |
Output is correct |
16 |
Correct |
36 ms |
250704 KB |
Output is correct |
17 |
Correct |
36 ms |
250704 KB |
Output is correct |
18 |
Correct |
38 ms |
250708 KB |
Output is correct |
19 |
Correct |
37 ms |
250708 KB |
Output is correct |
20 |
Correct |
36 ms |
250704 KB |
Output is correct |
21 |
Correct |
37 ms |
250704 KB |
Output is correct |
22 |
Correct |
37 ms |
250776 KB |
Output is correct |
23 |
Correct |
36 ms |
250712 KB |
Output is correct |
24 |
Correct |
37 ms |
250704 KB |
Output is correct |
25 |
Correct |
37 ms |
250728 KB |
Output is correct |
26 |
Correct |
44 ms |
250828 KB |
Output is correct |
27 |
Correct |
37 ms |
250964 KB |
Output is correct |
28 |
Correct |
38 ms |
250864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
250664 KB |
Output is correct |
2 |
Correct |
35 ms |
250708 KB |
Output is correct |
3 |
Correct |
34 ms |
250708 KB |
Output is correct |
4 |
Correct |
35 ms |
250896 KB |
Output is correct |
5 |
Correct |
35 ms |
250884 KB |
Output is correct |
6 |
Correct |
35 ms |
250704 KB |
Output is correct |
7 |
Correct |
34 ms |
250708 KB |
Output is correct |
8 |
Correct |
34 ms |
250708 KB |
Output is correct |
9 |
Correct |
35 ms |
250664 KB |
Output is correct |
10 |
Correct |
35 ms |
250716 KB |
Output is correct |
11 |
Correct |
35 ms |
250708 KB |
Output is correct |
12 |
Correct |
37 ms |
250700 KB |
Output is correct |
13 |
Correct |
35 ms |
250704 KB |
Output is correct |
14 |
Correct |
36 ms |
250708 KB |
Output is correct |
15 |
Correct |
35 ms |
250704 KB |
Output is correct |
16 |
Correct |
36 ms |
250704 KB |
Output is correct |
17 |
Correct |
36 ms |
250704 KB |
Output is correct |
18 |
Correct |
38 ms |
250708 KB |
Output is correct |
19 |
Correct |
37 ms |
250708 KB |
Output is correct |
20 |
Correct |
36 ms |
250704 KB |
Output is correct |
21 |
Correct |
37 ms |
250704 KB |
Output is correct |
22 |
Correct |
37 ms |
250776 KB |
Output is correct |
23 |
Correct |
36 ms |
250712 KB |
Output is correct |
24 |
Correct |
37 ms |
250704 KB |
Output is correct |
25 |
Correct |
37 ms |
250728 KB |
Output is correct |
26 |
Correct |
44 ms |
250828 KB |
Output is correct |
27 |
Correct |
37 ms |
250964 KB |
Output is correct |
28 |
Correct |
38 ms |
250864 KB |
Output is correct |
29 |
Correct |
68 ms |
251212 KB |
Output is correct |
30 |
Correct |
65 ms |
251032 KB |
Output is correct |
31 |
Correct |
70 ms |
250972 KB |
Output is correct |
32 |
Correct |
59 ms |
250960 KB |
Output is correct |
33 |
Correct |
60 ms |
250972 KB |
Output is correct |
34 |
Correct |
64 ms |
251220 KB |
Output is correct |
35 |
Correct |
62 ms |
251032 KB |
Output is correct |
36 |
Correct |
66 ms |
250964 KB |
Output is correct |
37 |
Correct |
68 ms |
250856 KB |
Output is correct |
38 |
Correct |
66 ms |
251448 KB |
Output is correct |
39 |
Correct |
67 ms |
251028 KB |
Output is correct |
40 |
Correct |
65 ms |
251036 KB |
Output is correct |
41 |
Correct |
65 ms |
250960 KB |
Output is correct |
42 |
Correct |
60 ms |
250964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
250664 KB |
Output is correct |
2 |
Correct |
35 ms |
250708 KB |
Output is correct |
3 |
Correct |
34 ms |
250708 KB |
Output is correct |
4 |
Correct |
35 ms |
250896 KB |
Output is correct |
5 |
Correct |
35 ms |
250884 KB |
Output is correct |
6 |
Correct |
35 ms |
250704 KB |
Output is correct |
7 |
Correct |
34 ms |
250708 KB |
Output is correct |
8 |
Correct |
34 ms |
250708 KB |
Output is correct |
9 |
Correct |
35 ms |
250664 KB |
Output is correct |
10 |
Correct |
35 ms |
250716 KB |
Output is correct |
11 |
Correct |
35 ms |
250708 KB |
Output is correct |
12 |
Correct |
37 ms |
250700 KB |
Output is correct |
13 |
Correct |
35 ms |
250704 KB |
Output is correct |
14 |
Correct |
36 ms |
250708 KB |
Output is correct |
15 |
Correct |
35 ms |
250704 KB |
Output is correct |
16 |
Correct |
36 ms |
250704 KB |
Output is correct |
17 |
Correct |
36 ms |
250704 KB |
Output is correct |
18 |
Correct |
38 ms |
250708 KB |
Output is correct |
19 |
Correct |
37 ms |
250708 KB |
Output is correct |
20 |
Correct |
36 ms |
250704 KB |
Output is correct |
21 |
Correct |
37 ms |
250704 KB |
Output is correct |
22 |
Correct |
37 ms |
250776 KB |
Output is correct |
23 |
Correct |
36 ms |
250712 KB |
Output is correct |
24 |
Correct |
37 ms |
250704 KB |
Output is correct |
25 |
Correct |
37 ms |
250728 KB |
Output is correct |
26 |
Correct |
44 ms |
250828 KB |
Output is correct |
27 |
Correct |
37 ms |
250964 KB |
Output is correct |
28 |
Correct |
38 ms |
250864 KB |
Output is correct |
29 |
Correct |
68 ms |
251212 KB |
Output is correct |
30 |
Correct |
65 ms |
251032 KB |
Output is correct |
31 |
Correct |
70 ms |
250972 KB |
Output is correct |
32 |
Correct |
59 ms |
250960 KB |
Output is correct |
33 |
Correct |
60 ms |
250972 KB |
Output is correct |
34 |
Correct |
64 ms |
251220 KB |
Output is correct |
35 |
Correct |
62 ms |
251032 KB |
Output is correct |
36 |
Correct |
66 ms |
250964 KB |
Output is correct |
37 |
Correct |
68 ms |
250856 KB |
Output is correct |
38 |
Correct |
66 ms |
251448 KB |
Output is correct |
39 |
Correct |
67 ms |
251028 KB |
Output is correct |
40 |
Correct |
65 ms |
251036 KB |
Output is correct |
41 |
Correct |
65 ms |
250960 KB |
Output is correct |
42 |
Correct |
60 ms |
250964 KB |
Output is correct |
43 |
Runtime error |
607 ms |
510112 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
250664 KB |
Output is correct |
2 |
Correct |
35 ms |
250708 KB |
Output is correct |
3 |
Correct |
34 ms |
250708 KB |
Output is correct |
4 |
Correct |
35 ms |
250896 KB |
Output is correct |
5 |
Correct |
35 ms |
250884 KB |
Output is correct |
6 |
Correct |
35 ms |
250704 KB |
Output is correct |
7 |
Correct |
34 ms |
250708 KB |
Output is correct |
8 |
Correct |
34 ms |
250708 KB |
Output is correct |
9 |
Correct |
35 ms |
250664 KB |
Output is correct |
10 |
Correct |
35 ms |
250716 KB |
Output is correct |
11 |
Correct |
35 ms |
250708 KB |
Output is correct |
12 |
Correct |
37 ms |
250700 KB |
Output is correct |
13 |
Correct |
35 ms |
250704 KB |
Output is correct |
14 |
Correct |
36 ms |
250708 KB |
Output is correct |
15 |
Correct |
35 ms |
250704 KB |
Output is correct |
16 |
Correct |
36 ms |
250704 KB |
Output is correct |
17 |
Correct |
36 ms |
250704 KB |
Output is correct |
18 |
Correct |
38 ms |
250708 KB |
Output is correct |
19 |
Correct |
37 ms |
250708 KB |
Output is correct |
20 |
Correct |
36 ms |
250704 KB |
Output is correct |
21 |
Correct |
37 ms |
250704 KB |
Output is correct |
22 |
Correct |
37 ms |
250776 KB |
Output is correct |
23 |
Correct |
36 ms |
250712 KB |
Output is correct |
24 |
Correct |
37 ms |
250704 KB |
Output is correct |
25 |
Correct |
37 ms |
250728 KB |
Output is correct |
26 |
Correct |
44 ms |
250828 KB |
Output is correct |
27 |
Correct |
37 ms |
250964 KB |
Output is correct |
28 |
Correct |
38 ms |
250864 KB |
Output is correct |
29 |
Correct |
68 ms |
251212 KB |
Output is correct |
30 |
Correct |
65 ms |
251032 KB |
Output is correct |
31 |
Correct |
70 ms |
250972 KB |
Output is correct |
32 |
Correct |
59 ms |
250960 KB |
Output is correct |
33 |
Correct |
60 ms |
250972 KB |
Output is correct |
34 |
Correct |
64 ms |
251220 KB |
Output is correct |
35 |
Correct |
62 ms |
251032 KB |
Output is correct |
36 |
Correct |
66 ms |
250964 KB |
Output is correct |
37 |
Correct |
68 ms |
250856 KB |
Output is correct |
38 |
Correct |
66 ms |
251448 KB |
Output is correct |
39 |
Correct |
67 ms |
251028 KB |
Output is correct |
40 |
Correct |
65 ms |
251036 KB |
Output is correct |
41 |
Correct |
65 ms |
250960 KB |
Output is correct |
42 |
Correct |
60 ms |
250964 KB |
Output is correct |
43 |
Runtime error |
607 ms |
510112 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |