#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Line {
ll m, b, s;
ll eval(ll x) { return m * x + b; }
};
int main() {
int N, K; cin >> N >> K;
string s; cin >> s;
vector<int> h(N);
int x = 0, y = 0;
for(int i = 0; i < 2 * N; i++) {
if(s[i] == 'A') h[x] = y, x++;
else y++;
}
auto f = [&] (ll k) {
vector<pair<int, Line>> cht;
int best = 0;
vector<pair<ll, ll>> dp(N + 1);
ll g = 0;
int j = 0;
for(int i = 0; i < N; i++) {
while(j <= h[i] && j <= i) {
Line l;
l.m = -j;
l.b = dp[j].first - g + (ll) i * j;
while(cht.size() > 0) {
if(cht.back().second.eval(cht.back().second.s) > l.eval(cht.back().second.s)) cht.pop_back();
else break;
if(best == cht.size()) best--;
}
if(cht.size() == 0) {
l.s = 0;
cht.push_back({j, l});
best = 0;
} else {
l.s = (l.b - cht.back().second.b) / (cht.back().second.m - l.m) + 1;
cht.push_back({j, l});
}
j++;
}
g += h[i];
while(best + 1 < cht.size() && cht[best + 1].second.s <= i + 1) best++;
dp[i + 1].first = cht[best].second.eval(i + 1) + k + g;
dp[i + 1].second = dp[cht[best].first].second + 1;
}
return dp[N];
};
ll L = 0, R = (ll)N * N + 1;
while(L < R) {
ll mid = (L + R) / 2;
if(f(mid).second <= K) {
R = mid;
} else {
L = mid + 1;
}
}
pair<ll, ll> ans = f(L);
cout << ans.first - L * K << endl;
}
Compilation message
chorus.cpp: In lambda function:
chorus.cpp:32:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, Line> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | if(best == cht.size()) best--;
| ~~~~~^~~~~~~~~~~~~
chorus.cpp:45:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, Line> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while(best + 1 < cht.size() && cht[best + 1].second.s <= i + 1) best++;
| ~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
412 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
412 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
900 KB |
Output is correct |
30 |
Correct |
5 ms |
864 KB |
Output is correct |
31 |
Correct |
5 ms |
772 KB |
Output is correct |
32 |
Correct |
4 ms |
760 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
4 ms |
664 KB |
Output is correct |
37 |
Correct |
5 ms |
760 KB |
Output is correct |
38 |
Correct |
4 ms |
968 KB |
Output is correct |
39 |
Correct |
4 ms |
840 KB |
Output is correct |
40 |
Correct |
3 ms |
664 KB |
Output is correct |
41 |
Correct |
3 ms |
660 KB |
Output is correct |
42 |
Correct |
2 ms |
656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
412 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
900 KB |
Output is correct |
30 |
Correct |
5 ms |
864 KB |
Output is correct |
31 |
Correct |
5 ms |
772 KB |
Output is correct |
32 |
Correct |
4 ms |
760 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
4 ms |
664 KB |
Output is correct |
37 |
Correct |
5 ms |
760 KB |
Output is correct |
38 |
Correct |
4 ms |
968 KB |
Output is correct |
39 |
Correct |
4 ms |
840 KB |
Output is correct |
40 |
Correct |
3 ms |
664 KB |
Output is correct |
41 |
Correct |
3 ms |
660 KB |
Output is correct |
42 |
Correct |
2 ms |
656 KB |
Output is correct |
43 |
Correct |
71 ms |
3824 KB |
Output is correct |
44 |
Correct |
128 ms |
6260 KB |
Output is correct |
45 |
Correct |
129 ms |
6268 KB |
Output is correct |
46 |
Correct |
110 ms |
6308 KB |
Output is correct |
47 |
Correct |
100 ms |
6684 KB |
Output is correct |
48 |
Correct |
137 ms |
9992 KB |
Output is correct |
49 |
Correct |
129 ms |
10804 KB |
Output is correct |
50 |
Correct |
129 ms |
6044 KB |
Output is correct |
51 |
Correct |
140 ms |
6100 KB |
Output is correct |
52 |
Correct |
123 ms |
10352 KB |
Output is correct |
53 |
Correct |
120 ms |
10112 KB |
Output is correct |
54 |
Correct |
156 ms |
6304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
412 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
900 KB |
Output is correct |
30 |
Correct |
5 ms |
864 KB |
Output is correct |
31 |
Correct |
5 ms |
772 KB |
Output is correct |
32 |
Correct |
4 ms |
760 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
4 ms |
664 KB |
Output is correct |
37 |
Correct |
5 ms |
760 KB |
Output is correct |
38 |
Correct |
4 ms |
968 KB |
Output is correct |
39 |
Correct |
4 ms |
840 KB |
Output is correct |
40 |
Correct |
3 ms |
664 KB |
Output is correct |
41 |
Correct |
3 ms |
660 KB |
Output is correct |
42 |
Correct |
2 ms |
656 KB |
Output is correct |
43 |
Correct |
71 ms |
3824 KB |
Output is correct |
44 |
Correct |
128 ms |
6260 KB |
Output is correct |
45 |
Correct |
129 ms |
6268 KB |
Output is correct |
46 |
Correct |
110 ms |
6308 KB |
Output is correct |
47 |
Correct |
100 ms |
6684 KB |
Output is correct |
48 |
Correct |
137 ms |
9992 KB |
Output is correct |
49 |
Correct |
129 ms |
10804 KB |
Output is correct |
50 |
Correct |
129 ms |
6044 KB |
Output is correct |
51 |
Correct |
140 ms |
6100 KB |
Output is correct |
52 |
Correct |
123 ms |
10352 KB |
Output is correct |
53 |
Correct |
120 ms |
10112 KB |
Output is correct |
54 |
Correct |
156 ms |
6304 KB |
Output is correct |
55 |
Correct |
894 ms |
43672 KB |
Output is correct |
56 |
Correct |
1291 ms |
75336 KB |
Output is correct |
57 |
Correct |
1376 ms |
57444 KB |
Output is correct |
58 |
Correct |
973 ms |
58524 KB |
Output is correct |
59 |
Correct |
1100 ms |
75040 KB |
Output is correct |
60 |
Correct |
1040 ms |
75160 KB |
Output is correct |
61 |
Correct |
1129 ms |
74696 KB |
Output is correct |
62 |
Correct |
1433 ms |
55740 KB |
Output is correct |
63 |
Correct |
1452 ms |
55688 KB |
Output is correct |
64 |
Correct |
1134 ms |
75160 KB |
Output is correct |
65 |
Correct |
1149 ms |
75016 KB |
Output is correct |
66 |
Correct |
1410 ms |
55328 KB |
Output is correct |
67 |
Correct |
1133 ms |
75268 KB |
Output is correct |