#include <bits/stdc++.h>
using i64 = long long;
template <typename T>
struct Fenwick {
int n;
std::vector<T> a;
Fenwick(int n) : n(n), a(n) {}
void add(int x, T v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] += v;
}
}
T sum(int x) {
T ans = 0;
for (int i = x; i > 0; i -= i & -i) {
ans += a[i - 1];
}
return ans;
}
T rangeSum(int l, int r) {
return sum(r) - sum(l);
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::string s;
std::cin >> s;
std::vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'B') {
a.push_back(i);
} else {
b.push_back(i);
}
}
auto solve = [&](int off) {
std::vector<std::pair<int, int>> v;
for (int i = 0; i < n; i++) {
int j = (i + off) % n;
v.emplace_back(std::minmax(a[i], b[j]));
}
std::sort(v.begin(), v.end());
i64 ans = 0;
Fenwick<int> f(2 * n);
for (auto [x, y] : v) {
ans += f.rangeSum(x, y);
f.add(y, 1);
}
return ans;
};
int l = 0, r = n - 1;
while (l < r) {
int m = (l + r) / 2;
if (solve(m) < solve(m + 1)) {
l = m + 1;
} else {
r = m;
}
}
std::cout << solve(l) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
4 ms |
312 KB |
Output is correct |
35 |
Correct |
4 ms |
388 KB |
Output is correct |
36 |
Correct |
4 ms |
332 KB |
Output is correct |
37 |
Correct |
3 ms |
332 KB |
Output is correct |
38 |
Correct |
3 ms |
312 KB |
Output is correct |
39 |
Correct |
3 ms |
332 KB |
Output is correct |
40 |
Correct |
3 ms |
332 KB |
Output is correct |
41 |
Correct |
3 ms |
332 KB |
Output is correct |
42 |
Correct |
4 ms |
308 KB |
Output is correct |
43 |
Correct |
4 ms |
332 KB |
Output is correct |
44 |
Correct |
4 ms |
312 KB |
Output is correct |
45 |
Correct |
3 ms |
332 KB |
Output is correct |
46 |
Correct |
3 ms |
332 KB |
Output is correct |
47 |
Correct |
5 ms |
332 KB |
Output is correct |
48 |
Correct |
3 ms |
332 KB |
Output is correct |
49 |
Correct |
4 ms |
312 KB |
Output is correct |
50 |
Correct |
2 ms |
332 KB |
Output is correct |
51 |
Correct |
4 ms |
332 KB |
Output is correct |
52 |
Correct |
4 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
4 ms |
312 KB |
Output is correct |
35 |
Correct |
4 ms |
388 KB |
Output is correct |
36 |
Correct |
4 ms |
332 KB |
Output is correct |
37 |
Correct |
3 ms |
332 KB |
Output is correct |
38 |
Correct |
3 ms |
312 KB |
Output is correct |
39 |
Correct |
3 ms |
332 KB |
Output is correct |
40 |
Correct |
3 ms |
332 KB |
Output is correct |
41 |
Correct |
3 ms |
332 KB |
Output is correct |
42 |
Correct |
4 ms |
308 KB |
Output is correct |
43 |
Correct |
4 ms |
332 KB |
Output is correct |
44 |
Correct |
4 ms |
312 KB |
Output is correct |
45 |
Correct |
3 ms |
332 KB |
Output is correct |
46 |
Correct |
3 ms |
332 KB |
Output is correct |
47 |
Correct |
5 ms |
332 KB |
Output is correct |
48 |
Correct |
3 ms |
332 KB |
Output is correct |
49 |
Correct |
4 ms |
312 KB |
Output is correct |
50 |
Correct |
2 ms |
332 KB |
Output is correct |
51 |
Correct |
4 ms |
332 KB |
Output is correct |
52 |
Correct |
4 ms |
332 KB |
Output is correct |
53 |
Correct |
892 ms |
6296 KB |
Output is correct |
54 |
Correct |
824 ms |
6304 KB |
Output is correct |
55 |
Correct |
785 ms |
6316 KB |
Output is correct |
56 |
Correct |
840 ms |
6260 KB |
Output is correct |
57 |
Correct |
517 ms |
6192 KB |
Output is correct |
58 |
Correct |
579 ms |
6284 KB |
Output is correct |
59 |
Correct |
675 ms |
6276 KB |
Output is correct |
60 |
Correct |
671 ms |
6196 KB |
Output is correct |
61 |
Correct |
789 ms |
6208 KB |
Output is correct |
62 |
Correct |
701 ms |
6204 KB |
Output is correct |
63 |
Correct |
807 ms |
6284 KB |
Output is correct |
64 |
Correct |
657 ms |
6140 KB |
Output is correct |
65 |
Correct |
727 ms |
6016 KB |
Output is correct |
66 |
Correct |
507 ms |
6092 KB |
Output is correct |
67 |
Correct |
537 ms |
6244 KB |
Output is correct |
68 |
Correct |
681 ms |
6256 KB |
Output is correct |
69 |
Correct |
571 ms |
6400 KB |
Output is correct |
70 |
Correct |
652 ms |
5964 KB |
Output is correct |
71 |
Correct |
496 ms |
6356 KB |
Output is correct |
72 |
Correct |
799 ms |
6396 KB |
Output is correct |
73 |
Correct |
850 ms |
6400 KB |
Output is correct |