#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e6 + 5;
const long long inf = 1e18;
struct line {
long long a, b;
long long eval(long long x) const {
return a * x + b;
}
long long div(long long a, long long b) const {
return a / b - ((a ^ b) < 0 && a % b);
}
long long slope(const line &o) const {
return div(o.b - b, a - o.a);
}
};
int n, k;
int a[N];
long long pf[N];
string s;
deque<line> hull;
pair<long long, int> dp[N];
void add(line x) {
while (hull.size() > 1 && hull.end()[-2].slope(hull.back()) <= hull.back().slope(x)) {
hull.pop_back();
}
hull.push_back(x);
}
pair<long long, int> qry(long long x) {
while (hull.size() > 1 && hull[1].eval(x) < hull[0].eval(x)) {
hull.pop_front();
}
int j = hull[0].a;
return {hull[0].eval(x), dp[j].second + 1};
}
pair<long long, int> check(long long pen) {
hull.clear();
fill(dp, dp + n + 1, pair<long long, int>{inf, 0});
dp[0] = {0, 0};
deque<int> dq{0};
for (int i = 1, j = 0; i <= n; ++i) {
while (j < i && a[j + 1] <= i) {
add({j, dp[j].first + pf[j]});
if (dq.size() && dq.front() == j) {
dq.pop_front();
}
++j;
}
if (dq.size()) {
dp[i] = dp[dq.front()];
dp[i].second += 1;
}
if (hull.size()) {
auto x = qry(-i);
dp[i] = min(dp[i], {x.first + (long long) j * i - pf[j], x.second});
}
dp[i].first += pen;
while (dq.size() && dp[i] <= dp[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
return dp[n];
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> k >> s;
for (int i = 0, p = 1; i < 2 * n; ++i) {
if (s[i] == 'A') {
++a[p];
} else {
a[p + 1] = a[p];
++p;
}
}
for (int i = 1; i <= n; ++i) {
pf[i] = pf[i - 1] + a[i];
}
long long l = 0, r = (long long) n * n, p = r;
while (l <= r) {
auto md = (l + r) / 2;
if (check(md).second <= k) {
p = md;
r = md - 1;
} else {
l = md + 1;
}
}
cout << check(p).first - k * p;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
5 ms |
2652 KB |
Output is correct |
30 |
Correct |
5 ms |
2460 KB |
Output is correct |
31 |
Correct |
5 ms |
2684 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
3 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
5 ms |
2652 KB |
Output is correct |
37 |
Correct |
5 ms |
2652 KB |
Output is correct |
38 |
Correct |
4 ms |
2696 KB |
Output is correct |
39 |
Correct |
5 ms |
2652 KB |
Output is correct |
40 |
Correct |
4 ms |
2660 KB |
Output is correct |
41 |
Correct |
4 ms |
2652 KB |
Output is correct |
42 |
Correct |
5 ms |
2660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
5 ms |
2652 KB |
Output is correct |
30 |
Correct |
5 ms |
2460 KB |
Output is correct |
31 |
Correct |
5 ms |
2684 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
3 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
5 ms |
2652 KB |
Output is correct |
37 |
Correct |
5 ms |
2652 KB |
Output is correct |
38 |
Correct |
4 ms |
2696 KB |
Output is correct |
39 |
Correct |
5 ms |
2652 KB |
Output is correct |
40 |
Correct |
4 ms |
2660 KB |
Output is correct |
41 |
Correct |
4 ms |
2652 KB |
Output is correct |
42 |
Correct |
5 ms |
2660 KB |
Output is correct |
43 |
Correct |
79 ms |
4184 KB |
Output is correct |
44 |
Correct |
131 ms |
5456 KB |
Output is correct |
45 |
Correct |
133 ms |
5352 KB |
Output is correct |
46 |
Correct |
88 ms |
5604 KB |
Output is correct |
47 |
Correct |
80 ms |
5608 KB |
Output is correct |
48 |
Correct |
82 ms |
5712 KB |
Output is correct |
49 |
Correct |
107 ms |
6120 KB |
Output is correct |
50 |
Correct |
142 ms |
6032 KB |
Output is correct |
51 |
Correct |
151 ms |
5452 KB |
Output is correct |
52 |
Correct |
83 ms |
5608 KB |
Output is correct |
53 |
Correct |
85 ms |
5604 KB |
Output is correct |
54 |
Correct |
145 ms |
6120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
5 ms |
2652 KB |
Output is correct |
30 |
Correct |
5 ms |
2460 KB |
Output is correct |
31 |
Correct |
5 ms |
2684 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
3 ms |
2652 KB |
Output is correct |
35 |
Correct |
4 ms |
2652 KB |
Output is correct |
36 |
Correct |
5 ms |
2652 KB |
Output is correct |
37 |
Correct |
5 ms |
2652 KB |
Output is correct |
38 |
Correct |
4 ms |
2696 KB |
Output is correct |
39 |
Correct |
5 ms |
2652 KB |
Output is correct |
40 |
Correct |
4 ms |
2660 KB |
Output is correct |
41 |
Correct |
4 ms |
2652 KB |
Output is correct |
42 |
Correct |
5 ms |
2660 KB |
Output is correct |
43 |
Correct |
79 ms |
4184 KB |
Output is correct |
44 |
Correct |
131 ms |
5456 KB |
Output is correct |
45 |
Correct |
133 ms |
5352 KB |
Output is correct |
46 |
Correct |
88 ms |
5604 KB |
Output is correct |
47 |
Correct |
80 ms |
5608 KB |
Output is correct |
48 |
Correct |
82 ms |
5712 KB |
Output is correct |
49 |
Correct |
107 ms |
6120 KB |
Output is correct |
50 |
Correct |
142 ms |
6032 KB |
Output is correct |
51 |
Correct |
151 ms |
5452 KB |
Output is correct |
52 |
Correct |
83 ms |
5608 KB |
Output is correct |
53 |
Correct |
85 ms |
5604 KB |
Output is correct |
54 |
Correct |
145 ms |
6120 KB |
Output is correct |
55 |
Correct |
1044 ms |
25584 KB |
Output is correct |
56 |
Correct |
1311 ms |
38388 KB |
Output is correct |
57 |
Correct |
1643 ms |
37876 KB |
Output is correct |
58 |
Correct |
929 ms |
39832 KB |
Output is correct |
59 |
Correct |
1012 ms |
39924 KB |
Output is correct |
60 |
Correct |
1030 ms |
39980 KB |
Output is correct |
61 |
Correct |
999 ms |
40052 KB |
Output is correct |
62 |
Correct |
1754 ms |
37872 KB |
Output is correct |
63 |
Correct |
1707 ms |
37948 KB |
Output is correct |
64 |
Correct |
1078 ms |
39920 KB |
Output is correct |
65 |
Correct |
1022 ms |
39920 KB |
Output is correct |
66 |
Correct |
1684 ms |
38128 KB |
Output is correct |
67 |
Correct |
1084 ms |
39840 KB |
Output is correct |