#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;
};
i64 answer = 0;
for (int i = 0; i < N; ++i) answer = std::max(answer, calc(i));
std::cout << answer << std::endl;
return 0;
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 |
1 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 |
0 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 |
1 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 |
0 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 |
8 ms |
212 KB |
Output is correct |
15 |
Correct |
8 ms |
324 KB |
Output is correct |
16 |
Correct |
8 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
212 KB |
Output is correct |
19 |
Correct |
6 ms |
300 KB |
Output is correct |
20 |
Correct |
6 ms |
212 KB |
Output is correct |
21 |
Correct |
8 ms |
300 KB |
Output is correct |
22 |
Correct |
7 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
5 ms |
300 KB |
Output is correct |
26 |
Correct |
7 ms |
212 KB |
Output is correct |
27 |
Correct |
8 ms |
300 KB |
Output is correct |
28 |
Correct |
6 ms |
212 KB |
Output is correct |
29 |
Correct |
7 ms |
212 KB |
Output is correct |
30 |
Correct |
8 ms |
212 KB |
Output is correct |
31 |
Correct |
5 ms |
212 KB |
Output is correct |
32 |
Correct |
11 ms |
212 KB |
Output is correct |
33 |
Correct |
9 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
8 ms |
212 KB |
Output is correct |
15 |
Correct |
8 ms |
324 KB |
Output is correct |
16 |
Correct |
8 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
212 KB |
Output is correct |
19 |
Correct |
6 ms |
300 KB |
Output is correct |
20 |
Correct |
6 ms |
212 KB |
Output is correct |
21 |
Correct |
8 ms |
300 KB |
Output is correct |
22 |
Correct |
7 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
5 ms |
300 KB |
Output is correct |
26 |
Correct |
7 ms |
212 KB |
Output is correct |
27 |
Correct |
8 ms |
300 KB |
Output is correct |
28 |
Correct |
6 ms |
212 KB |
Output is correct |
29 |
Correct |
7 ms |
212 KB |
Output is correct |
30 |
Correct |
8 ms |
212 KB |
Output is correct |
31 |
Correct |
5 ms |
212 KB |
Output is correct |
32 |
Correct |
11 ms |
212 KB |
Output is correct |
33 |
Correct |
9 ms |
300 KB |
Output is correct |
34 |
Correct |
1529 ms |
400 KB |
Output is correct |
35 |
Correct |
1538 ms |
412 KB |
Output is correct |
36 |
Correct |
1395 ms |
392 KB |
Output is correct |
37 |
Correct |
1258 ms |
392 KB |
Output is correct |
38 |
Correct |
1147 ms |
392 KB |
Output is correct |
39 |
Correct |
1382 ms |
460 KB |
Output is correct |
40 |
Correct |
1112 ms |
384 KB |
Output is correct |
41 |
Correct |
1012 ms |
400 KB |
Output is correct |
42 |
Correct |
1523 ms |
396 KB |
Output is correct |
43 |
Correct |
1787 ms |
396 KB |
Output is correct |
44 |
Correct |
1329 ms |
392 KB |
Output is correct |
45 |
Correct |
1054 ms |
388 KB |
Output is correct |
46 |
Correct |
1237 ms |
388 KB |
Output is correct |
47 |
Correct |
1934 ms |
392 KB |
Output is correct |
48 |
Correct |
1637 ms |
392 KB |
Output is correct |
49 |
Correct |
1189 ms |
384 KB |
Output is correct |
50 |
Correct |
1004 ms |
388 KB |
Output is correct |
51 |
Execution timed out |
2035 ms |
388 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
8 ms |
212 KB |
Output is correct |
15 |
Correct |
8 ms |
324 KB |
Output is correct |
16 |
Correct |
8 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
212 KB |
Output is correct |
19 |
Correct |
6 ms |
300 KB |
Output is correct |
20 |
Correct |
6 ms |
212 KB |
Output is correct |
21 |
Correct |
8 ms |
300 KB |
Output is correct |
22 |
Correct |
7 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
5 ms |
300 KB |
Output is correct |
26 |
Correct |
7 ms |
212 KB |
Output is correct |
27 |
Correct |
8 ms |
300 KB |
Output is correct |
28 |
Correct |
6 ms |
212 KB |
Output is correct |
29 |
Correct |
7 ms |
212 KB |
Output is correct |
30 |
Correct |
8 ms |
212 KB |
Output is correct |
31 |
Correct |
5 ms |
212 KB |
Output is correct |
32 |
Correct |
11 ms |
212 KB |
Output is correct |
33 |
Correct |
9 ms |
300 KB |
Output is correct |
34 |
Correct |
1529 ms |
400 KB |
Output is correct |
35 |
Correct |
1538 ms |
412 KB |
Output is correct |
36 |
Correct |
1395 ms |
392 KB |
Output is correct |
37 |
Correct |
1258 ms |
392 KB |
Output is correct |
38 |
Correct |
1147 ms |
392 KB |
Output is correct |
39 |
Correct |
1382 ms |
460 KB |
Output is correct |
40 |
Correct |
1112 ms |
384 KB |
Output is correct |
41 |
Correct |
1012 ms |
400 KB |
Output is correct |
42 |
Correct |
1523 ms |
396 KB |
Output is correct |
43 |
Correct |
1787 ms |
396 KB |
Output is correct |
44 |
Correct |
1329 ms |
392 KB |
Output is correct |
45 |
Correct |
1054 ms |
388 KB |
Output is correct |
46 |
Correct |
1237 ms |
388 KB |
Output is correct |
47 |
Correct |
1934 ms |
392 KB |
Output is correct |
48 |
Correct |
1637 ms |
392 KB |
Output is correct |
49 |
Correct |
1189 ms |
384 KB |
Output is correct |
50 |
Correct |
1004 ms |
388 KB |
Output is correct |
51 |
Execution timed out |
2035 ms |
388 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |