#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<int> white, red;
for(int i = 1; i <= 2 * n; i++) {
char c;
cin >> c;
if(c == 'W') {
white.emplace_back(i);
}
else {
red.emplace_back(i);
}
}
if(red[0] == 1) {
swap(red, white);
}
int ans = 0;
auto solve_for = [&](int k) {
auto dist = [&](int i, int j) {
int len = abs(j - i) - 1;
return min(len, 2 * n - len - 2);
};
int cnt = 0;
for(int i = 0; i < n; i++) {
cnt += dist(white[i], red[(i + k) % n]);
}
ans = max(ans, cnt);
return cnt;
};
int L = solve_for(0), R = solve_for(n - 1);
if(L > R) {
int l = 1, r = n - 2;
while(l <= r) {
int m = (l + r) / 2;
int v1 = solve_for(m);
if(v1 <= R) {
r = m - 1;
continue;
}
int v2 = solve_for(m + 1);
if(v1 < v2) {
l = m + 2;
}
else {
r = m - 1;
}
}
}
else if(L < R) {
int l = 1, r = n - 2;
while(l <= r) {
int m = (l + r) / 2;
int v1 = solve_for(m);
if(v1 <= L) {
l = m + 1;
continue;
}
int v2 = solve_for(m + 1);
if(v1 < v2) {
l = m + 2;
}
else {
r = m - 1;
}
}
}
else {
assert(false);
}
cout << ans / 2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |