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 <algorithm>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> W, B;
W.reserve(N);
B.reserve(N);
for (int i = 0; i < 2 * N; ++i) {
char c;
cin >> c;
(c == 'W' ? W : B).push_back(i);
}
auto f = [&](int k) {
ll ret = 0;
for (int i = 0; i < N; ++i) {
int j = i + k;
if (j >= N) j -= N;
int d = abs(W[i] - B[j]);
ret += min(d - 1, 2 * N - 1 - d);
}
return ret / 2;
};
int l = 0, r = N - 1;
while (r - l > 5) {
const int x1 = (l + l + r) / 3;
const int x2 = (l + r + r) / 3;
if (f(x1) < f(x2)) {
l = x1;
} else {
r = x2;
}
}
ll ans = 0;
for (int k = l; k <= r; ++k) {
ans = max(ans, f(k));
}
cout << ans << '\n';
return 0;
}
# | 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... |