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 mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
int dist(int a, int b){
return min(abs(a-b), abs(a-b+n));
}
int bp[400000];
int wp[400000];
int br(int l, int r){
if(l>r) return 0;
if(l==0) return bp[r];
else return bp[r] - bp[l-1];
}
int wr(int l, int r){
if(l>r) return 0;
if(l==0) return wp[r];
else return wp[r] - wp[l-1];
}
set<int> w, b;
ll gans(int off){
auto it = b.begin();
FOR(i,off) it++;
ll ans=0;
FOA(v,w){
//cout<<v<<" "<<*it<<endl;
int mi = min(v, *it);
int ma = max(v, *it);
ans += min(br(mi+1,ma-1) , (wr(0,mi-1) + wr(ma+1,n-1))) + min(br(0,mi-1) + br(ma+1,n-1) , (wr(mi+1,ma-1)));
//cout<<v<<" "<<*it<<" "<<ans<<endl;
it++;
if(it==b.end()) it = b.begin();
}
return ans;
}
int main(){
cin>>n;
string s;
cin>>s;
n*=2;
bp[0] = (s[0]=='B');
FORi(i,1,n){
bp[i] = bp[i-1];
if(s[i]=='B') bp[i]++;
}
wp[0] = (s[0]=='W');
FORi(i,1,n){
wp[i] = wp[i-1];
if(s[i]=='W') wp[i]++;
}
FOR(i,n){
if(s[i]=='W') w.insert(i);
else b.insert(i);
}
ll bans=0;
int l=0, r=n/2+1;
while(r-l>2){
int m1 = l + (r-l)/3;
int m2 = r - (r-l)/3;
ll a1 = gans(m1);
bans = max(bans, a1);
ll a2 = gans(m2);
bans = max(bans, a2);
if(a1<=a2){
l = m1;
}
else{
r = m2;
}
}
FORi(x,l,r+1) bans = max(bans, gans(x));
cout<<bans/2<<endl;
}
# | 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... |