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;
// finding max distance
int main() {
//freopen("input.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
vector<int> black, white;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'B') {
black.push_back(i);
}
else {
white.push_back(i);
}
}
auto solve = [&](int k) {
long long res = 0;
for (int i = 0; i < n; i++) {
int dist = abs(black[i] - white[(i + k) % n]);
res += min(dist, 2 * n - dist);
}
return res;
};
auto findMax = [&](int l, int r) {
int lower = l, upper = r;
while (lower < upper) {
int mid = (lower + upper) / 2;
if (solve(mid) <= solve(mid + 1)) {
lower = mid + 1;
}
else upper = mid;
}
return solve(lower);
};
long long res;
if (solve(0) >= solve(n - 1)) {
// up - down - up
// find last i: solve(0) <= solve(i)
int s0 = solve(0);
int lower = 0, upper = n - 1;
while (lower < upper) {
int mid = (lower + upper + 1) / 2;
if (solve(mid) >= s0) {
lower = mid;
}
else upper = mid - 1;
}
res = findMax(0, lower);
}
else {
// down - up - down
// find the first i: solve(i) >= solve(n - 1)
int sn = solve(n - 1);
int lower = 0, upper = n - 1;
while (lower < upper) {
int mid = (lower + upper) / 2;
if (solve(mid) >= sn) {
upper = mid;
}
else lower = mid + 1;
}
res = findMax(lower, n - 1);
}
cout << (res - n) / 2 << endl;
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... |