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>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=2e5+5;
const int STALA=30;
vi b,w;
char s[MAX*2];
int ans=0;
int n;
int ile(int x,int y){
if (x>y)swap(x,y);
int between=y-x-1;
return min(n*2-2-between,between);
}
int f(int shift){
int sum=0;
assert(sz(b)==sz(w) && sz(b)==n);
for (int i=0;i<n;i++){
int x=b[i];
int y=w[(i+shift)%n];
//if (shift==1)cout<<"TERAZ "<<x<<" "<<y<<" "<<ile(x,y)<<"\n";
sum+=ile(x,y);
}
return sum/2;
}
int ternary(int l,int r){
int ip=l,ik=r;
for (int i=0;i<STALA;i++){
if (abs(ik-ip)<5)break;
int dodaj=(ik-ip)/3;
int mid1=ip+dodaj;
int mid2=ik-dodaj;
if (f(mid1)<f(mid2))ip=mid1;
else ik=mid2;
}
int maxi=0;
for (int i=ip-7;i<=ik+7;i++){
if (i>=0 && i<=n-1)maxi=max(maxi,f(i));
}
return maxi;
}
int32_t main(){
BOOST;
cin>>n;
for (int i=0;i<n*2;i++){
cin>>s[i];
if (s[i]=='B')b.pb(i);
else w.pb(i);
}
if (f(0)>=f(n-1)){
int ip=0,ik=n;
while (ip+1<ik){
int mid=(ip+ik)>>1;
if (f(mid)>=f(0))ip=mid;
else ik=mid;
}
cout<<ternary(0,ip);
}
else{
int ip=-1,ik=n-1;
while (ip+1<ik){
int mid=(ip+ik)>>1;
if (f(mid)>=f(n-1))ik=mid;
else ip=mid;
}
cout<<ternary(ik,n-1);
}
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... |