#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 = 1000001;
int64_t p[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')
j++;
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 cht = [&](int64_t const lambda)
{
deque<pair<linear_fn, size_t>> q;
q.emplace_back(linear_fn(0, -p[j_[1] - 1] + j_[1]), 0);
for (int64_t i = 1; i <= n; ++i)
{
while (q.size() >= 2 && q[0].first(i) >= q[1].first(i))
q.pop_front();
int64_t v = q[0].first(i) + p[i] - i - 1 + lambda;
size_t u = q[0].second + 1;
if (i == n)
return make_pair(v, u);
int64_t const j__ = j_[i + 1];
linear_fn f(-i, v - p[j__ - 1] + j__ * i + j__ - i);
while (q.size() >= 2 && q[q.size() - 2].first.intersects_earlier(f, q.back().first))
q.pop_back();
q.emplace_back(f, u);
}
};
int64_t a = 0, b = 1LL << 51;
while (a < b)
{
int64_t lambda = (a + b + 1) / 2;
auto [val, used] = cht(lambda);
if (used < k)
b = lambda - 1;
else
a = lambda;
}
int64_t val = cht(a).first;
cout << val - (int64_t)k * a << '\n';
}
Compilation message
chorus.cpp: In lambda function:
chorus.cpp:47:31: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int64_t i = 1; i <= n; ++i)
| ~~^~~~
chorus.cpp:53:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
53 | if (i == n)
| ~~^~~~
chorus.cpp:61:5: warning: control reaches end of non-void function [-Wreturn-type]
61 | };
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 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 |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 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 |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
5 ms |
460 KB |
Output is correct |
30 |
Correct |
6 ms |
464 KB |
Output is correct |
31 |
Correct |
6 ms |
464 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
468 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
5 ms |
468 KB |
Output is correct |
37 |
Correct |
6 ms |
468 KB |
Output is correct |
38 |
Correct |
4 ms |
468 KB |
Output is correct |
39 |
Correct |
6 ms |
464 KB |
Output is correct |
40 |
Correct |
4 ms |
468 KB |
Output is correct |
41 |
Correct |
5 ms |
492 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 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 |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
5 ms |
460 KB |
Output is correct |
30 |
Correct |
6 ms |
464 KB |
Output is correct |
31 |
Correct |
6 ms |
464 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
468 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
5 ms |
468 KB |
Output is correct |
37 |
Correct |
6 ms |
468 KB |
Output is correct |
38 |
Correct |
4 ms |
468 KB |
Output is correct |
39 |
Correct |
6 ms |
464 KB |
Output is correct |
40 |
Correct |
4 ms |
468 KB |
Output is correct |
41 |
Correct |
5 ms |
492 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
43 |
Correct |
69 ms |
2088 KB |
Output is correct |
44 |
Correct |
118 ms |
3324 KB |
Output is correct |
45 |
Correct |
129 ms |
3300 KB |
Output is correct |
46 |
Correct |
66 ms |
2400 KB |
Output is correct |
47 |
Correct |
72 ms |
3480 KB |
Output is correct |
48 |
Correct |
83 ms |
3344 KB |
Output is correct |
49 |
Correct |
83 ms |
3336 KB |
Output is correct |
50 |
Correct |
121 ms |
3356 KB |
Output is correct |
51 |
Correct |
120 ms |
3324 KB |
Output is correct |
52 |
Correct |
90 ms |
3340 KB |
Output is correct |
53 |
Correct |
87 ms |
3352 KB |
Output is correct |
54 |
Correct |
123 ms |
3348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 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 |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
5 ms |
460 KB |
Output is correct |
30 |
Correct |
6 ms |
464 KB |
Output is correct |
31 |
Correct |
6 ms |
464 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
468 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
5 ms |
468 KB |
Output is correct |
37 |
Correct |
6 ms |
468 KB |
Output is correct |
38 |
Correct |
4 ms |
468 KB |
Output is correct |
39 |
Correct |
6 ms |
464 KB |
Output is correct |
40 |
Correct |
4 ms |
468 KB |
Output is correct |
41 |
Correct |
5 ms |
492 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
43 |
Correct |
69 ms |
2088 KB |
Output is correct |
44 |
Correct |
118 ms |
3324 KB |
Output is correct |
45 |
Correct |
129 ms |
3300 KB |
Output is correct |
46 |
Correct |
66 ms |
2400 KB |
Output is correct |
47 |
Correct |
72 ms |
3480 KB |
Output is correct |
48 |
Correct |
83 ms |
3344 KB |
Output is correct |
49 |
Correct |
83 ms |
3336 KB |
Output is correct |
50 |
Correct |
121 ms |
3356 KB |
Output is correct |
51 |
Correct |
120 ms |
3324 KB |
Output is correct |
52 |
Correct |
90 ms |
3340 KB |
Output is correct |
53 |
Correct |
87 ms |
3352 KB |
Output is correct |
54 |
Correct |
123 ms |
3348 KB |
Output is correct |
55 |
Correct |
849 ms |
19508 KB |
Output is correct |
56 |
Correct |
1124 ms |
31428 KB |
Output is correct |
57 |
Correct |
1327 ms |
31500 KB |
Output is correct |
58 |
Correct |
676 ms |
19948 KB |
Output is correct |
59 |
Correct |
786 ms |
32044 KB |
Output is correct |
60 |
Correct |
887 ms |
31340 KB |
Output is correct |
61 |
Correct |
955 ms |
31424 KB |
Output is correct |
62 |
Correct |
1305 ms |
31424 KB |
Output is correct |
63 |
Correct |
1368 ms |
31428 KB |
Output is correct |
64 |
Correct |
937 ms |
31368 KB |
Output is correct |
65 |
Correct |
914 ms |
31296 KB |
Output is correct |
66 |
Correct |
1309 ms |
31412 KB |
Output is correct |
67 |
Correct |
915 ms |
31428 KB |
Output is correct |