#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=400050;
char s[N];
ll ans[N];
int n;
void Add(int l,int r,int f){
if(l>r)return;
if(l<0)l+=n,r+=n;
if(r>=n)Add(l,n-1,f),Add(0,r-n,f);
else ans[l]+=f,ans[r+1]-=f;
}
int main(){
scanf("%i",&n);
scanf("%s",s+1);
vector<int> b,w;
for(int i=1;i<=2*n;i++){
if(s[i]=='B')b.pb(i);
else w.pb(i);
}
//for(int i:b)printf("%i ",i);printf("\n");
//for(int i:w)printf("%i ",i);printf("\n");
auto Get=[&](int i,int j){
int x=abs(i-j)-1;
return min(x,2*n-x-2);
};
for(int z=0,o=0,p=0,q=0;z<n;z++){
int i=b[z];
while(o<n&&w[o]<i)o++;
p=max(p,o);
while(p<n&&Get(w[p],i)==w[p]-i-1)p++;
while(q<o&&Get(w[q],i)!=i-w[q]-1)q++;
//printf("%i %i %i %i\n",i,q,o,p);
Add(o-z,p-1-z,-i-1);
Add(p-z,n-1-z,2*n+i-1);
Add(0-z,q-1-z,2*n-i-1);
Add(q-z,o-1-z,i-1);
}
swap(b,w);
for(int z=0,o=0,p=0,q=0;z<n;z++){
int i=b[z];
while(o<n&&w[o]<i)o++;
p=max(p,o);
while(p<n&&Get(w[p],i)==w[p]-i-1)p++;
while(q<o&&Get(w[q],i)!=i-w[q]-1)q++;
//Add(o+z,p-1+z,-i);
Add(z-(p-1),z-o,-i);
//Add(p+z,n-1+z,i);
Add(z-(n-1),z-p,i);
//Add(0+z,q-1+z,-i);
Add(z-(q-1),z-0,-i);
//Add(q+z,o-1+z,i);
Add(z-(o-1),z-q,i);
}
ll sol=0,sum=0;
for(int i=0;i<n;i++){
sum+=ans[i];
sol=max(sol,sum);
}
printf("%lld\n",sol/2);
/*
int best=0;
for(int i=0;i<n;i++){
if(Get(b[0],w[i])>Get(b[0],w[best]))best=i;
}
ll ans=0;
for(int t=best-1000;t<=best+1000;t++){
ll now=0;
for(int z=0;z<n;z++){
int i=b[z],j=w[(z+t+n)%n];
now+=Get(i,j);
}
ans=max(ans,now);
}
printf("%lld\n",ans/2);*/
return 0;
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
16 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
monochrome.cpp:17:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
17 | scanf("%s",s+1);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
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 |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
416 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
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 |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
416 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
29 ms |
4340 KB |
Output is correct |
54 |
Correct |
27 ms |
4396 KB |
Output is correct |
55 |
Correct |
25 ms |
4332 KB |
Output is correct |
56 |
Correct |
26 ms |
4248 KB |
Output is correct |
57 |
Correct |
17 ms |
4252 KB |
Output is correct |
58 |
Correct |
18 ms |
4464 KB |
Output is correct |
59 |
Correct |
17 ms |
4332 KB |
Output is correct |
60 |
Correct |
17 ms |
4240 KB |
Output is correct |
61 |
Correct |
17 ms |
4244 KB |
Output is correct |
62 |
Correct |
18 ms |
4240 KB |
Output is correct |
63 |
Correct |
20 ms |
4332 KB |
Output is correct |
64 |
Correct |
17 ms |
4212 KB |
Output is correct |
65 |
Correct |
18 ms |
4116 KB |
Output is correct |
66 |
Correct |
18 ms |
4212 KB |
Output is correct |
67 |
Correct |
18 ms |
4212 KB |
Output is correct |
68 |
Correct |
17 ms |
4084 KB |
Output is correct |
69 |
Correct |
17 ms |
4332 KB |
Output is correct |
70 |
Correct |
16 ms |
4080 KB |
Output is correct |
71 |
Correct |
16 ms |
4340 KB |
Output is correct |
72 |
Correct |
14 ms |
4340 KB |
Output is correct |
73 |
Correct |
16 ms |
4460 KB |
Output is correct |