#include <bits/stdc++.h>
#define ent '\n'
//#define int long long
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 12;
int t[maxn * 2], r[maxn * 2];
int a[maxn], b[maxn];
int n, m, k;
void upd(int i, int x){
for(; i <= n * 2;i |= (i + 1)){
t[i] += x;
}
}
int GET(int i){
int ans = 0;
for(; i > 0; i = (i & (i + 1)) - 1){
ans += t[i];
}
return ans;
}
int get(int l, int r){
return GET(r) - GET(l - 1);
}
ll get(int k){
for(int i=1;i<=n*2;i++){
t[i] = r[i] = 0;
}
vector<pair<int, int>> v;
for(int i=0;i<n;i++){
r[min(a[i], b[(i + k) % n])] = max(a[i], b[(i + k) % n]);
}
ll ans = 0;
for(int i=1;i<=2 * n;i++){
if(!r[i]) continue;
ans += get(i, r[i]);
upd(r[i], 1);
}
return ans;
}
void solve(){
cin >> n;
int l = 0, r = 0;
ll ans = 0;
for(int i=1;i<=2*n;i++){
char c;
cin >> c;
if(c == 'W') a[l++] = i;
else b[r++] = i;
}
l = 1, r = n;
while(r - l > 3){
int m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
ll x = get(m1), y = get(m2);
ans = max({ans, x, y});
if(x < y) l = m1;
else r = m2;
}
for(int i=l;i<=r;i++){
ans = max(ans, get(i));
}
cout << ans << ent;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
2 ms |
348 KB |
Output is correct |
40 |
Correct |
2 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
348 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
348 KB |
Output is correct |
46 |
Correct |
2 ms |
460 KB |
Output is correct |
47 |
Correct |
2 ms |
348 KB |
Output is correct |
48 |
Correct |
2 ms |
464 KB |
Output is correct |
49 |
Correct |
2 ms |
348 KB |
Output is correct |
50 |
Correct |
2 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
532 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
2 ms |
348 KB |
Output is correct |
40 |
Correct |
2 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
348 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
348 KB |
Output is correct |
46 |
Correct |
2 ms |
460 KB |
Output is correct |
47 |
Correct |
2 ms |
348 KB |
Output is correct |
48 |
Correct |
2 ms |
464 KB |
Output is correct |
49 |
Correct |
2 ms |
348 KB |
Output is correct |
50 |
Correct |
2 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
532 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
53 |
Correct |
565 ms |
5528 KB |
Output is correct |
54 |
Correct |
567 ms |
5468 KB |
Output is correct |
55 |
Correct |
506 ms |
5292 KB |
Output is correct |
56 |
Correct |
529 ms |
5252 KB |
Output is correct |
57 |
Correct |
440 ms |
5400 KB |
Output is correct |
58 |
Correct |
458 ms |
5528 KB |
Output is correct |
59 |
Correct |
426 ms |
5164 KB |
Output is correct |
60 |
Correct |
448 ms |
5456 KB |
Output is correct |
61 |
Correct |
462 ms |
5404 KB |
Output is correct |
62 |
Correct |
464 ms |
5468 KB |
Output is correct |
63 |
Correct |
461 ms |
5208 KB |
Output is correct |
64 |
Correct |
445 ms |
5212 KB |
Output is correct |
65 |
Correct |
484 ms |
4956 KB |
Output is correct |
66 |
Correct |
432 ms |
5212 KB |
Output is correct |
67 |
Correct |
445 ms |
5212 KB |
Output is correct |
68 |
Correct |
413 ms |
4952 KB |
Output is correct |
69 |
Correct |
449 ms |
5500 KB |
Output is correct |
70 |
Correct |
363 ms |
5208 KB |
Output is correct |
71 |
Correct |
434 ms |
5468 KB |
Output is correct |
72 |
Correct |
439 ms |
5520 KB |
Output is correct |
73 |
Correct |
480 ms |
5468 KB |
Output is correct |