#include <bits/stdc++.h>
using namespace std;
const int nx=1e6+5;
#define ll long long
ll n, k, h[nx], p[nx], qs[nx], r=0, c=1, idx=1;
pair<ll, ll> dp[nx];
string s;
struct line
{
ll m, c, cnt;
line(ll m=0, ll c=0, ll cnt=0): m(m), c(c), cnt(cnt){}
ll val(ll x) {return m*x+c;}
};
struct convexhulltrick
{
deque<line> dq;
ll bad(line a, line b, line c)
{
return (a.c-c.c)*(b.m-a.m)<(a.c-b.c)*(c.m-a.m);
}
void add(line x)
{
while (dq.size()>=2&&bad(dq[dq.size()-2], dq.back(), x)) dq.pop_back();
dq.push_back(x);
}
line query(ll x)
{
while (dq.size()>=2&&dq[0].val(x)>dq[1].val(x)) dq.pop_front();
return dq[0];
}
} d;
pair<ll, ll> solve(ll lambda)
{
idx=1;
d.dq.clear();
for (int i=1; i<=n; i++)
{
while (idx<=p[i]&&idx<=i) d.add(line(-idx, qs[idx-1]+dp[idx-1].first, dp[idx-1].second)), idx++;
dp[i]={d.query(i).val(i)+i*p[i]-qs[p[i]-1]+lambda, d.query(i).cnt+1};
}
return dp[n];
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k>>s;
for (int i=0; i<2*n; i++)
{
if (s[i]=='A') r++, h[c]=r;
if (s[i]=='B') c++, h[c]=r;
}
for (int i=1; i<=n; i++)
{
qs[i]=qs[i-1]+h[i];
while (idx<=i&&h[idx]<i) idx++;
p[i]=idx;
}
/*
cout<<"debug "<<1<<'\n';
solve(1);
for (int i=1; i<=n; i++) cout<<i<<' '<<dp[i].first<<' '<<dp[i].second<<'\n';
for (auto x:d.dq) cout<<"dq "<<x.m<<' '<<x.c<<' '<<x.cnt<<'\n';*/
ll l=0, r=1e12;
while (l<r)
{
ll md=(l+r)/2;
if (solve(md).second<=k) r=md;
else l=md+1;
}
cout<<solve(l).first-k*l;
}
/*
(a.c-c.c)/(c.m-a.m)<(a.c-b.c)/(b.m-a.m)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 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 |
4568 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4560 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 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 |
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 |
4568 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4560 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 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 |
4696 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 |
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 |
4568 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4560 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 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 |
4696 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 |
7 ms |
4696 KB |
Output is correct |
30 |
Correct |
8 ms |
4700 KB |
Output is correct |
31 |
Correct |
8 ms |
4700 KB |
Output is correct |
32 |
Correct |
5 ms |
4700 KB |
Output is correct |
33 |
Correct |
5 ms |
4700 KB |
Output is correct |
34 |
Correct |
5 ms |
4700 KB |
Output is correct |
35 |
Correct |
5 ms |
4700 KB |
Output is correct |
36 |
Correct |
8 ms |
4696 KB |
Output is correct |
37 |
Correct |
8 ms |
4700 KB |
Output is correct |
38 |
Correct |
5 ms |
4700 KB |
Output is correct |
39 |
Correct |
7 ms |
4700 KB |
Output is correct |
40 |
Correct |
5 ms |
4700 KB |
Output is correct |
41 |
Correct |
6 ms |
4728 KB |
Output is correct |
42 |
Correct |
4 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 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 |
4568 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4560 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 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 |
4696 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 |
7 ms |
4696 KB |
Output is correct |
30 |
Correct |
8 ms |
4700 KB |
Output is correct |
31 |
Correct |
8 ms |
4700 KB |
Output is correct |
32 |
Correct |
5 ms |
4700 KB |
Output is correct |
33 |
Correct |
5 ms |
4700 KB |
Output is correct |
34 |
Correct |
5 ms |
4700 KB |
Output is correct |
35 |
Correct |
5 ms |
4700 KB |
Output is correct |
36 |
Correct |
8 ms |
4696 KB |
Output is correct |
37 |
Correct |
8 ms |
4700 KB |
Output is correct |
38 |
Correct |
5 ms |
4700 KB |
Output is correct |
39 |
Correct |
7 ms |
4700 KB |
Output is correct |
40 |
Correct |
5 ms |
4700 KB |
Output is correct |
41 |
Correct |
6 ms |
4728 KB |
Output is correct |
42 |
Correct |
4 ms |
4728 KB |
Output is correct |
43 |
Correct |
90 ms |
8068 KB |
Output is correct |
44 |
Correct |
152 ms |
8936 KB |
Output is correct |
45 |
Correct |
152 ms |
8920 KB |
Output is correct |
46 |
Correct |
90 ms |
9180 KB |
Output is correct |
47 |
Correct |
96 ms |
9000 KB |
Output is correct |
48 |
Correct |
103 ms |
9000 KB |
Output is correct |
49 |
Correct |
118 ms |
8936 KB |
Output is correct |
50 |
Correct |
164 ms |
8928 KB |
Output is correct |
51 |
Correct |
180 ms |
8924 KB |
Output is correct |
52 |
Correct |
104 ms |
8996 KB |
Output is correct |
53 |
Correct |
100 ms |
9180 KB |
Output is correct |
54 |
Correct |
159 ms |
8936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 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 |
4568 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4560 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 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 |
4696 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 |
7 ms |
4696 KB |
Output is correct |
30 |
Correct |
8 ms |
4700 KB |
Output is correct |
31 |
Correct |
8 ms |
4700 KB |
Output is correct |
32 |
Correct |
5 ms |
4700 KB |
Output is correct |
33 |
Correct |
5 ms |
4700 KB |
Output is correct |
34 |
Correct |
5 ms |
4700 KB |
Output is correct |
35 |
Correct |
5 ms |
4700 KB |
Output is correct |
36 |
Correct |
8 ms |
4696 KB |
Output is correct |
37 |
Correct |
8 ms |
4700 KB |
Output is correct |
38 |
Correct |
5 ms |
4700 KB |
Output is correct |
39 |
Correct |
7 ms |
4700 KB |
Output is correct |
40 |
Correct |
5 ms |
4700 KB |
Output is correct |
41 |
Correct |
6 ms |
4728 KB |
Output is correct |
42 |
Correct |
4 ms |
4728 KB |
Output is correct |
43 |
Correct |
90 ms |
8068 KB |
Output is correct |
44 |
Correct |
152 ms |
8936 KB |
Output is correct |
45 |
Correct |
152 ms |
8920 KB |
Output is correct |
46 |
Correct |
90 ms |
9180 KB |
Output is correct |
47 |
Correct |
96 ms |
9000 KB |
Output is correct |
48 |
Correct |
103 ms |
9000 KB |
Output is correct |
49 |
Correct |
118 ms |
8936 KB |
Output is correct |
50 |
Correct |
164 ms |
8928 KB |
Output is correct |
51 |
Correct |
180 ms |
8924 KB |
Output is correct |
52 |
Correct |
104 ms |
8996 KB |
Output is correct |
53 |
Correct |
100 ms |
9180 KB |
Output is correct |
54 |
Correct |
159 ms |
8936 KB |
Output is correct |
55 |
Correct |
1042 ms |
39872 KB |
Output is correct |
56 |
Correct |
1336 ms |
55392 KB |
Output is correct |
57 |
Correct |
1627 ms |
55540 KB |
Output is correct |
58 |
Correct |
909 ms |
55668 KB |
Output is correct |
59 |
Correct |
993 ms |
55620 KB |
Output is correct |
60 |
Correct |
1062 ms |
55540 KB |
Output is correct |
61 |
Correct |
1148 ms |
55540 KB |
Output is correct |
62 |
Correct |
1745 ms |
55544 KB |
Output is correct |
63 |
Correct |
1839 ms |
55532 KB |
Output is correct |
64 |
Correct |
1078 ms |
55400 KB |
Output is correct |
65 |
Correct |
1062 ms |
55528 KB |
Output is correct |
66 |
Correct |
1743 ms |
55544 KB |
Output is correct |
67 |
Correct |
1060 ms |
55540 KB |
Output is correct |