#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++;
b[ib]=max(ib,ia);
}
}
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 |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 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 |
4440 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 |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 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 |
4440 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 |
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 |
2 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 |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 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 |
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 |
4440 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 |
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 |
2 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 |
4700 KB |
Output is correct |
31 |
Correct |
5 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4952 KB |
Output is correct |
35 |
Correct |
3 ms |
4700 KB |
Output is correct |
36 |
Correct |
6 ms |
4772 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
4 ms |
4700 KB |
Output is correct |
39 |
Correct |
6 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 |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 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 |
4440 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 |
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 |
2 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 |
4700 KB |
Output is correct |
31 |
Correct |
5 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4952 KB |
Output is correct |
35 |
Correct |
3 ms |
4700 KB |
Output is correct |
36 |
Correct |
6 ms |
4772 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
4 ms |
4700 KB |
Output is correct |
39 |
Correct |
6 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 |
71 ms |
8096 KB |
Output is correct |
44 |
Correct |
125 ms |
9016 KB |
Output is correct |
45 |
Correct |
125 ms |
8904 KB |
Output is correct |
46 |
Correct |
68 ms |
9004 KB |
Output is correct |
47 |
Correct |
61 ms |
8872 KB |
Output is correct |
48 |
Correct |
71 ms |
8872 KB |
Output is correct |
49 |
Correct |
69 ms |
8868 KB |
Output is correct |
50 |
Correct |
130 ms |
8876 KB |
Output is correct |
51 |
Correct |
133 ms |
8872 KB |
Output is correct |
52 |
Correct |
69 ms |
8872 KB |
Output is correct |
53 |
Correct |
68 ms |
8872 KB |
Output is correct |
54 |
Correct |
129 ms |
8872 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 |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 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 |
4440 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 |
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 |
2 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 |
4700 KB |
Output is correct |
31 |
Correct |
5 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4952 KB |
Output is correct |
35 |
Correct |
3 ms |
4700 KB |
Output is correct |
36 |
Correct |
6 ms |
4772 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
4 ms |
4700 KB |
Output is correct |
39 |
Correct |
6 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 |
71 ms |
8096 KB |
Output is correct |
44 |
Correct |
125 ms |
9016 KB |
Output is correct |
45 |
Correct |
125 ms |
8904 KB |
Output is correct |
46 |
Correct |
68 ms |
9004 KB |
Output is correct |
47 |
Correct |
61 ms |
8872 KB |
Output is correct |
48 |
Correct |
71 ms |
8872 KB |
Output is correct |
49 |
Correct |
69 ms |
8868 KB |
Output is correct |
50 |
Correct |
130 ms |
8876 KB |
Output is correct |
51 |
Correct |
133 ms |
8872 KB |
Output is correct |
52 |
Correct |
69 ms |
8872 KB |
Output is correct |
53 |
Correct |
68 ms |
8872 KB |
Output is correct |
54 |
Correct |
129 ms |
8872 KB |
Output is correct |
55 |
Correct |
924 ms |
35228 KB |
Output is correct |
56 |
Correct |
1145 ms |
45772 KB |
Output is correct |
57 |
Correct |
1469 ms |
45652 KB |
Output is correct |
58 |
Correct |
681 ms |
45844 KB |
Output is correct |
59 |
Correct |
819 ms |
45748 KB |
Output is correct |
60 |
Correct |
796 ms |
45764 KB |
Output is correct |
61 |
Correct |
786 ms |
45844 KB |
Output is correct |
62 |
Correct |
1556 ms |
45752 KB |
Output is correct |
63 |
Correct |
1585 ms |
45768 KB |
Output is correct |
64 |
Correct |
810 ms |
45588 KB |
Output is correct |
65 |
Correct |
827 ms |
45844 KB |
Output is correct |
66 |
Correct |
1560 ms |
45748 KB |
Output is correct |
67 |
Correct |
825 ms |
45752 KB |
Output is correct |