Submission #293453

#TimeUsernameProblemLanguageResultExecution timeMemory
293453kshitij_sodaniMonochrome Points (JOI20_monochrome)C++14
100 / 100
1249 ms18880 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
//#define endl '\n' 
 
 
int n;
vector<int> aa;
vector<int> bb;
bool sect(int cc,int dd,int ee,int ff){
	if(cc>dd){
		swap(cc,dd);
	}
	if(ee>ff){
		swap(ee,ff);
	}
	if(cc>ee){
		swap(cc,ee);
		swap(dd,ff);
	}
 
 
	if(cc<=ee and ee<=dd and dd<=ff){
		return true;
	}
 
	return false;
}
int tree[400001];
int vis[400001];
void u(int i,int j){
	while(i<400001){
		tree[i]+=j;
		i+=(i&-i);
	}
}
int stt(int i){
	int su=0;
	while(i>0){
		su+=tree[i];
		i-=(i&-i);
	}
	return su;
}
llo vis2[200001];
pair<int,int> sto[400001];
llo val(int ii){
	if(vis2[ii]>-1){
		return vis2[ii];
	}
	vector<int> ss;
	for(int i=ii;i<n;i++){
		ss.pb(bb[i]);
	}
	for(int i=0;i<ii;i++){
		ss.pb(bb[i]);
	}
	llo co=0;
	vector<pair<int,int>> mm;
	for(int i=0;i<n;i++){
	//	mm.pb({aa[i],i});
	//	mm.pb({ss[i],i});
		sto[aa[i]]={aa[i],i};
		sto[ss[i]]={ss[i],i};
	}
	for(int i=0;i<2*n;i++){
		mm.pb(sto[i]);
	}
	for(int j=0;j<2*n;j++){
		tree[j+1]=0;
		vis[j]=0;
	}
//	sort(mm.begin(),mm.end());
	for(auto j:mm){
		if(vis[j.b]){
			co+=stt(j.a+1)-stt(vis[j.b]+1);
			u(vis[j.b]+1,-1);
		}
		else{
			vis[j.b]=j.a;
			u(j.a+1,1);
		}
	}
	vis2[ii]=co;
	return co;
}
llo cal(int  x,int y){
	int low=x;
	int high=y;
	while(low<high-1){
		int mid=(low+high)/2;
		int xx=val(mid);
		int yy=val(mid+1);
		if(xx<yy){
			low=mid+1;
		}
		else{
			high=mid;
		}
	}
	llo ma=0;
	for(int i=low;i<=high;i++){
		ma=max(ma,val(i));
	}
 
 
 
 
 
	return ma;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
		for(int i=0;i<n;i++){
			vis2[i]=-1;
		}
 
	for(int i=0;i<2*n;i++){
		char cc;
		cin>>cc;
		if(cc=='B'){
			aa.pb(i);
		}	
		else{
			bb.pb(i);
		}
	}
	/*if(n>10){
		return 0;
	}*/
	vector<int> ss;
	for(auto j:bb){
		ss.pb(j);
	}
	llo ans=0;
	/*vector<int> pp;
	for(int jj=0;jj<n;jj++){
		ans=max(ans,val(jj));
	}*/
	ans=val(n-1);
	llo xx=val(0);
	llo ans2=0;
	
 
	if(xx<ans){
		int low=0;
		int high=n-1;
		while(low<high-1){
			int mid=(low+high)/2;
			if(val(mid)<ans){
				low=mid+1;
			}
			else{
				high=mid;
			}
		}
		int ind=high;
		if(val(low)>=ans){
			ind=min(ind,low);
		}
		ans2=max(ans2,cal(ind,n-1));
/*	for(int j=ind;j<n;j++){
		ans=max(ans,val(j));
	}*/
   	 }
   	 if(xx>=ans){
   	 	int low=0;
		int high=n-1;
		while(low<high-1){
			int mid=(low+high)/2;
			if(val(mid)>=xx){
				low=mid;
			}
			else{
				high=mid;
			}
		}
		int ind=low;
		if(val(high)>=xx){
			ind=max(ind,high);
		}
	/*	for(int i=0;i<=ind;i++){
			ans=max(ans,val(i));
		}*/
		ans2=max(ans2,(cal(0,ind)));
   	 }
 
 
	/*int pop=0;
	for(int ii=0;ii<n;ii++){
		vector<int> tt;
		for(int j=ii;j<n;j++){
			tt.pb(pp[j]);
		}
		for(int j=0;j<ii;j++){
			tt.pb(pp[j]);
		}
		int kk=1;
		for(int i=1;i<n-1;i++){
			if(tt[i]<tt[i+1] and tt[i]<tt[i-1]){
				kk=0;
			}
		}
		for(int i=0;i<n;i++){
			if(tt[i]==tt[(i+1)%n]){
				kk=0;
			}
		}
 
		pop=max(pop,kk);
 
 
	}*/
/*	if(pop==0){
		while(true){
			continue;
		}
	}*/
	cout<<ans2<<endl;
	/*for(auto j:pp){
		cout<<j<<",";
	}
	cout<<endl;*/
 
 
 
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...