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>
#pragma GCC optimize("O3")
#define maxn 1000000
using namespace std;
int n;
long long ans=0;
char s[2*maxn];
int d[2*maxn];
int seg[8*maxn];
void update(int id,int l,int r,int p,int v) {
if(l==r) {
seg[id]=v;
return;
}
int m=(l+r)>>1;
if(p<=m) update(id*2+1,l,m,p,v);
else update(id*2+2,m+1,r,p,v);
seg[id]=seg[id*2+1]+seg[id*2+2];
}
int query(int id,int l,int r,int x,int y) {
if(x<=l && r<=y) return seg[id];
if(r<x || y<l) return 0;
int m=(l+r)>>1;
return query(id*2+1,l,m,x,y)+query(id*2+2,m+1,r,x,y);
}
int l[maxn];
int r[maxn];
int main() {
scanf("%d",&n);
scanf("%s",s);
if(s[0]=='B') {
for(int i=0;i<2*n;i++) {
if(s[i]=='B') s[i]='W';
else s[i]='B';
}
}
for(int i=0;i<n;i++) {
int cb=0;
int x=0;
int y=0;
long long cans=0;
int tmp=0;
while(cb<i || s[y]!='B') {
if(s[y]=='B') cb++;
y++;
if(y==2*n) y=0;
}
int k=1;
l[k]=x;
r[k]=y;
d[x]=-k;
d[y]=k;
x++;
while(x<2*n) {
if(s[x]=='W') {
k++;
do {
y++;
if(y==2*n) y=0;
} while(s[y]!='B');
l[k]=min(x,y);
r[k]=max(x,y);
d[min(x,y)]=-k;
d[max(x,y)]=k;
}
x++;
}
for(int i=0;i<2*n;i++) {
if(d[i]>0) {
update(0,0,2*n-1,l[d[i]],0);
cans+=query(0,0,2*n-1,l[d[i]],r[d[i]]);
}
else update(0,0,2*n-1,i,1);
}
ans=max(ans,cans);
}
printf("%lld",ans);
return 0;
}
Compilation message (stderr)
monochrome.cpp: In function 'int main()':
monochrome.cpp:42:13: warning: unused variable 'tmp' [-Wunused-variable]
42 | int tmp=0;
| ^~~
monochrome.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
29 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
monochrome.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
30 | scanf("%s",s);
| ~~~~~^~~~~~~~
# | 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... |