#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
string t = "";
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'B') {
for (int j = 0; j < 2 * n; j++) {
t += s[(i + j) % (2 * n)];
}
break;
}
}
s = t;
vector<int> a;
vector<int> b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'B') {
a.push_back(i);
} else {
b.push_back(i);
}
}
vector<int> fenw(2 * n);
auto Modify = [&](int x, int v) {
while (x < 2 * n) {
fenw[x] += v;
x |= (x + 1);
}
};
auto Query = [&](int x) {
int v = 0;
while (x >= 0) {
v += fenw[x];
x = ((x & (x + 1))) - 1;
}
return v;
};
auto GetSum = [&](int L, int R) {
return Query(R) - Query(L - 1);
};
vector<vector<array<int, 3>>> qs(2 * n + 1);
vector<int> to(2 * n + 1, -1);
auto Solve = [&](int k) {
for (int i = 0; i <= 2 * n; i++) {
qs[i].clear();
to[i] = -1;
}
for (int i = 0; i < n; i++) {
int L = a[i];
int R = b[(i + k) % n];
to[L] = R;
if (L <= R) {
qs[L].push_back({0, L - 1, -1});
qs[L].push_back({R + 1, 2 * n - 1, -1});
qs[R].push_back({0, L - 1, +1});
qs[R].push_back({R + 1, 2 * n - 1, +1});
} else {
swap(L, R);
qs[L].push_back({L + 1, R - 1, +1});
qs[R].push_back({L + 1, R - 1, -1});
qs[2 * n].push_back({L + 1, R - 1, +1});
}
}
for (int i = 0; i < 2 * n; i++) {
fenw[i] = 0;
}
long long ret = 0;
for (int i = 0; i <= 2 * n; i++) {
for (auto& p : qs[i]) {
if (p[0] <= p[1]) {
ret += GetSum(p[0], p[1]) * p[2];
}
}
if (to[i] != -1) {
Modify(to[i], 1);
}
}
return ret;
};
int low = 0, high = n - 2;
long long ans = Solve(n - 1);
while (low <= high) {
int mid = low + high >> 1;
long long a = Solve(mid);
long long b = Solve(mid + 1);
ans = max(ans, a);
ans = max(ans, b);
if (a > b) {
high = mid - 1;
} else {
low = mid + 2;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:91:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int mid = low + high >> 1;
| ~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
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 |
348 KB |
Output is correct |
27 |
Correct |
1 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 |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
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 |
348 KB |
Output is correct |
27 |
Correct |
1 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 |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
604 KB |
Output is correct |
36 |
Correct |
5 ms |
604 KB |
Output is correct |
37 |
Correct |
5 ms |
604 KB |
Output is correct |
38 |
Correct |
4 ms |
604 KB |
Output is correct |
39 |
Correct |
5 ms |
748 KB |
Output is correct |
40 |
Correct |
4 ms |
604 KB |
Output is correct |
41 |
Correct |
4 ms |
604 KB |
Output is correct |
42 |
Correct |
5 ms |
600 KB |
Output is correct |
43 |
Correct |
4 ms |
604 KB |
Output is correct |
44 |
Correct |
4 ms |
744 KB |
Output is correct |
45 |
Correct |
5 ms |
604 KB |
Output is correct |
46 |
Correct |
5 ms |
740 KB |
Output is correct |
47 |
Correct |
5 ms |
604 KB |
Output is correct |
48 |
Correct |
5 ms |
604 KB |
Output is correct |
49 |
Correct |
5 ms |
600 KB |
Output is correct |
50 |
Correct |
4 ms |
600 KB |
Output is correct |
51 |
Correct |
3 ms |
604 KB |
Output is correct |
52 |
Correct |
4 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
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 |
348 KB |
Output is correct |
27 |
Correct |
1 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 |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
604 KB |
Output is correct |
36 |
Correct |
5 ms |
604 KB |
Output is correct |
37 |
Correct |
5 ms |
604 KB |
Output is correct |
38 |
Correct |
4 ms |
604 KB |
Output is correct |
39 |
Correct |
5 ms |
748 KB |
Output is correct |
40 |
Correct |
4 ms |
604 KB |
Output is correct |
41 |
Correct |
4 ms |
604 KB |
Output is correct |
42 |
Correct |
5 ms |
600 KB |
Output is correct |
43 |
Correct |
4 ms |
604 KB |
Output is correct |
44 |
Correct |
4 ms |
744 KB |
Output is correct |
45 |
Correct |
5 ms |
604 KB |
Output is correct |
46 |
Correct |
5 ms |
740 KB |
Output is correct |
47 |
Correct |
5 ms |
604 KB |
Output is correct |
48 |
Correct |
5 ms |
604 KB |
Output is correct |
49 |
Correct |
5 ms |
600 KB |
Output is correct |
50 |
Correct |
4 ms |
600 KB |
Output is correct |
51 |
Correct |
3 ms |
604 KB |
Output is correct |
52 |
Correct |
4 ms |
604 KB |
Output is correct |
53 |
Correct |
1010 ms |
29780 KB |
Output is correct |
54 |
Correct |
965 ms |
29752 KB |
Output is correct |
55 |
Correct |
982 ms |
29360 KB |
Output is correct |
56 |
Correct |
964 ms |
29072 KB |
Output is correct |
57 |
Correct |
705 ms |
29684 KB |
Output is correct |
58 |
Correct |
658 ms |
30396 KB |
Output is correct |
59 |
Correct |
687 ms |
29268 KB |
Output is correct |
60 |
Correct |
816 ms |
28540 KB |
Output is correct |
61 |
Correct |
886 ms |
29304 KB |
Output is correct |
62 |
Correct |
774 ms |
28668 KB |
Output is correct |
63 |
Correct |
939 ms |
29592 KB |
Output is correct |
64 |
Correct |
842 ms |
28528 KB |
Output is correct |
65 |
Correct |
825 ms |
27980 KB |
Output is correct |
66 |
Correct |
871 ms |
27396 KB |
Output is correct |
67 |
Correct |
862 ms |
27652 KB |
Output is correct |
68 |
Correct |
806 ms |
27756 KB |
Output is correct |
69 |
Correct |
759 ms |
29300 KB |
Output is correct |
70 |
Correct |
722 ms |
28156 KB |
Output is correct |
71 |
Correct |
749 ms |
28252 KB |
Output is correct |
72 |
Correct |
749 ms |
28168 KB |
Output is correct |
73 |
Correct |
796 ms |
30580 KB |
Output is correct |