#include <bits/stdc++.h>
using namespace std;
#define int int64_t
typedef vector<int> vi;
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define all(x) x.begin(), x.end()
#define f first
#define s second
struct ft {
vector<int> bit; int n;
ft(int _n) {bit.resize(_n + 5); n = _n + 5;}
void upd(int i, int x) {i += 2; for (;i < n; i += i & (-i)) bit[i] += x;}
int query(int i) {i += 2; int x = 0; for(; i > 0; i -= i & (-i)) x += bit[i]; return x;}
int query(int i, int j) {if (i > j) return 0; return query(j) - query(i - 1);}
};
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n; string s; cin >> s;
vi b, w; FOR(i, 0, 2 * n) {
if (s[i] == 'B') b.pb(i);
else w.pb(i);
}
auto calc = [&] (vi l, vi r) {
ft f(2 * n + 5);
vi ord(n); iota(all(ord), 0);
int ans = 0;
FOR(i, 0, n) if (r[i] < l[i]) swap(r[i], l[i]);
sort(all(ord), [&] (int u, int v) {return l[u] < l[v];});
FOR(j, 0, n) {
int i = ord[j];
ans += f.query(l[i], r[i]);
f.upd(l[i], -1); f.upd(r[i], 1);
}
return ans;
};
auto solve = [&] (int r) {
vi tb = b;
rotate(tb.begin(), tb.begin() + r, tb.end());
return calc(tb, w);
};
int lo = 0, hi = n - 1;
while (lo < hi) {
int m = (lo + hi) / 2;
if (solve(m) < solve(m + 1)) lo = m + 1;
else hi = m;
}
cout << solve(lo) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 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 |
460 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 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 |
460 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 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 |
460 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 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 |
604 KB |
Output is correct |
36 |
Correct |
4 ms |
600 KB |
Output is correct |
37 |
Correct |
3 ms |
600 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
600 KB |
Output is correct |
40 |
Correct |
3 ms |
348 KB |
Output is correct |
41 |
Correct |
4 ms |
348 KB |
Output is correct |
42 |
Correct |
5 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
608 KB |
Output is correct |
44 |
Correct |
4 ms |
356 KB |
Output is correct |
45 |
Correct |
3 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
3 ms |
604 KB |
Output is correct |
48 |
Correct |
3 ms |
464 KB |
Output is correct |
49 |
Correct |
2 ms |
452 KB |
Output is correct |
50 |
Correct |
5 ms |
604 KB |
Output is correct |
51 |
Correct |
2 ms |
604 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 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 |
460 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 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 |
604 KB |
Output is correct |
36 |
Correct |
4 ms |
600 KB |
Output is correct |
37 |
Correct |
3 ms |
600 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
600 KB |
Output is correct |
40 |
Correct |
3 ms |
348 KB |
Output is correct |
41 |
Correct |
4 ms |
348 KB |
Output is correct |
42 |
Correct |
5 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
608 KB |
Output is correct |
44 |
Correct |
4 ms |
356 KB |
Output is correct |
45 |
Correct |
3 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
3 ms |
604 KB |
Output is correct |
48 |
Correct |
3 ms |
464 KB |
Output is correct |
49 |
Correct |
2 ms |
452 KB |
Output is correct |
50 |
Correct |
5 ms |
604 KB |
Output is correct |
51 |
Correct |
2 ms |
604 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
53 |
Correct |
1356 ms |
14276 KB |
Output is correct |
54 |
Correct |
1438 ms |
14044 KB |
Output is correct |
55 |
Correct |
1384 ms |
13276 KB |
Output is correct |
56 |
Correct |
1211 ms |
13652 KB |
Output is correct |
57 |
Correct |
1238 ms |
13408 KB |
Output is correct |
58 |
Correct |
1223 ms |
13752 KB |
Output is correct |
59 |
Correct |
1136 ms |
13532 KB |
Output is correct |
60 |
Correct |
1284 ms |
13568 KB |
Output is correct |
61 |
Correct |
1326 ms |
13576 KB |
Output is correct |
62 |
Correct |
1349 ms |
13496 KB |
Output is correct |
63 |
Correct |
1358 ms |
13532 KB |
Output is correct |
64 |
Correct |
1349 ms |
13532 KB |
Output is correct |
65 |
Correct |
1137 ms |
13400 KB |
Output is correct |
66 |
Correct |
1296 ms |
13160 KB |
Output is correct |
67 |
Correct |
1475 ms |
13192 KB |
Output is correct |
68 |
Correct |
1062 ms |
12592 KB |
Output is correct |
69 |
Correct |
1391 ms |
13820 KB |
Output is correct |
70 |
Correct |
1184 ms |
12684 KB |
Output is correct |
71 |
Correct |
1726 ms |
14160 KB |
Output is correct |
72 |
Correct |
1257 ms |
13812 KB |
Output is correct |
73 |
Correct |
1531 ms |
14040 KB |
Output is correct |