#include<bits/stdc++.h>
#define st first
#define nd second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define pp pop_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){cerr<<h;if(sizeof...(t))cerr<<", ";debug(t...);};
#define deb(x...) cerr<<#x<<" = ";debug(x);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<19, INF=1e9+5;
int tab[N+N];
void add(int v, int c){
for(v+=N; v; v>>=1)tab[v]+=c;
}
int sum(int l, int r){
int ans=0;
for(l+=N,r+=N; l<r; l>>=1, r>>=1){
if(l&1)ans+=tab[l++];
if(r&1)ans+=tab[--r];
}
return ans;
}
vi A, B;
int n;
ll solve(int a){
int wsk=n, wsk2=n;
vector<pii> V(n);
for(int i=0; i<a; i++){
//deb(wsk, wsk2);
V[i]=mp(A[i], B[n-a+i]);
}
for(int i=a; i<n; i++){
V[i]=mp(A[i], B[i-a]);
}
for(pii &i:V)if(i.st>i.nd)swap(i.st, i.nd);
sort(all(V));
ll ans=0;
for(int i=0; i<n; i++){
//deb(V[i]);
//deb(V[i].st, V[i].nd);
ans+=sum(V[i].st, V[i].nd);
add(V[i].nd, 1);
}
for(int i=0; i<n; i++){
add(V[i].nd, -1);
}
//deb(ans);
return ans;
}
ll res[N];
ll ans;
ll getans(int t){
t%=n;
if(res[t]==-1)res[t]=solve(t);
ans=max(ans, res[t]);
return res[t];
}
int main(){
//ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
string s;
cin>>s;
for(int i=0; i<n+n; i++){
if(s[i]=='B')A.pb(i);
else B.pb(i);
}
for(int i=0; i<n; i++){
res[i]=-1ll;
//deb(getans(i));
}
if(n<=10){
for(int i=0;i<n; i++)getans(i);
}
else{
int k=n/8;
for(int i=0; ;i+=k){
if(getans(i)<=getans(i+1) && getans(i+k)>getans(i+k+1)){
int l=i+1, r=i+k;
while(l<r){
int m=(l+r)>>1;
if(getans(m)<=getans(m+1))l=m+1;
else r=m;
}
getans(r);
break;
}
}
}
cout<<ans;
}
Compilation message
monochrome.cpp: In function 'll solve(int)':
monochrome.cpp:40:6: warning: unused variable 'wsk' [-Wunused-variable]
40 | int wsk=n, wsk2=n;
| ^~~
monochrome.cpp:40:13: warning: unused variable 'wsk2' [-Wunused-variable]
40 | int wsk=n, wsk2=n;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
7 ms |
340 KB |
Output is correct |
35 |
Correct |
7 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
340 KB |
Output is correct |
37 |
Correct |
7 ms |
340 KB |
Output is correct |
38 |
Correct |
6 ms |
464 KB |
Output is correct |
39 |
Correct |
6 ms |
340 KB |
Output is correct |
40 |
Correct |
6 ms |
444 KB |
Output is correct |
41 |
Correct |
6 ms |
340 KB |
Output is correct |
42 |
Correct |
7 ms |
340 KB |
Output is correct |
43 |
Correct |
5 ms |
340 KB |
Output is correct |
44 |
Correct |
6 ms |
340 KB |
Output is correct |
45 |
Correct |
7 ms |
448 KB |
Output is correct |
46 |
Correct |
7 ms |
340 KB |
Output is correct |
47 |
Correct |
5 ms |
340 KB |
Output is correct |
48 |
Correct |
6 ms |
340 KB |
Output is correct |
49 |
Correct |
5 ms |
340 KB |
Output is correct |
50 |
Correct |
6 ms |
340 KB |
Output is correct |
51 |
Correct |
7 ms |
440 KB |
Output is correct |
52 |
Correct |
6 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
7 ms |
340 KB |
Output is correct |
35 |
Correct |
7 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
340 KB |
Output is correct |
37 |
Correct |
7 ms |
340 KB |
Output is correct |
38 |
Correct |
6 ms |
464 KB |
Output is correct |
39 |
Correct |
6 ms |
340 KB |
Output is correct |
40 |
Correct |
6 ms |
444 KB |
Output is correct |
41 |
Correct |
6 ms |
340 KB |
Output is correct |
42 |
Correct |
7 ms |
340 KB |
Output is correct |
43 |
Correct |
5 ms |
340 KB |
Output is correct |
44 |
Correct |
6 ms |
340 KB |
Output is correct |
45 |
Correct |
7 ms |
448 KB |
Output is correct |
46 |
Correct |
7 ms |
340 KB |
Output is correct |
47 |
Correct |
5 ms |
340 KB |
Output is correct |
48 |
Correct |
6 ms |
340 KB |
Output is correct |
49 |
Correct |
5 ms |
340 KB |
Output is correct |
50 |
Correct |
6 ms |
340 KB |
Output is correct |
51 |
Correct |
7 ms |
440 KB |
Output is correct |
52 |
Correct |
6 ms |
340 KB |
Output is correct |
53 |
Correct |
1338 ms |
8916 KB |
Output is correct |
54 |
Correct |
1367 ms |
9436 KB |
Output is correct |
55 |
Correct |
1220 ms |
8844 KB |
Output is correct |
56 |
Correct |
1224 ms |
8888 KB |
Output is correct |
57 |
Correct |
843 ms |
8060 KB |
Output is correct |
58 |
Correct |
940 ms |
8672 KB |
Output is correct |
59 |
Correct |
958 ms |
8344 KB |
Output is correct |
60 |
Correct |
960 ms |
8936 KB |
Output is correct |
61 |
Correct |
1078 ms |
9056 KB |
Output is correct |
62 |
Correct |
1080 ms |
9052 KB |
Output is correct |
63 |
Correct |
1137 ms |
8964 KB |
Output is correct |
64 |
Correct |
1026 ms |
8164 KB |
Output is correct |
65 |
Correct |
1100 ms |
8576 KB |
Output is correct |
66 |
Correct |
1055 ms |
8544 KB |
Output is correct |
67 |
Correct |
1058 ms |
8636 KB |
Output is correct |
68 |
Correct |
824 ms |
8156 KB |
Output is correct |
69 |
Correct |
865 ms |
8328 KB |
Output is correct |
70 |
Correct |
849 ms |
7232 KB |
Output is correct |
71 |
Correct |
845 ms |
7524 KB |
Output is correct |
72 |
Correct |
1137 ms |
7520 KB |
Output is correct |
73 |
Correct |
1172 ms |
9268 KB |
Output is correct |