#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)>=ans){
low=mid;
}
else{
high=mid;
}
}
int ind=low;
if(val(high)>=ans){
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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
8 ms |
512 KB |
Output is correct |
35 |
Correct |
7 ms |
512 KB |
Output is correct |
36 |
Correct |
8 ms |
512 KB |
Output is correct |
37 |
Correct |
7 ms |
512 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
8 ms |
512 KB |
Output is correct |
40 |
Correct |
7 ms |
512 KB |
Output is correct |
41 |
Correct |
7 ms |
512 KB |
Output is correct |
42 |
Correct |
8 ms |
512 KB |
Output is correct |
43 |
Correct |
6 ms |
512 KB |
Output is correct |
44 |
Correct |
8 ms |
512 KB |
Output is correct |
45 |
Correct |
6 ms |
512 KB |
Output is correct |
46 |
Correct |
6 ms |
512 KB |
Output is correct |
47 |
Correct |
6 ms |
512 KB |
Output is correct |
48 |
Correct |
7 ms |
512 KB |
Output is correct |
49 |
Correct |
7 ms |
512 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
7 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
8 ms |
512 KB |
Output is correct |
35 |
Correct |
7 ms |
512 KB |
Output is correct |
36 |
Correct |
8 ms |
512 KB |
Output is correct |
37 |
Correct |
7 ms |
512 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
8 ms |
512 KB |
Output is correct |
40 |
Correct |
7 ms |
512 KB |
Output is correct |
41 |
Correct |
7 ms |
512 KB |
Output is correct |
42 |
Correct |
8 ms |
512 KB |
Output is correct |
43 |
Correct |
6 ms |
512 KB |
Output is correct |
44 |
Correct |
8 ms |
512 KB |
Output is correct |
45 |
Correct |
6 ms |
512 KB |
Output is correct |
46 |
Correct |
6 ms |
512 KB |
Output is correct |
47 |
Correct |
6 ms |
512 KB |
Output is correct |
48 |
Correct |
7 ms |
512 KB |
Output is correct |
49 |
Correct |
7 ms |
512 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
7 ms |
512 KB |
Output is correct |
53 |
Correct |
1282 ms |
18756 KB |
Output is correct |
54 |
Correct |
1244 ms |
18744 KB |
Output is correct |
55 |
Correct |
1200 ms |
18116 KB |
Output is correct |
56 |
Correct |
1213 ms |
18264 KB |
Output is correct |
57 |
Correct |
1150 ms |
18272 KB |
Output is correct |
58 |
Correct |
1169 ms |
18700 KB |
Output is correct |
59 |
Correct |
1099 ms |
18196 KB |
Output is correct |
60 |
Correct |
1139 ms |
18320 KB |
Output is correct |
61 |
Correct |
1169 ms |
18568 KB |
Output is correct |
62 |
Correct |
1185 ms |
18752 KB |
Output is correct |
63 |
Correct |
1153 ms |
18252 KB |
Output is correct |
64 |
Correct |
1067 ms |
18436 KB |
Output is correct |
65 |
Correct |
1115 ms |
17872 KB |
Output is correct |
66 |
Correct |
1004 ms |
18112 KB |
Output is correct |
67 |
Correct |
998 ms |
18096 KB |
Output is correct |
68 |
Correct |
945 ms |
17420 KB |
Output is correct |
69 |
Correct |
1196 ms |
18884 KB |
Output is correct |
70 |
Correct |
1040 ms |
17600 KB |
Output is correct |
71 |
Correct |
457 ms |
18680 KB |
Output is correct |
72 |
Correct |
459 ms |
18668 KB |
Output is correct |
73 |
Correct |
1150 ms |
18824 KB |
Output is correct |