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;
const int N=400001;
int n;
string s;
int r[N], l[N];
int sum[4*N];
void upd(int i, int x, int lx, int rx){
sum[x]++;
if(rx-lx==1) return;
int m=(lx+rx)/2;
if(i<m) upd(i, 2*x+1, lx, m);
else upd(i, 2*x+2, m, rx);
}
void upd(int i) { upd(i, 0, 0, 2*n+1); }
int get_sum(int l, int r, int x, int lx, int rx){
if(lx>=r || rx<=l) return 0;
if(lx>=l && rx<=r) return sum[x];
int m=(lx+rx)/2;
return get_sum(l, r, 2*x+1, lx, m)+get_sum(l, r, 2*x+2, m, rx);
}
int get_sum(int l, int r) {
if(r<=l) return 0;
return get_sum(l, r, 0, 0, 2*n+1);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
long long ans2=0;
for(int t=0; t<n; t++){
vector<int> white, black;
for(int i=n; i<2*n; i++){
if(s[i]=='W') white.push_back(i+1);
else black.push_back(i+1);
}
int j=0, k=0;
for(int i=0; i<n; i++){
if(s[i]=='B') {
l[white[j]]=i+1, r[i+1]=white[j];
j++;
}
else{
l[black[k]]=i+1, r[i+1]=black[k];
k++;
}
}
// for(int i=0; i<n; i++){
// cout << "r[" << i+1 << "] = " << r[i+1] << "\n";
// }
for(int i=0; i<4*n; i++) sum[i]=0;
long long ans=0;
for(int i=1; i<=2*n; i++){
if(r[i]) continue;
ans+=get_sum(l[i]+1, i);
upd(l[i]);
}
ans2=max(ans2, (long long)n*(n-1)/2-ans);
s+=s[0];
s.erase(s.begin());
}
cout << ans2;
}
# | 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... |