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;
vi a, b;
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];
}
int main(){
cin>>n;
string s;
cin>>s;
n*=2;
set<int> w, b;
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);
}
vi c(n);
int bans=0;
FORi(off,0, n/2+1){
auto it = b.begin();
FOR(i,off) it++;
int ans=0;
FOA(v,w){
c[v] = *it;
c[*it] = v;
//cout<<v<<" "<<*it<<endl;
// ans = min(b in range + w in other side, w in range + b in other side)
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();
}
//cout<<ans<<endl;
/*
FOR(i,n){
if(s[i]=='B')
FORi(j,min(i,c[i])+1, max(i,c[i])){
if(c[j]>max(i,c[i]) || c[j]<min(i,c[i])) ans++;
}
}*/
bans = max(bans, ans);
}
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... |