#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll,ll>;
const int N=1e6+5;
int n,k;
string s;
int a[N],b[N];
ll qs[N];
pl dp[N];
struct Line{
ll m,c,cnt;
Line(ll _m,ll _c,ll _cnt):m(_m),c(_c),cnt(_cnt){}
ll eval(ll x){
return m*x+c;
}
};
struct CHT{
deque<Line> hull;
bool bad(Line x,Line y,Line z){
return (x.c-y.c)*(z.m-y.m)>(y.c-z.c)*(y.m-x.m);
}
void insert(ll m,ll c,ll cnt){
Line v(m,c,cnt);
while(hull.size()>1&&bad(hull.end()[-2],hull.back(),v))hull.pop_back();
hull.emplace_back(v);
}
pl query(ll x){
while(hull.size()>1&&hull[0].eval(x)>hull[1].eval(x))hull.pop_front();
return pl(hull[0].eval(x),hull[0].cnt);
}
};
pl solve(ll lambda){
CHT cht;
cht.insert(0,0,0);
queue<int> q;
for(int i=1;i<=n;i++){
while(!q.empty()&&b[q.front()]<i){
int j=q.front();
q.pop();
cht.insert(-j,dp[j].first-qs[b[j]]+1LL*j*b[j],dp[j].second);
}
dp[i]=cht.query(i);
dp[i].first+=qs[i]+lambda;
dp[i].second++;
q.emplace(i);
}
return dp[n];
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k >> s;
for(int i=0,ia=0,ib=0;i<2*n;i++){
if(s[i]=='A'){
a[++ia]=ib;
}else{
ib++;
}
}
for(int i=1,p=1;i<=n;i++){
while(p<=n&&a[p]<i)p++;
b[i]=max(i,p-1);
}
for(int i=1;i<=n;i++)qs[i]=qs[i-1]+a[i];
ll l=0,r=1LL*n*n;
while(l<r){
ll m=(l+r)/2;
if(solve(m).second<=k)r=m;
else l=m+1;
}
cout << solve(l).first-k*l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
4 ms |
4700 KB |
Output is correct |
30 |
Correct |
5 ms |
4776 KB |
Output is correct |
31 |
Correct |
5 ms |
4772 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4952 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
4 ms |
4700 KB |
Output is correct |
36 |
Correct |
5 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
3 ms |
4700 KB |
Output is correct |
39 |
Correct |
5 ms |
4700 KB |
Output is correct |
40 |
Correct |
3 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
4700 KB |
Output is correct |
42 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
4 ms |
4700 KB |
Output is correct |
30 |
Correct |
5 ms |
4776 KB |
Output is correct |
31 |
Correct |
5 ms |
4772 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4952 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
4 ms |
4700 KB |
Output is correct |
36 |
Correct |
5 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
3 ms |
4700 KB |
Output is correct |
39 |
Correct |
5 ms |
4700 KB |
Output is correct |
40 |
Correct |
3 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
4700 KB |
Output is correct |
42 |
Correct |
2 ms |
4700 KB |
Output is correct |
43 |
Correct |
73 ms |
8100 KB |
Output is correct |
44 |
Correct |
124 ms |
8780 KB |
Output is correct |
45 |
Correct |
125 ms |
8904 KB |
Output is correct |
46 |
Correct |
57 ms |
8960 KB |
Output is correct |
47 |
Correct |
62 ms |
8868 KB |
Output is correct |
48 |
Correct |
77 ms |
8872 KB |
Output is correct |
49 |
Correct |
68 ms |
8868 KB |
Output is correct |
50 |
Correct |
128 ms |
8876 KB |
Output is correct |
51 |
Correct |
131 ms |
8872 KB |
Output is correct |
52 |
Correct |
67 ms |
8872 KB |
Output is correct |
53 |
Correct |
69 ms |
8868 KB |
Output is correct |
54 |
Correct |
132 ms |
8872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
4 ms |
4700 KB |
Output is correct |
30 |
Correct |
5 ms |
4776 KB |
Output is correct |
31 |
Correct |
5 ms |
4772 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4952 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
4 ms |
4700 KB |
Output is correct |
36 |
Correct |
5 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
3 ms |
4700 KB |
Output is correct |
39 |
Correct |
5 ms |
4700 KB |
Output is correct |
40 |
Correct |
3 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
4700 KB |
Output is correct |
42 |
Correct |
2 ms |
4700 KB |
Output is correct |
43 |
Correct |
73 ms |
8100 KB |
Output is correct |
44 |
Correct |
124 ms |
8780 KB |
Output is correct |
45 |
Correct |
125 ms |
8904 KB |
Output is correct |
46 |
Correct |
57 ms |
8960 KB |
Output is correct |
47 |
Correct |
62 ms |
8868 KB |
Output is correct |
48 |
Correct |
77 ms |
8872 KB |
Output is correct |
49 |
Correct |
68 ms |
8868 KB |
Output is correct |
50 |
Correct |
128 ms |
8876 KB |
Output is correct |
51 |
Correct |
131 ms |
8872 KB |
Output is correct |
52 |
Correct |
67 ms |
8872 KB |
Output is correct |
53 |
Correct |
69 ms |
8868 KB |
Output is correct |
54 |
Correct |
132 ms |
8872 KB |
Output is correct |
55 |
Correct |
921 ms |
35232 KB |
Output is correct |
56 |
Correct |
1136 ms |
45756 KB |
Output is correct |
57 |
Correct |
1474 ms |
45804 KB |
Output is correct |
58 |
Correct |
674 ms |
45808 KB |
Output is correct |
59 |
Correct |
775 ms |
45760 KB |
Output is correct |
60 |
Correct |
787 ms |
45764 KB |
Output is correct |
61 |
Correct |
787 ms |
45764 KB |
Output is correct |
62 |
Correct |
1534 ms |
45748 KB |
Output is correct |
63 |
Correct |
1573 ms |
45620 KB |
Output is correct |
64 |
Correct |
810 ms |
45764 KB |
Output is correct |
65 |
Correct |
804 ms |
45760 KB |
Output is correct |
66 |
Correct |
1553 ms |
45748 KB |
Output is correct |
67 |
Correct |
798 ms |
45764 KB |
Output is correct |