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;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N;
string S;
cin >> N >> S;
vector<int> mismatch_white;
vector<int> mismatch_black;
for (int i = 0; i < N; i++) {
if (S[i] == S[i + N]) {
if (S[i] == 'W') {
mismatch_white.emplace_back(i);
} else {
mismatch_black.emplace_back(i);
}
}
}
auto Solve = [&](int shift) {
long long res = 0;
for (int i = 0; i < (int) mismatch_black.size(); i++) {
res += abs(mismatch_black[i] - mismatch_white[i + shift]);
}
return res;
};
int mismatch = mismatch_white.size();
for (int add = 1; add <= 2; add++) {
for (int i = 0; i < mismatch; i++) {
mismatch_white.emplace_back(mismatch_white[i]);
}
}
for (int i = 0; i < mismatch; i++) {
mismatch_white[i] -= N;
mismatch_white[i + 2 * mismatch] += N;
}
int lo = 0, hi = 2 * mismatch;
while (lo < hi) {
int md = (lo + hi) / 2;
if (Solve(md) < Solve(md + 1)) {
hi = md;
} else {
lo = md + 1;
}
}
cout << (1ll * N * (N - 1) / 2 - Solve(lo)) << "\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... |