#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 time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 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 |
512 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 |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 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 |
512 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 |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
8 ms |
512 KB |
Output is correct |
35 |
Correct |
8 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 |
6 ms |
512 KB |
Output is correct |
39 |
Correct |
7 ms |
512 KB |
Output is correct |
40 |
Correct |
7 ms |
512 KB |
Output is correct |
41 |
Correct |
6 ms |
512 KB |
Output is correct |
42 |
Correct |
7 ms |
512 KB |
Output is correct |
43 |
Correct |
6 ms |
512 KB |
Output is correct |
44 |
Correct |
7 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 |
9 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 |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 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 |
512 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 |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
8 ms |
512 KB |
Output is correct |
35 |
Correct |
8 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 |
6 ms |
512 KB |
Output is correct |
39 |
Correct |
7 ms |
512 KB |
Output is correct |
40 |
Correct |
7 ms |
512 KB |
Output is correct |
41 |
Correct |
6 ms |
512 KB |
Output is correct |
42 |
Correct |
7 ms |
512 KB |
Output is correct |
43 |
Correct |
6 ms |
512 KB |
Output is correct |
44 |
Correct |
7 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 |
9 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 |
1242 ms |
18792 KB |
Output is correct |
54 |
Correct |
1249 ms |
18640 KB |
Output is correct |
55 |
Correct |
1193 ms |
18040 KB |
Output is correct |
56 |
Correct |
1203 ms |
18116 KB |
Output is correct |
57 |
Correct |
1133 ms |
18340 KB |
Output is correct |
58 |
Correct |
1183 ms |
18688 KB |
Output is correct |
59 |
Correct |
1097 ms |
18256 KB |
Output is correct |
60 |
Correct |
1117 ms |
18124 KB |
Output is correct |
61 |
Correct |
1217 ms |
18264 KB |
Output is correct |
62 |
Correct |
1182 ms |
18364 KB |
Output is correct |
63 |
Correct |
1161 ms |
18356 KB |
Output is correct |
64 |
Correct |
1038 ms |
18312 KB |
Output is correct |
65 |
Correct |
1116 ms |
17792 KB |
Output is correct |
66 |
Correct |
983 ms |
18092 KB |
Output is correct |
67 |
Correct |
1010 ms |
17956 KB |
Output is correct |
68 |
Correct |
939 ms |
17536 KB |
Output is correct |
69 |
Correct |
1147 ms |
18844 KB |
Output is correct |
70 |
Correct |
1032 ms |
17580 KB |
Output is correct |
71 |
Correct |
429 ms |
18636 KB |
Output is correct |
72 |
Correct |
438 ms |
18624 KB |
Output is correct |
73 |
Correct |
1140 ms |
18880 KB |
Output is correct |