#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define int long long
#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;
}
}
int ask(int p){
int 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)->int {
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);
}
int 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;
};
int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
3 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3416 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3528 KB |
Output is correct |
13 |
Correct |
3 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
3 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3416 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3528 KB |
Output is correct |
13 |
Correct |
3 ms |
3420 KB |
Output is correct |
14 |
Correct |
4 ms |
3620 KB |
Output is correct |
15 |
Correct |
4 ms |
3620 KB |
Output is correct |
16 |
Correct |
5 ms |
3620 KB |
Output is correct |
17 |
Correct |
4 ms |
3620 KB |
Output is correct |
18 |
Correct |
4 ms |
3420 KB |
Output is correct |
19 |
Correct |
3 ms |
3616 KB |
Output is correct |
20 |
Correct |
5 ms |
3620 KB |
Output is correct |
21 |
Correct |
4 ms |
3620 KB |
Output is correct |
22 |
Correct |
4 ms |
3616 KB |
Output is correct |
23 |
Correct |
4 ms |
3420 KB |
Output is correct |
24 |
Correct |
4 ms |
3616 KB |
Output is correct |
25 |
Correct |
4 ms |
3524 KB |
Output is correct |
26 |
Correct |
4 ms |
3620 KB |
Output is correct |
27 |
Correct |
4 ms |
3524 KB |
Output is correct |
28 |
Correct |
4 ms |
3620 KB |
Output is correct |
29 |
Correct |
4 ms |
3620 KB |
Output is correct |
30 |
Correct |
4 ms |
3528 KB |
Output is correct |
31 |
Correct |
5 ms |
3616 KB |
Output is correct |
32 |
Correct |
4 ms |
3420 KB |
Output is correct |
33 |
Correct |
3 ms |
3620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
3 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3416 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3528 KB |
Output is correct |
13 |
Correct |
3 ms |
3420 KB |
Output is correct |
14 |
Correct |
4 ms |
3620 KB |
Output is correct |
15 |
Correct |
4 ms |
3620 KB |
Output is correct |
16 |
Correct |
5 ms |
3620 KB |
Output is correct |
17 |
Correct |
4 ms |
3620 KB |
Output is correct |
18 |
Correct |
4 ms |
3420 KB |
Output is correct |
19 |
Correct |
3 ms |
3616 KB |
Output is correct |
20 |
Correct |
5 ms |
3620 KB |
Output is correct |
21 |
Correct |
4 ms |
3620 KB |
Output is correct |
22 |
Correct |
4 ms |
3616 KB |
Output is correct |
23 |
Correct |
4 ms |
3420 KB |
Output is correct |
24 |
Correct |
4 ms |
3616 KB |
Output is correct |
25 |
Correct |
4 ms |
3524 KB |
Output is correct |
26 |
Correct |
4 ms |
3620 KB |
Output is correct |
27 |
Correct |
4 ms |
3524 KB |
Output is correct |
28 |
Correct |
4 ms |
3620 KB |
Output is correct |
29 |
Correct |
4 ms |
3620 KB |
Output is correct |
30 |
Correct |
4 ms |
3528 KB |
Output is correct |
31 |
Correct |
5 ms |
3616 KB |
Output is correct |
32 |
Correct |
4 ms |
3420 KB |
Output is correct |
33 |
Correct |
3 ms |
3620 KB |
Output is correct |
34 |
Correct |
12 ms |
3676 KB |
Output is correct |
35 |
Correct |
11 ms |
3692 KB |
Output is correct |
36 |
Correct |
11 ms |
3676 KB |
Output is correct |
37 |
Correct |
12 ms |
3696 KB |
Output is correct |
38 |
Correct |
10 ms |
3676 KB |
Output is correct |
39 |
Correct |
12 ms |
3688 KB |
Output is correct |
40 |
Correct |
11 ms |
3672 KB |
Output is correct |
41 |
Correct |
10 ms |
3672 KB |
Output is correct |
42 |
Correct |
13 ms |
3676 KB |
Output is correct |
43 |
Correct |
12 ms |
3676 KB |
Output is correct |
44 |
Correct |
12 ms |
3676 KB |
Output is correct |
45 |
Correct |
12 ms |
3676 KB |
Output is correct |
46 |
Correct |
12 ms |
3676 KB |
Output is correct |
47 |
Correct |
11 ms |
3676 KB |
Output is correct |
48 |
Correct |
10 ms |
3676 KB |
Output is correct |
49 |
Correct |
11 ms |
3676 KB |
Output is correct |
50 |
Correct |
12 ms |
3676 KB |
Output is correct |
51 |
Correct |
12 ms |
3676 KB |
Output is correct |
52 |
Correct |
11 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
3 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3416 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3528 KB |
Output is correct |
13 |
Correct |
3 ms |
3420 KB |
Output is correct |
14 |
Correct |
4 ms |
3620 KB |
Output is correct |
15 |
Correct |
4 ms |
3620 KB |
Output is correct |
16 |
Correct |
5 ms |
3620 KB |
Output is correct |
17 |
Correct |
4 ms |
3620 KB |
Output is correct |
18 |
Correct |
4 ms |
3420 KB |
Output is correct |
19 |
Correct |
3 ms |
3616 KB |
Output is correct |
20 |
Correct |
5 ms |
3620 KB |
Output is correct |
21 |
Correct |
4 ms |
3620 KB |
Output is correct |
22 |
Correct |
4 ms |
3616 KB |
Output is correct |
23 |
Correct |
4 ms |
3420 KB |
Output is correct |
24 |
Correct |
4 ms |
3616 KB |
Output is correct |
25 |
Correct |
4 ms |
3524 KB |
Output is correct |
26 |
Correct |
4 ms |
3620 KB |
Output is correct |
27 |
Correct |
4 ms |
3524 KB |
Output is correct |
28 |
Correct |
4 ms |
3620 KB |
Output is correct |
29 |
Correct |
4 ms |
3620 KB |
Output is correct |
30 |
Correct |
4 ms |
3528 KB |
Output is correct |
31 |
Correct |
5 ms |
3616 KB |
Output is correct |
32 |
Correct |
4 ms |
3420 KB |
Output is correct |
33 |
Correct |
3 ms |
3620 KB |
Output is correct |
34 |
Correct |
12 ms |
3676 KB |
Output is correct |
35 |
Correct |
11 ms |
3692 KB |
Output is correct |
36 |
Correct |
11 ms |
3676 KB |
Output is correct |
37 |
Correct |
12 ms |
3696 KB |
Output is correct |
38 |
Correct |
10 ms |
3676 KB |
Output is correct |
39 |
Correct |
12 ms |
3688 KB |
Output is correct |
40 |
Correct |
11 ms |
3672 KB |
Output is correct |
41 |
Correct |
10 ms |
3672 KB |
Output is correct |
42 |
Correct |
13 ms |
3676 KB |
Output is correct |
43 |
Correct |
12 ms |
3676 KB |
Output is correct |
44 |
Correct |
12 ms |
3676 KB |
Output is correct |
45 |
Correct |
12 ms |
3676 KB |
Output is correct |
46 |
Correct |
12 ms |
3676 KB |
Output is correct |
47 |
Correct |
11 ms |
3676 KB |
Output is correct |
48 |
Correct |
10 ms |
3676 KB |
Output is correct |
49 |
Correct |
11 ms |
3676 KB |
Output is correct |
50 |
Correct |
12 ms |
3676 KB |
Output is correct |
51 |
Correct |
12 ms |
3676 KB |
Output is correct |
52 |
Correct |
11 ms |
3676 KB |
Output is correct |
53 |
Execution timed out |
2053 ms |
11136 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |