#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 = 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
432 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
432 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
5 ms |
768 KB |
Output is correct |
31 |
Correct |
4 ms |
776 KB |
Output is correct |
32 |
Correct |
3 ms |
776 KB |
Output is correct |
33 |
Correct |
4 ms |
772 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
4 ms |
1156 KB |
Output is correct |
36 |
Correct |
4 ms |
680 KB |
Output is correct |
37 |
Correct |
4 ms |
776 KB |
Output is correct |
38 |
Correct |
4 ms |
964 KB |
Output is correct |
39 |
Correct |
4 ms |
784 KB |
Output is correct |
40 |
Correct |
3 ms |
664 KB |
Output is correct |
41 |
Correct |
3 ms |
664 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
432 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
5 ms |
768 KB |
Output is correct |
31 |
Correct |
4 ms |
776 KB |
Output is correct |
32 |
Correct |
3 ms |
776 KB |
Output is correct |
33 |
Correct |
4 ms |
772 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
4 ms |
1156 KB |
Output is correct |
36 |
Correct |
4 ms |
680 KB |
Output is correct |
37 |
Correct |
4 ms |
776 KB |
Output is correct |
38 |
Correct |
4 ms |
964 KB |
Output is correct |
39 |
Correct |
4 ms |
784 KB |
Output is correct |
40 |
Correct |
3 ms |
664 KB |
Output is correct |
41 |
Correct |
3 ms |
664 KB |
Output is correct |
42 |
Correct |
2 ms |
656 KB |
Output is correct |
43 |
Correct |
6 ms |
3872 KB |
Output is correct |
44 |
Correct |
121 ms |
6520 KB |
Output is correct |
45 |
Correct |
128 ms |
6456 KB |
Output is correct |
46 |
Incorrect |
95 ms |
6244 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
432 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
5 ms |
768 KB |
Output is correct |
31 |
Correct |
4 ms |
776 KB |
Output is correct |
32 |
Correct |
3 ms |
776 KB |
Output is correct |
33 |
Correct |
4 ms |
772 KB |
Output is correct |
34 |
Correct |
4 ms |
968 KB |
Output is correct |
35 |
Correct |
4 ms |
1156 KB |
Output is correct |
36 |
Correct |
4 ms |
680 KB |
Output is correct |
37 |
Correct |
4 ms |
776 KB |
Output is correct |
38 |
Correct |
4 ms |
964 KB |
Output is correct |
39 |
Correct |
4 ms |
784 KB |
Output is correct |
40 |
Correct |
3 ms |
664 KB |
Output is correct |
41 |
Correct |
3 ms |
664 KB |
Output is correct |
42 |
Correct |
2 ms |
656 KB |
Output is correct |
43 |
Correct |
6 ms |
3872 KB |
Output is correct |
44 |
Correct |
121 ms |
6520 KB |
Output is correct |
45 |
Correct |
128 ms |
6456 KB |
Output is correct |
46 |
Incorrect |
95 ms |
6244 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |