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 {
add(l, n - 1, x);
add(0, r, 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;
pt2 = 0;
pt3 = 0;
for (int j = 0; j < n; j++) {
while (pt < n && a[pt] < b[j]) {
pt++;
}
while (pt2 < n && b[j] >= n + a[pt2]) {
pt2++;
}
while (pt3 < n && a[pt3] < n + b[j]) {
pt3++;
}
if (0 < pt2) {
add((n - (pt2 - 1) + j) % n, (n - 0 + j) % n, -b[j]);
}
if (pt2 < pt) {
add((n - (pt - 1) + j) % n, (n - pt2 + j) % n, b[j]);
}
if (pt < pt3) {
add((n - (pt3 - 1) + j) % n, (n - pt + j) % n, -b[j]);
}
if (pt3 < n) {
add((n - (n - 1) + j) % n, (n - pt3 + 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... |