#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
constexpr ll INF = 4e18;
char s[2002000];
int a[1001000], b[1001000], nxt[2002000], prv[2002000], r2[1001000];
ll cst[505][505], dp_naive[505][505], sum[1001000];
inline ll cost(int l, int r){ // O(1)
if (r2[r] < l) return 0;
return (ll)(r+1) * (r2[r]-l+1) - (sum[r2[r]] - sum[l-1]);
}
ll naive(int n, int k){
for (int i=1;i<=n;i++){
for (int j=i;j<=n;j++){
for (int k=i;k<=j;k++){
cst[i][j] += max(j-nxt[b[k]]+1, 0);
}
}
}
for (int i=0;i<=n;i++){
fill(dp_naive[i], dp_naive[i]+k+1, INF);
}
dp_naive[0][0] = 0;
for (int i=1;i<=n;i++){
for (int j=1;j<=k;j++){
for (int p=0;p<i;p++) dp_naive[i][j] = min(dp_naive[i][j], dp_naive[p][j-1] + cst[p+1][i]);
}
}
return dp_naive[n][k];
}
ll dp[1001000];
int dp2[1001000];
struct Hull{
vector<int> st, st2;
int pt, cap;
ll lambda;
void init(int n, ll _lambda){
st.clear();
st2.clear();
pt = 0;
cap = n;
lambda = _lambda;
}
inline ll calc(int f, int x){
assert(f < x);
return dp[f] + cost(f+1, x) * 2 + lambda;
}
int cross(int f, int g){
int l = g+1, r = cap, ret = cap+1;
while(l<=r){
int mid = (l+r)>>1;
if (calc(f, mid) > calc(g, mid)) ret = mid, r = mid-1;
else l = mid+1;
}
return ret;
}
void push(int x){
if (x==cap) return;
while(st.size() >= 2 && st2.back() >= x+1 && calc(st.back(), st2.back()) >= calc(x, st2.back())){
st.pop_back();
st2.pop_back();
}
if (st.size() >= 1 && calc(st.back(), cap) <= calc(x, cap)) return;
if (st.size() >= 1) st2.push_back(cross(st.back(), x));
st.push_back(x);
assert(st2.empty() || st2.back() <= cap);
}
pair<ll, int> query(int x){
pt = min(pt, (int)st.size()-1);
while(pt+1<(int)st.size() && st2[pt] <= x) pt++;
return {calc(st[pt], x), dp2[st[pt]]+1};
}
}hull;
int alien(int n, ll lambda){ // y + \lambda x min
hull.init(n, lambda);
hull.push(0);
for (int i=1;i<=n;i++){
auto [val, cnt] = hull.query(i);
dp[i] = val;
dp2[i] = cnt;
hull.push(i);
}
return dp2[n];
}
ll solve(int n, int k){
ll l = 0, r = 1e12 + 100, opt = -1;
while(l<=r){
ll mid = (l+r) / 2;
int val = alien(n, mid*2-1);
if (val==k){
return (dp[n] - (mid*2-1) * val) / 2;
}
if (val >= k) opt = mid, l = mid+1;
else r = mid-1;
}
assert(opt >= 0);
int cnt = alien(n, opt*2-1);
ll val = (dp[n] - (opt*2-1) * cnt) / 2;
if (cnt==k){
return val;
}
int cnt2 = alien(n, opt*2+1);
ll val2 = (dp[n] - (opt*2+1) * cnt2) / 2;
assert(cnt2 < k);
return val2 - opt * (k-cnt2);
}
int main(){
int n, k;
scanf("%d %d", &n, &k);
hull.st.reserve(n+100);
hull.st2.reserve(n+100);
scanf("%s", s+1);
int ptA = 1, ptB = 1;
for (int i=1;i<=n*2;i++){
if (s[i]=='A'){
prv[i] = ptB-1;
a[ptA++] = i;
}
else{
prv[i] = ptB;
b[ptB++] = i;
}
}
for (int i=n*2;i;i--){
if (s[i]=='A') nxt[i] = --ptA;
else nxt[i] = ptA;
}
for (int i=1;i<=n;i++) sum[i] = sum[i-1] + nxt[b[i]], r2[i] = min(i, prv[a[i]]);
printf("%lld\n", solve(n, k));
}
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:144:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
chorus.cpp:149:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | scanf("%s", s+1);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
9 ms |
572 KB |
Output is correct |
30 |
Correct |
11 ms |
648 KB |
Output is correct |
31 |
Correct |
10 ms |
644 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
576 KB |
Output is correct |
34 |
Correct |
9 ms |
668 KB |
Output is correct |
35 |
Correct |
8 ms |
580 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
10 ms |
660 KB |
Output is correct |
38 |
Correct |
8 ms |
596 KB |
Output is correct |
39 |
Correct |
10 ms |
596 KB |
Output is correct |
40 |
Correct |
8 ms |
648 KB |
Output is correct |
41 |
Correct |
8 ms |
596 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
9 ms |
572 KB |
Output is correct |
30 |
Correct |
11 ms |
648 KB |
Output is correct |
31 |
Correct |
10 ms |
644 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
576 KB |
Output is correct |
34 |
Correct |
9 ms |
668 KB |
Output is correct |
35 |
Correct |
8 ms |
580 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
10 ms |
660 KB |
Output is correct |
38 |
Correct |
8 ms |
596 KB |
Output is correct |
39 |
Correct |
10 ms |
596 KB |
Output is correct |
40 |
Correct |
8 ms |
648 KB |
Output is correct |
41 |
Correct |
8 ms |
596 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
43 |
Correct |
189 ms |
3816 KB |
Output is correct |
44 |
Correct |
292 ms |
6152 KB |
Output is correct |
45 |
Correct |
321 ms |
6224 KB |
Output is correct |
46 |
Correct |
4 ms |
5460 KB |
Output is correct |
47 |
Correct |
12 ms |
5504 KB |
Output is correct |
48 |
Correct |
245 ms |
6240 KB |
Output is correct |
49 |
Correct |
225 ms |
6244 KB |
Output is correct |
50 |
Correct |
72 ms |
5612 KB |
Output is correct |
51 |
Correct |
280 ms |
5628 KB |
Output is correct |
52 |
Correct |
226 ms |
6220 KB |
Output is correct |
53 |
Correct |
262 ms |
6260 KB |
Output is correct |
54 |
Correct |
306 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
9 ms |
572 KB |
Output is correct |
30 |
Correct |
11 ms |
648 KB |
Output is correct |
31 |
Correct |
10 ms |
644 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
576 KB |
Output is correct |
34 |
Correct |
9 ms |
668 KB |
Output is correct |
35 |
Correct |
8 ms |
580 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
10 ms |
660 KB |
Output is correct |
38 |
Correct |
8 ms |
596 KB |
Output is correct |
39 |
Correct |
10 ms |
596 KB |
Output is correct |
40 |
Correct |
8 ms |
648 KB |
Output is correct |
41 |
Correct |
8 ms |
596 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
43 |
Correct |
189 ms |
3816 KB |
Output is correct |
44 |
Correct |
292 ms |
6152 KB |
Output is correct |
45 |
Correct |
321 ms |
6224 KB |
Output is correct |
46 |
Correct |
4 ms |
5460 KB |
Output is correct |
47 |
Correct |
12 ms |
5504 KB |
Output is correct |
48 |
Correct |
245 ms |
6240 KB |
Output is correct |
49 |
Correct |
225 ms |
6244 KB |
Output is correct |
50 |
Correct |
72 ms |
5612 KB |
Output is correct |
51 |
Correct |
280 ms |
5628 KB |
Output is correct |
52 |
Correct |
226 ms |
6220 KB |
Output is correct |
53 |
Correct |
262 ms |
6260 KB |
Output is correct |
54 |
Correct |
306 ms |
5708 KB |
Output is correct |
55 |
Correct |
2438 ms |
38304 KB |
Output is correct |
56 |
Correct |
3651 ms |
58948 KB |
Output is correct |
57 |
Correct |
4046 ms |
58992 KB |
Output is correct |
58 |
Correct |
39 ms |
51192 KB |
Output is correct |
59 |
Correct |
92 ms |
52376 KB |
Output is correct |
60 |
Correct |
3489 ms |
59044 KB |
Output is correct |
61 |
Correct |
3163 ms |
58968 KB |
Output is correct |
62 |
Correct |
785 ms |
53236 KB |
Output is correct |
63 |
Correct |
2763 ms |
53732 KB |
Output is correct |
64 |
Correct |
2940 ms |
55596 KB |
Output is correct |
65 |
Correct |
3358 ms |
58980 KB |
Output is correct |
66 |
Correct |
3892 ms |
53980 KB |
Output is correct |
67 |
Correct |
3325 ms |
59124 KB |
Output is correct |