#include <bits/stdc++.h>
using i64 = long long;
int main() {
int N;
std::cin >> N;
std::string S;
std::cin >> S;
std::vector<int> blackPos, whitePos;
for (int i = 0; i < 2 * N; ++i) {
if (S[i] == 'B') blackPos.push_back(i);
if (S[i] == 'W') whitePos.push_back(i);
}
for (int i = 0; i < 3 * N; ++i) blackPos.push_back(blackPos[i] + 2 * N);
for (int i = 0; i < 3 * N; ++i) whitePos.push_back(whitePos[i] + 2 * N);
auto calc = [&](const int w) {
// blackPos[i] - whitePos[i + w]
std::vector<std::pair<int, int>> ps;
for (int i = 0; i < N; ++i) {
int a = blackPos[i], b = whitePos[i + w];
while (b >= 2 * N) b -= 2 * N;
ps.push_back({a, b});
}
i64 cnt = 0;
std::vector<int> ids;
for (int i = 0; i < N; ++i) {
if (blackPos[i] > whitePos[i + w]) {
ids.push_back(i);
std::swap(blackPos[i], whitePos[i + w]);
std::swap(blackPos[i + N], whitePos[i + N + w]);
std::swap(blackPos[i + 2 * N], whitePos[i + 2 * N + w]);
}
}
for (int i = 0; i < N; ++i) {
int b = i + 1;
while (b != i and blackPos[b] < whitePos[i + w] and whitePos[b + w] < blackPos[i] + 2 * N) {
++b;
}
cnt += b - i - 1;
}
for (const int i : ids) {
std::swap(blackPos[i], whitePos[i + w]);
std::swap(blackPos[i + N], whitePos[i + N + w]);
std::swap(blackPos[i + 2 * N], whitePos[i + 2 * N + w]);
}
return cnt;
};
int l = -1, r = N;
while (r - l > 2) {
int x1 = (2 * l + r) / 3, x2 = (l + 2 * r) / 3;
const auto v1 = calc(x1), v2 = calc(x2);
if (v1 > v2) r = x2;
else l = x1;
}
std::cout << calc((l + r) / 2) << std::endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
2 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
296 KB |
Output is correct |
27 |
Correct |
2 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
2 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
2 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
296 KB |
Output is correct |
27 |
Correct |
2 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
2 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
300 KB |
Output is correct |
34 |
Correct |
81 ms |
340 KB |
Output is correct |
35 |
Correct |
43 ms |
340 KB |
Output is correct |
36 |
Correct |
43 ms |
340 KB |
Output is correct |
37 |
Correct |
42 ms |
340 KB |
Output is correct |
38 |
Correct |
39 ms |
340 KB |
Output is correct |
39 |
Correct |
44 ms |
340 KB |
Output is correct |
40 |
Correct |
52 ms |
384 KB |
Output is correct |
41 |
Correct |
33 ms |
340 KB |
Output is correct |
42 |
Correct |
44 ms |
340 KB |
Output is correct |
43 |
Correct |
47 ms |
396 KB |
Output is correct |
44 |
Correct |
49 ms |
340 KB |
Output is correct |
45 |
Correct |
38 ms |
380 KB |
Output is correct |
46 |
Correct |
78 ms |
340 KB |
Output is correct |
47 |
Correct |
50 ms |
396 KB |
Output is correct |
48 |
Correct |
44 ms |
400 KB |
Output is correct |
49 |
Correct |
35 ms |
340 KB |
Output is correct |
50 |
Correct |
48 ms |
340 KB |
Output is correct |
51 |
Correct |
70 ms |
400 KB |
Output is correct |
52 |
Correct |
43 ms |
400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
2 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
296 KB |
Output is correct |
27 |
Correct |
2 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
2 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
300 KB |
Output is correct |
34 |
Correct |
81 ms |
340 KB |
Output is correct |
35 |
Correct |
43 ms |
340 KB |
Output is correct |
36 |
Correct |
43 ms |
340 KB |
Output is correct |
37 |
Correct |
42 ms |
340 KB |
Output is correct |
38 |
Correct |
39 ms |
340 KB |
Output is correct |
39 |
Correct |
44 ms |
340 KB |
Output is correct |
40 |
Correct |
52 ms |
384 KB |
Output is correct |
41 |
Correct |
33 ms |
340 KB |
Output is correct |
42 |
Correct |
44 ms |
340 KB |
Output is correct |
43 |
Correct |
47 ms |
396 KB |
Output is correct |
44 |
Correct |
49 ms |
340 KB |
Output is correct |
45 |
Correct |
38 ms |
380 KB |
Output is correct |
46 |
Correct |
78 ms |
340 KB |
Output is correct |
47 |
Correct |
50 ms |
396 KB |
Output is correct |
48 |
Correct |
44 ms |
400 KB |
Output is correct |
49 |
Correct |
35 ms |
340 KB |
Output is correct |
50 |
Correct |
48 ms |
340 KB |
Output is correct |
51 |
Correct |
70 ms |
400 KB |
Output is correct |
52 |
Correct |
43 ms |
400 KB |
Output is correct |
53 |
Execution timed out |
2045 ms |
10960 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |