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;
typedef long long ll;
ll solve(string s) {
int n = (int) s.size() / 2;
vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] != s[0]) {
a.push_back(i);
}
else {
b.push_back(i);
}
}
vector<ll> cnt(n, -n);
function<void(int, int, int)> add = [&] (int l, int r, int x) {
if (l <= r) {
for (int j = l; j <= r; j++) {
cnt[j] += x;
}
} else {
for (int j = l; j < n; j++) {
cnt[j] += x;
}
for (int j = 0; j <= r; j++) {
cnt[j] += x;
}
}
};
int pt = 0, pt2 = 0, pt3 = 0;
for (int i = 0; i < n; i++) {
while (pt < n && b[pt] < a[i]) {
pt++;
}
while (pt2 < n && a[i] >= n + b[pt2]) {
pt2++;
}
while (pt3 < n && b[pt3] < n + a[i]) {
pt3++;
}
if (pt < pt3) {
add((n - i + pt) % n, (n - i + pt3 - 1) % n, -a[i]);
}
if (pt3 < n) {
add((n - i + pt3) % n, (n - i + n - 1) % n, 2 * n + a[i]);
}
if (0 < pt2) {
add((n - i) % n, (n - i + pt2 - 1) % n, 2 * n - a[i]);
}
if (pt2 < pt) {
add((n - i + pt2) % n, (n - i + pt - 1) % n, a[i]);
}
}
pt = 0;
for (int j = 0; j < n; j++) {
while (pt < n && a[pt] < b[j]) {
pt++;
}
for (int i = 0; i < pt; i++) {
if (b[j] < n + a[i]) {
cnt[(n - i + j) % n] += b[j];
} else {
cnt[(n - i + j) % n] -= b[j];
}
}
for (int i = pt; i < n; i++) {
if (a[i] < n + b[j]) {
cnt[(n - i + j) % n] -= b[j];
} else {
cnt[(n - i + j) % n] += b[j];
}
}
}
ll sol = 0;
for (auto &x : cnt) {
sol = max(sol, x);
}
return sol /2;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
if (0) {
assert(solve("WWWBWBBBBWWBWWBWWBBWWBBBWBBBWWBW") == 105);
assert(solve("WBBBWBBWWBWWBWWBWBWB") == 41);
assert(solve("BWBWBBWBWW") == 8);
assert(solve("BBWWBW") == 2);
cout << "ok!\n";
exit(0);
}
int n;
string s;
cin >> n >> s;
cout << solve(s) << "\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... |