#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 |
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 |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 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 |
4452 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 |
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 |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 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 |
4452 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 |
4560 KB |
Output is correct |
27 |
Correct |
1 ms |
4560 KB |
Output is correct |
28 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 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 |
4452 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 |
4560 KB |
Output is correct |
27 |
Correct |
1 ms |
4560 KB |
Output is correct |
28 |
Correct |
1 ms |
4440 KB |
Output is correct |
29 |
Correct |
4 ms |
4696 KB |
Output is correct |
30 |
Correct |
6 ms |
4768 KB |
Output is correct |
31 |
Correct |
6 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4852 KB |
Output is correct |
34 |
Correct |
3 ms |
4604 KB |
Output is correct |
35 |
Correct |
5 ms |
4700 KB |
Output is correct |
36 |
Correct |
6 ms |
4576 KB |
Output is correct |
37 |
Correct |
7 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 |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 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 |
4452 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 |
4560 KB |
Output is correct |
27 |
Correct |
1 ms |
4560 KB |
Output is correct |
28 |
Correct |
1 ms |
4440 KB |
Output is correct |
29 |
Correct |
4 ms |
4696 KB |
Output is correct |
30 |
Correct |
6 ms |
4768 KB |
Output is correct |
31 |
Correct |
6 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4852 KB |
Output is correct |
34 |
Correct |
3 ms |
4604 KB |
Output is correct |
35 |
Correct |
5 ms |
4700 KB |
Output is correct |
36 |
Correct |
6 ms |
4576 KB |
Output is correct |
37 |
Correct |
7 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 |
75 ms |
7944 KB |
Output is correct |
44 |
Correct |
131 ms |
8928 KB |
Output is correct |
45 |
Correct |
136 ms |
8696 KB |
Output is correct |
46 |
Correct |
60 ms |
9216 KB |
Output is correct |
47 |
Correct |
63 ms |
9128 KB |
Output is correct |
48 |
Correct |
69 ms |
9424 KB |
Output is correct |
49 |
Correct |
69 ms |
9124 KB |
Output is correct |
50 |
Correct |
145 ms |
8792 KB |
Output is correct |
51 |
Correct |
134 ms |
8792 KB |
Output is correct |
52 |
Correct |
69 ms |
9128 KB |
Output is correct |
53 |
Correct |
82 ms |
9128 KB |
Output is correct |
54 |
Correct |
131 ms |
8764 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 |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 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 |
4452 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 |
4560 KB |
Output is correct |
27 |
Correct |
1 ms |
4560 KB |
Output is correct |
28 |
Correct |
1 ms |
4440 KB |
Output is correct |
29 |
Correct |
4 ms |
4696 KB |
Output is correct |
30 |
Correct |
6 ms |
4768 KB |
Output is correct |
31 |
Correct |
6 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4852 KB |
Output is correct |
34 |
Correct |
3 ms |
4604 KB |
Output is correct |
35 |
Correct |
5 ms |
4700 KB |
Output is correct |
36 |
Correct |
6 ms |
4576 KB |
Output is correct |
37 |
Correct |
7 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 |
75 ms |
7944 KB |
Output is correct |
44 |
Correct |
131 ms |
8928 KB |
Output is correct |
45 |
Correct |
136 ms |
8696 KB |
Output is correct |
46 |
Correct |
60 ms |
9216 KB |
Output is correct |
47 |
Correct |
63 ms |
9128 KB |
Output is correct |
48 |
Correct |
69 ms |
9424 KB |
Output is correct |
49 |
Correct |
69 ms |
9124 KB |
Output is correct |
50 |
Correct |
145 ms |
8792 KB |
Output is correct |
51 |
Correct |
134 ms |
8792 KB |
Output is correct |
52 |
Correct |
69 ms |
9128 KB |
Output is correct |
53 |
Correct |
82 ms |
9128 KB |
Output is correct |
54 |
Correct |
131 ms |
8764 KB |
Output is correct |
55 |
Correct |
956 ms |
34568 KB |
Output is correct |
56 |
Correct |
1172 ms |
45568 KB |
Output is correct |
57 |
Correct |
1507 ms |
44820 KB |
Output is correct |
58 |
Correct |
681 ms |
47620 KB |
Output is correct |
59 |
Correct |
860 ms |
47608 KB |
Output is correct |
60 |
Correct |
847 ms |
48068 KB |
Output is correct |
61 |
Correct |
846 ms |
47624 KB |
Output is correct |
62 |
Correct |
1625 ms |
44720 KB |
Output is correct |
63 |
Correct |
1618 ms |
44708 KB |
Output is correct |
64 |
Correct |
870 ms |
47680 KB |
Output is correct |
65 |
Correct |
889 ms |
47816 KB |
Output is correct |
66 |
Correct |
1574 ms |
45192 KB |
Output is correct |
67 |
Correct |
810 ms |
47660 KB |
Output is correct |