#include <bits/stdc++.h>
using namespace std;
struct linear_fn
{
int64_t m, t;
linear_fn(int64_t m, int64_t t) { this->m = m, this->t = t; }
int64_t operator()(int64_t x) { return m * x + t; }
bool intersects_earlier(linear_fn const &f, linear_fn const &g)
{
return (__int128_t)(f.t - t) * (m - g.m) < (__int128_t)(m - f.m) * (g.t - t);
}
};
constexpr size_t N = 5001;
int64_t p[N], b[N], f[N], u[N], j_[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, k;
string s;
cin >> n >> k >> s;
for (size_t i = 1, j = 1, l = 1; i <= n << 1; ++i)
{
if (s[i - 1] == 'B')
b[j++] = i;
else
p[l++] = j;
}
for (size_t i = 1; i <= n; ++i)
j_[i] = max<int64_t>(i, lower_bound(p + 1, p + n + 1, i) - p);
for (size_t i = 1; i <= n; ++i)
p[i] += p[i - 1];
auto dp = [&](int64_t const lambda)
{
fill(f + 1, f + N, INT64_MAX);
for (int64_t i = 1; i <= n; ++i)
for (int64_t j = 0; j < i; ++j)
if (f[j] != INT64_MAX)
{
int64_t j__ = j_[j + 1],
val = f[j] + p[i] - p[j__ - 1] - (i - j__ + 1) * (j + 1) + lambda;
if (val < f[i])
f[i] = val, u[i] = u[j] + 1;
}
return make_pair(f[n], u[n]);
};
int64_t a = 0, b = 1LL << 42;
while (a < b)
{
int64_t lambda = (a + b) / 2;
auto [val, used] = dp(lambda);
if (used > k)
a = lambda + 1;
else
b = lambda;
}
auto val = dp(a).first;
cout << val - (int64_t)k * a << '\n';
}
Compilation message
chorus.cpp: In lambda function:
chorus.cpp:45:35: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int64_t i = 1; i <= n; ++i)
| ~~^~~~
chorus.cpp: In function 'int main()':
chorus.cpp:62:22: warning: comparison of integer expressions of different signedness: 'std::tuple_element<1, std::pair<long int, long int> >::type' {aka 'long int'} and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
62 | if (used > k)
| ~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 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 |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
9 ms |
340 KB |
Output is correct |
19 |
Correct |
12 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
324 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
8 ms |
340 KB |
Output is correct |
23 |
Correct |
12 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
340 KB |
Output is correct |
25 |
Correct |
12 ms |
400 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
12 ms |
396 KB |
Output is correct |
28 |
Correct |
8 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 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 |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
9 ms |
340 KB |
Output is correct |
19 |
Correct |
12 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
324 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
8 ms |
340 KB |
Output is correct |
23 |
Correct |
12 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
340 KB |
Output is correct |
25 |
Correct |
12 ms |
400 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
12 ms |
396 KB |
Output is correct |
28 |
Correct |
8 ms |
340 KB |
Output is correct |
29 |
Correct |
694 ms |
528 KB |
Output is correct |
30 |
Correct |
735 ms |
532 KB |
Output is correct |
31 |
Correct |
732 ms |
532 KB |
Output is correct |
32 |
Correct |
634 ms |
544 KB |
Output is correct |
33 |
Correct |
641 ms |
536 KB |
Output is correct |
34 |
Correct |
729 ms |
528 KB |
Output is correct |
35 |
Correct |
821 ms |
532 KB |
Output is correct |
36 |
Correct |
714 ms |
536 KB |
Output is correct |
37 |
Correct |
747 ms |
528 KB |
Output is correct |
38 |
Correct |
765 ms |
532 KB |
Output is correct |
39 |
Correct |
712 ms |
536 KB |
Output is correct |
40 |
Correct |
713 ms |
544 KB |
Output is correct |
41 |
Correct |
736 ms |
588 KB |
Output is correct |
42 |
Correct |
713 ms |
544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 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 |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
9 ms |
340 KB |
Output is correct |
19 |
Correct |
12 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
324 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
8 ms |
340 KB |
Output is correct |
23 |
Correct |
12 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
340 KB |
Output is correct |
25 |
Correct |
12 ms |
400 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
12 ms |
396 KB |
Output is correct |
28 |
Correct |
8 ms |
340 KB |
Output is correct |
29 |
Correct |
694 ms |
528 KB |
Output is correct |
30 |
Correct |
735 ms |
532 KB |
Output is correct |
31 |
Correct |
732 ms |
532 KB |
Output is correct |
32 |
Correct |
634 ms |
544 KB |
Output is correct |
33 |
Correct |
641 ms |
536 KB |
Output is correct |
34 |
Correct |
729 ms |
528 KB |
Output is correct |
35 |
Correct |
821 ms |
532 KB |
Output is correct |
36 |
Correct |
714 ms |
536 KB |
Output is correct |
37 |
Correct |
747 ms |
528 KB |
Output is correct |
38 |
Correct |
765 ms |
532 KB |
Output is correct |
39 |
Correct |
712 ms |
536 KB |
Output is correct |
40 |
Correct |
713 ms |
544 KB |
Output is correct |
41 |
Correct |
736 ms |
588 KB |
Output is correct |
42 |
Correct |
713 ms |
544 KB |
Output is correct |
43 |
Runtime error |
2 ms |
1216 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 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 |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
9 ms |
340 KB |
Output is correct |
19 |
Correct |
12 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
324 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
8 ms |
340 KB |
Output is correct |
23 |
Correct |
12 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
340 KB |
Output is correct |
25 |
Correct |
12 ms |
400 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
12 ms |
396 KB |
Output is correct |
28 |
Correct |
8 ms |
340 KB |
Output is correct |
29 |
Correct |
694 ms |
528 KB |
Output is correct |
30 |
Correct |
735 ms |
532 KB |
Output is correct |
31 |
Correct |
732 ms |
532 KB |
Output is correct |
32 |
Correct |
634 ms |
544 KB |
Output is correct |
33 |
Correct |
641 ms |
536 KB |
Output is correct |
34 |
Correct |
729 ms |
528 KB |
Output is correct |
35 |
Correct |
821 ms |
532 KB |
Output is correct |
36 |
Correct |
714 ms |
536 KB |
Output is correct |
37 |
Correct |
747 ms |
528 KB |
Output is correct |
38 |
Correct |
765 ms |
532 KB |
Output is correct |
39 |
Correct |
712 ms |
536 KB |
Output is correct |
40 |
Correct |
713 ms |
544 KB |
Output is correct |
41 |
Correct |
736 ms |
588 KB |
Output is correct |
42 |
Correct |
713 ms |
544 KB |
Output is correct |
43 |
Runtime error |
2 ms |
1216 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |