#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);
deque<int> dq;
for(int i=1;i<=n;i++){
while(!dq.empty()&&b[dq.front()]<i){
int j=dq.front();
dq.pop_front();
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];
if(!dq.empty())dp[i]=min(dp[i],dp[dq.front()]);
dp[i].first+=lambda;
dp[i].second++;
dq.emplace_back(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 |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 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 |
1 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 |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 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 |
1 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 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 |
4596 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 |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 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 |
1 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 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 |
4596 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 |
4696 KB |
Output is correct |
30 |
Correct |
6 ms |
4700 KB |
Output is correct |
31 |
Correct |
5 ms |
4952 KB |
Output is correct |
32 |
Correct |
3 ms |
4696 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4696 KB |
Output is correct |
35 |
Correct |
3 ms |
4772 KB |
Output is correct |
36 |
Correct |
6 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4688 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 |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 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 |
1 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 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 |
4596 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 |
4696 KB |
Output is correct |
30 |
Correct |
6 ms |
4700 KB |
Output is correct |
31 |
Correct |
5 ms |
4952 KB |
Output is correct |
32 |
Correct |
3 ms |
4696 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4696 KB |
Output is correct |
35 |
Correct |
3 ms |
4772 KB |
Output is correct |
36 |
Correct |
6 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4688 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 |
80 ms |
8100 KB |
Output is correct |
44 |
Correct |
128 ms |
8872 KB |
Output is correct |
45 |
Correct |
131 ms |
8904 KB |
Output is correct |
46 |
Correct |
59 ms |
8960 KB |
Output is correct |
47 |
Correct |
63 ms |
8868 KB |
Output is correct |
48 |
Correct |
71 ms |
8868 KB |
Output is correct |
49 |
Correct |
68 ms |
8868 KB |
Output is correct |
50 |
Correct |
132 ms |
8948 KB |
Output is correct |
51 |
Correct |
137 ms |
8872 KB |
Output is correct |
52 |
Correct |
71 ms |
8868 KB |
Output is correct |
53 |
Correct |
69 ms |
8872 KB |
Output is correct |
54 |
Correct |
145 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 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 |
1 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 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 |
4596 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 |
4696 KB |
Output is correct |
30 |
Correct |
6 ms |
4700 KB |
Output is correct |
31 |
Correct |
5 ms |
4952 KB |
Output is correct |
32 |
Correct |
3 ms |
4696 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4696 KB |
Output is correct |
35 |
Correct |
3 ms |
4772 KB |
Output is correct |
36 |
Correct |
6 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4688 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 |
80 ms |
8100 KB |
Output is correct |
44 |
Correct |
128 ms |
8872 KB |
Output is correct |
45 |
Correct |
131 ms |
8904 KB |
Output is correct |
46 |
Correct |
59 ms |
8960 KB |
Output is correct |
47 |
Correct |
63 ms |
8868 KB |
Output is correct |
48 |
Correct |
71 ms |
8868 KB |
Output is correct |
49 |
Correct |
68 ms |
8868 KB |
Output is correct |
50 |
Correct |
132 ms |
8948 KB |
Output is correct |
51 |
Correct |
137 ms |
8872 KB |
Output is correct |
52 |
Correct |
71 ms |
8868 KB |
Output is correct |
53 |
Correct |
69 ms |
8872 KB |
Output is correct |
54 |
Correct |
145 ms |
8960 KB |
Output is correct |
55 |
Correct |
970 ms |
35228 KB |
Output is correct |
56 |
Correct |
1187 ms |
45728 KB |
Output is correct |
57 |
Correct |
1562 ms |
45652 KB |
Output is correct |
58 |
Correct |
679 ms |
45808 KB |
Output is correct |
59 |
Correct |
783 ms |
45736 KB |
Output is correct |
60 |
Correct |
811 ms |
45764 KB |
Output is correct |
61 |
Correct |
808 ms |
45732 KB |
Output is correct |
62 |
Correct |
1610 ms |
45708 KB |
Output is correct |
63 |
Correct |
1630 ms |
45648 KB |
Output is correct |
64 |
Correct |
810 ms |
45764 KB |
Output is correct |
65 |
Correct |
844 ms |
45720 KB |
Output is correct |
66 |
Correct |
1603 ms |
45656 KB |
Output is correct |
67 |
Correct |
811 ms |
45760 KB |
Output is correct |