#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)4e5 + 100;
int col[N];
int n, m;
vector<int> C[2];
int segt[N*2];
void upd(int id, int v){
id += m;
while(id > 0){
segt[id] += v;
id /= 2;
}
}
int get(int l, int r){
l += m;
r += m;
int cum = 0;
while(l <= r){
if(l % 2 == 1) cum += segt[l ++ ];
if(r % 2 == 0) cum += segt[r -- ];
l /= 2;
r /= 2;
}
return cum;
}
ll f(int k){
vector<pii> seg;
for(int i = 0 ; i < n; i ++ ){
seg.push_back(mp(C[0][i], C[1][(i + k) % n]));
}
for(auto &x : seg){
if(x.fi > x.se) swap(x.fi, x.se);
}
for(int i = 0 ; i < m * 2; i ++ ){
segt[i]=0;
}
sort(seg.begin(), seg.end());
ll ff = 0;
for(auto q : seg){
ff += get(q.fi, q.se);
upd(q.se, +1);
}
return ff;
}
int main(){
fastIO;
cin >> n;
m = n * 2;
char qf;
for(int i = 0; i < m ; i ++ ){
cin >> qf;
if(qf == 'B'){
col[i]=0;
}
else{
col[i]=1;
}
C[col[i]].push_back(i);
}
ll high = f(0);
int li = 1;
int ri = n;
int mid;
while(li < ri){
mid = (li + ri) / 2;
if(f(mid) < f(mid + 1)){
li = mid + 1;
}
else{
ri = mid;
}
}
high = max(high, f(li));
cout << high << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
8 ms |
504 KB |
Output is correct |
35 |
Correct |
8 ms |
492 KB |
Output is correct |
36 |
Correct |
8 ms |
492 KB |
Output is correct |
37 |
Correct |
7 ms |
492 KB |
Output is correct |
38 |
Correct |
6 ms |
492 KB |
Output is correct |
39 |
Correct |
6 ms |
492 KB |
Output is correct |
40 |
Correct |
6 ms |
492 KB |
Output is correct |
41 |
Correct |
7 ms |
492 KB |
Output is correct |
42 |
Correct |
8 ms |
492 KB |
Output is correct |
43 |
Correct |
8 ms |
492 KB |
Output is correct |
44 |
Correct |
7 ms |
492 KB |
Output is correct |
45 |
Correct |
8 ms |
492 KB |
Output is correct |
46 |
Correct |
7 ms |
492 KB |
Output is correct |
47 |
Correct |
7 ms |
492 KB |
Output is correct |
48 |
Correct |
6 ms |
492 KB |
Output is correct |
49 |
Correct |
5 ms |
492 KB |
Output is correct |
50 |
Correct |
5 ms |
492 KB |
Output is correct |
51 |
Correct |
6 ms |
492 KB |
Output is correct |
52 |
Correct |
7 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
8 ms |
504 KB |
Output is correct |
35 |
Correct |
8 ms |
492 KB |
Output is correct |
36 |
Correct |
8 ms |
492 KB |
Output is correct |
37 |
Correct |
7 ms |
492 KB |
Output is correct |
38 |
Correct |
6 ms |
492 KB |
Output is correct |
39 |
Correct |
6 ms |
492 KB |
Output is correct |
40 |
Correct |
6 ms |
492 KB |
Output is correct |
41 |
Correct |
7 ms |
492 KB |
Output is correct |
42 |
Correct |
8 ms |
492 KB |
Output is correct |
43 |
Correct |
8 ms |
492 KB |
Output is correct |
44 |
Correct |
7 ms |
492 KB |
Output is correct |
45 |
Correct |
8 ms |
492 KB |
Output is correct |
46 |
Correct |
7 ms |
492 KB |
Output is correct |
47 |
Correct |
7 ms |
492 KB |
Output is correct |
48 |
Correct |
6 ms |
492 KB |
Output is correct |
49 |
Correct |
5 ms |
492 KB |
Output is correct |
50 |
Correct |
5 ms |
492 KB |
Output is correct |
51 |
Correct |
6 ms |
492 KB |
Output is correct |
52 |
Correct |
7 ms |
492 KB |
Output is correct |
53 |
Correct |
1814 ms |
10292 KB |
Output is correct |
54 |
Correct |
1750 ms |
10756 KB |
Output is correct |
55 |
Correct |
1550 ms |
10420 KB |
Output is correct |
56 |
Correct |
1699 ms |
10524 KB |
Output is correct |
57 |
Correct |
1175 ms |
10512 KB |
Output is correct |
58 |
Correct |
1274 ms |
10736 KB |
Output is correct |
59 |
Correct |
1396 ms |
10504 KB |
Output is correct |
60 |
Correct |
1235 ms |
10672 KB |
Output is correct |
61 |
Correct |
1491 ms |
10604 KB |
Output is correct |
62 |
Correct |
1460 ms |
10740 KB |
Output is correct |
63 |
Correct |
1526 ms |
10588 KB |
Output is correct |
64 |
Correct |
1236 ms |
10388 KB |
Output is correct |
65 |
Correct |
1457 ms |
10012 KB |
Output is correct |
66 |
Correct |
1171 ms |
10264 KB |
Output is correct |
67 |
Correct |
1300 ms |
10288 KB |
Output is correct |
68 |
Correct |
1434 ms |
10260 KB |
Output is correct |
69 |
Correct |
1239 ms |
10636 KB |
Output is correct |
70 |
Correct |
1187 ms |
10216 KB |
Output is correct |
71 |
Correct |
999 ms |
10928 KB |
Output is correct |
72 |
Correct |
1352 ms |
10808 KB |
Output is correct |
73 |
Correct |
1556 ms |
10952 KB |
Output is correct |