This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 {
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;
}
}
}
cout << ans / 2 << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |