#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define all(x) x.begin(), x.end()
#define endl '\n'
#define x first
#define y second
const int N=400005;
const int INF=1e18;
const int mod=924844033;
struct BIT{
vector<int> v=vector<int>(N);
void add(int p,int t){
for(int i=p;i<N;i+=i&(-i)){
v[i]+=t;
}
}
ll ask(int p){
ll ans=0;
for(int i=p;i>0;i-=i&(-i)){
ans+=v[i];
}
return ans;
}
};
int32_t main() {
LCBorz;
int n;cin>>n;
string s;cin>>s;
vector<int> a,b;
for(int i=0;i<n<<1;i++){
if(s[i]=='W'){
a.push_back(i+1);
}
else{
b.push_back(i+1);
}
}
auto solve=[&](int k)->ll {
vector<pair<int,int>> v(n);
BIT bit;
for(int i=0;i<n;i++){
v[i]=make_pair(a[i],b[(i+k)%n]);
if(v[i].x>v[i].y){
swap(v[i].x,v[i].y);
}
bit.add(v[i].x,1);
bit.add(v[i].y,-1);
}
ll ans=0;
sort(all(v));
for(int i=0;i<n;i++){
ans+=bit.ask(v[i].y-1)-bit.ask(v[i].x);
bit.add(v[i].x,-1);
bit.add(v[i].y,2);
}
return ans;
};
ll ans=0;
int l=0,r=n+1;
while(r-l>4){
int m=(l+r)>>1;
if(solve(m)>solve(m+1)){
r=m+1;
}
else{
l=m-1;
}
}
for(int i=l;i<=r;i++){
ans=max(ans,solve(i));
}
cout<<ans/2<<endl;
return 0;
}
Compilation message
monochrome.cpp:12:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
12 | const int INF=1e18;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
2 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
2136 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
2 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
2136 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
1884 KB |
Output is correct |
16 |
Correct |
2 ms |
2136 KB |
Output is correct |
17 |
Correct |
3 ms |
1884 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2044 KB |
Output is correct |
21 |
Correct |
3 ms |
2036 KB |
Output is correct |
22 |
Correct |
3 ms |
1888 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
1880 KB |
Output is correct |
27 |
Correct |
2 ms |
1880 KB |
Output is correct |
28 |
Correct |
3 ms |
2136 KB |
Output is correct |
29 |
Correct |
2 ms |
2036 KB |
Output is correct |
30 |
Correct |
2 ms |
1880 KB |
Output is correct |
31 |
Correct |
3 ms |
1884 KB |
Output is correct |
32 |
Correct |
2 ms |
1880 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
2 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
2136 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
1884 KB |
Output is correct |
16 |
Correct |
2 ms |
2136 KB |
Output is correct |
17 |
Correct |
3 ms |
1884 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2044 KB |
Output is correct |
21 |
Correct |
3 ms |
2036 KB |
Output is correct |
22 |
Correct |
3 ms |
1888 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
1880 KB |
Output is correct |
27 |
Correct |
2 ms |
1880 KB |
Output is correct |
28 |
Correct |
3 ms |
2136 KB |
Output is correct |
29 |
Correct |
2 ms |
2036 KB |
Output is correct |
30 |
Correct |
2 ms |
1880 KB |
Output is correct |
31 |
Correct |
3 ms |
1884 KB |
Output is correct |
32 |
Correct |
2 ms |
1880 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
34 |
Correct |
8 ms |
2084 KB |
Output is correct |
35 |
Correct |
8 ms |
1884 KB |
Output is correct |
36 |
Correct |
8 ms |
1884 KB |
Output is correct |
37 |
Correct |
8 ms |
2080 KB |
Output is correct |
38 |
Correct |
7 ms |
1884 KB |
Output is correct |
39 |
Correct |
8 ms |
2392 KB |
Output is correct |
40 |
Correct |
8 ms |
2076 KB |
Output is correct |
41 |
Correct |
7 ms |
1880 KB |
Output is correct |
42 |
Correct |
8 ms |
1956 KB |
Output is correct |
43 |
Correct |
8 ms |
1884 KB |
Output is correct |
44 |
Correct |
8 ms |
2080 KB |
Output is correct |
45 |
Correct |
8 ms |
1884 KB |
Output is correct |
46 |
Correct |
9 ms |
1884 KB |
Output is correct |
47 |
Correct |
8 ms |
1884 KB |
Output is correct |
48 |
Correct |
8 ms |
1884 KB |
Output is correct |
49 |
Correct |
7 ms |
1884 KB |
Output is correct |
50 |
Correct |
8 ms |
1884 KB |
Output is correct |
51 |
Correct |
7 ms |
1884 KB |
Output is correct |
52 |
Correct |
8 ms |
2088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
2 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
2136 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
1884 KB |
Output is correct |
16 |
Correct |
2 ms |
2136 KB |
Output is correct |
17 |
Correct |
3 ms |
1884 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2044 KB |
Output is correct |
21 |
Correct |
3 ms |
2036 KB |
Output is correct |
22 |
Correct |
3 ms |
1888 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
1880 KB |
Output is correct |
27 |
Correct |
2 ms |
1880 KB |
Output is correct |
28 |
Correct |
3 ms |
2136 KB |
Output is correct |
29 |
Correct |
2 ms |
2036 KB |
Output is correct |
30 |
Correct |
2 ms |
1880 KB |
Output is correct |
31 |
Correct |
3 ms |
1884 KB |
Output is correct |
32 |
Correct |
2 ms |
1880 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
34 |
Correct |
8 ms |
2084 KB |
Output is correct |
35 |
Correct |
8 ms |
1884 KB |
Output is correct |
36 |
Correct |
8 ms |
1884 KB |
Output is correct |
37 |
Correct |
8 ms |
2080 KB |
Output is correct |
38 |
Correct |
7 ms |
1884 KB |
Output is correct |
39 |
Correct |
8 ms |
2392 KB |
Output is correct |
40 |
Correct |
8 ms |
2076 KB |
Output is correct |
41 |
Correct |
7 ms |
1880 KB |
Output is correct |
42 |
Correct |
8 ms |
1956 KB |
Output is correct |
43 |
Correct |
8 ms |
1884 KB |
Output is correct |
44 |
Correct |
8 ms |
2080 KB |
Output is correct |
45 |
Correct |
8 ms |
1884 KB |
Output is correct |
46 |
Correct |
9 ms |
1884 KB |
Output is correct |
47 |
Correct |
8 ms |
1884 KB |
Output is correct |
48 |
Correct |
8 ms |
1884 KB |
Output is correct |
49 |
Correct |
7 ms |
1884 KB |
Output is correct |
50 |
Correct |
8 ms |
1884 KB |
Output is correct |
51 |
Correct |
7 ms |
1884 KB |
Output is correct |
52 |
Correct |
8 ms |
2088 KB |
Output is correct |
53 |
Correct |
1189 ms |
5860 KB |
Output is correct |
54 |
Correct |
1171 ms |
6084 KB |
Output is correct |
55 |
Correct |
1190 ms |
6136 KB |
Output is correct |
56 |
Correct |
1150 ms |
6108 KB |
Output is correct |
57 |
Correct |
1022 ms |
6020 KB |
Output is correct |
58 |
Correct |
933 ms |
6052 KB |
Output is correct |
59 |
Correct |
1032 ms |
5900 KB |
Output is correct |
60 |
Correct |
1095 ms |
6220 KB |
Output is correct |
61 |
Correct |
1127 ms |
6108 KB |
Output is correct |
62 |
Correct |
1073 ms |
6116 KB |
Output is correct |
63 |
Correct |
1182 ms |
6116 KB |
Output is correct |
64 |
Correct |
948 ms |
5988 KB |
Output is correct |
65 |
Correct |
1045 ms |
6056 KB |
Output is correct |
66 |
Correct |
1060 ms |
5820 KB |
Output is correct |
67 |
Correct |
1090 ms |
5888 KB |
Output is correct |
68 |
Correct |
832 ms |
5736 KB |
Output is correct |
69 |
Correct |
970 ms |
6156 KB |
Output is correct |
70 |
Correct |
932 ms |
5584 KB |
Output is correct |
71 |
Correct |
1147 ms |
6076 KB |
Output is correct |
72 |
Correct |
845 ms |
6064 KB |
Output is correct |
73 |
Correct |
1228 ms |
6236 KB |
Output is correct |