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;
using ll = long long;
using ld = long double;
using pi = pair<pair<int, int>, pair<int, int>>;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
int N, ST[200005]; string S;
vector<int> A, B;
void update(int p, int v) {
for(; p <= 2 * N; p += (p & (-p)))
ST[p] += v;
}
int query(int p) {
int res = 0;
for(; p; p -= (p & (-p)))
res += ST[p];
return res;
}
int query(int lo, int hi) {
return query(hi) - query(lo - 1);
}
int solve(int v) {
vector<pair<int, int>> P;
for(int l = 0; l < N; l++)
P.push_back({A[l], B[(l + v) % N]});
int arr[2 * N]{0};
for(int l = 0; l < N; l++) {
arr[A[l]] = l, arr[B[(l + v) % N]] = l;
}
for(int l = 0; l <= 2 * N; l++) ST[l] = 0;
int res = 0; unordered_map<int, int> M;
for(int l = 0; l < 2 * N; l++) {
if(M.find(arr[l]) != M.end()) {
int i = M[arr[l]];
update(i, -1); res += query(i, l + 1);
continue;
}
update(l + 1, 1); M[arr[l]] = l + 1;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> S; int res = 0;
for(int l = 0; l < 2 * N; l++) {
if(S[l] == 'B') A.push_back(l);
if(S[l] == 'W') B.push_back(l);
}
int lo = 0, hi = N - 1;
while(lo <= hi) {
int md = (lo + hi) / 2;
int U = solve(md), V = solve(md + 1);
if(V > U) { lo = md + 1, res = max(res, V); }
else hi = md - 1, res = max(res, U);
}
cout << res << "\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... |