# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
301913 | milisav | Monochrome Points (JOI20_monochrome) | C++14 | 767 ms | 5640 KiB |
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 bit[8*maxn];
void update(int p,int v) {
while(p<=2*n) {
bit[p]+=v;
p+=(p&(-p));
}
}
int query(int p) {
int ret=0;
while(p>0) {
ret+=bit[p];
p-=(p&(-p));
}
return ret;
}
int l[maxn];
int r[maxn];
long long val(int 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(l[d[i]]+1,-1);
cans+=query(r[d[i]]+1)-query(l[d[i]]+1);
}
else update(i+1,1);
}
return cans;
}
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';
}
}
int l=0,r=n-1;
while(l<r) {
int m1=(l+r)>>1;
int m2=m1+1;
long long v1=val(m1);
long long v2=val(m2);
ans=max(ans,v1);
ans=max(ans,v2);
if(v1>v2) r=m2-1;
else l=m1+1;
}
ans=max(ans,val(l));
printf("%lld",ans);
return 0;
}
Compilation message (stderr)
# | 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... |