#include <bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define all(x) x.begin(), x.end()
#define sz(a) ((int) a.size())
#define pb push_back
#define fst first
#define snd second
using namespace std;
typedef long long ll;
const int INF=1e9;
int n,m;
int ans=0;
string s;
int calc(vector<pair<int,int>>&p){
int cur=0;
L(i,0,n-1){
for(int j=i+1;j<n;j++){
int a=p[i].fst,b=p[i].snd;
int c=p[j].fst,d=p[j].snd;
bool caso1=(b>c&&a>c&&d>a&&d<b),caso2=(a<d&&a<c&&b>c&&b<d);
if(caso1||caso2){
cur++;
}
}
}
return cur;
}
vector<int>b,w;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
cin>>s;
m=n*2;
L(i,0,m-1){
if(s[i]=='B')b.pb(i);
else w.pb(i);
}
sort(all(b));
sort(all(w));
L(i,0,sz(w)-1){
vector<pair<int,int>>p;
int j=0,cur=i;
int aa=b[j++],bb=w[cur];
if(aa>bb)swap(aa,bb);
p.pb({aa,bb});
L(pasos,1,n-1){
cur++;
cur%=n;
int aa_=b[j++],bb_=w[cur];
if(aa_>bb_)swap(aa_,bb_);
p.pb({aa_,bb_});
}
//~ cout<<sz(p)<<endl;
//~ L(i,0,sz(p)-1){
//~ cout<<p[i].fst+1<<" "<<p[i].snd+1<<endl;
//~ }
ans=max(ans,calc(p));
}
cout<<ans<<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... |