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;
#define ar array
const int N = 4e5 + 5;
struct BIT{
int tree[N];
void add(int i, int v){ i++;
for(;i<N;i+=(i&-i)) tree[i] += v;
}
int get(int i){ i++;
int rr = 0;
for(;i>0;i-=(i&-i)) rr += tree[i];
return rr;
}
int get(int l, int r){
return get(r) - get(l-1);
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
string s; cin>>s;
ar<vector<int>, 2> p;
for(int i=0;i<2*n;i++){
p[s[i] == 'B'].push_back(i);
}
auto check = [&](int k){
memset(tree.tree, 0, sizeof tree.tree);
int res = 0;
vector<ar<int, 2>> a(n);
for(int i=0;i<n;i++){
int j = (i + k) % n;
a[i] = {p[0][i], p[1][j]};
if(a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);
}
sort(a.begin(), a.end());
for(auto x : a){
res += tree.get(x[0], x[1]);
tree.add(x[1], 1);
} return res;
};
int l = 0, r = n - 1;
while(l < r){
int m = (l + r) >> 1;
if(check(m) < check(m + 1)) l = m + 1;
else r = m;
}
cout<<check(l)<<"\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... |