#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 7;
int fen[N];
vector<int> start[N], finish[N];
void add_fen(int i, int val) {
for (++i; i < N; i += i & -i) {
fen[i] += val;
}
}
int get_fen(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
res += fen[i];
}
return res;
}
char s[N];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
vector<int> black, white;
for (int i = 0; i < 2 * n; ++i) {
if (s[i] == 'W') {
white.push_back(i);
} else {
black.push_back(i);
}
}
sort(black.begin(), black.end());
sort(white.begin(), white.end());
auto calc = [&](int k) {
k %= n;
for (int i = 0; i < 2 * n + 1; ++i) {
start[i].clear();
finish[i].clear();
}
memset(fen, 0, sizeof fen);
vector<pair<int, int>> segments;
for (int i = 0; i < n; ++i) {
int x = black[i], y = white[(i + k) % n];
segments.push_back({min(x, y), max(x, y)});
}
for (int i = 0; i < n; ++i) {
start[segments[i].first + 1].push_back(i);
finish[segments[i].second].push_back(i);
}
long long res = 0;
int tot = 0;
for (int i = 0; i < 2 * n; ++i) {
for (int j : start[i]) {
++tot;
add_fen(segments[j].first, 1);
}
for (int j : finish[i]) {
--tot;
add_fen(segments[j].first, -1);
}
for (int j : finish[i]) {
res += tot - get_fen(segments[j].first + 1);
}
}
return res;
};
if (n == 1) {
printf("%lld\n", calc(0));
return 0;
}
vector<int> f(n + 1);
for (int i = 0; i < n + 1; ++i) {
f[i] = i % n;
}
vector<long long> mem(n + 1, -1);
auto get = [&](int x) {
return mem[x] == -1 ? mem[x] = calc(f[x]) : mem[x];
};
if (get(0) < get(1)) {
reverse(f.begin() + 1, f.end());
}
int l = 0, r = n;
while (r - l > 1) {
int m = (l + r) / 2;
if (get(m) < get(0)) {
l = m;
} else {
r = m;
}
}
r = n;
while (r - l > 2) {
int m = (l + r) / 2;
if (get(m) < get(m + 1)) {
l = m;
} else {
r = m + 1;
}
}
long long ans = 0;
for (int k = l; k <= r; ++k) {
ans = max(ans, get(k));
}
printf("%lld\n", ans);
return 0;
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
monochrome.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%s", s);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
20564 KB |
Output is correct |
2 |
Correct |
11 ms |
20624 KB |
Output is correct |
3 |
Correct |
11 ms |
20564 KB |
Output is correct |
4 |
Correct |
11 ms |
20564 KB |
Output is correct |
5 |
Correct |
11 ms |
20564 KB |
Output is correct |
6 |
Correct |
11 ms |
20564 KB |
Output is correct |
7 |
Correct |
11 ms |
20532 KB |
Output is correct |
8 |
Correct |
11 ms |
20552 KB |
Output is correct |
9 |
Correct |
11 ms |
20660 KB |
Output is correct |
10 |
Correct |
11 ms |
20564 KB |
Output is correct |
11 |
Correct |
12 ms |
20656 KB |
Output is correct |
12 |
Correct |
11 ms |
20628 KB |
Output is correct |
13 |
Correct |
10 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
20564 KB |
Output is correct |
2 |
Correct |
11 ms |
20624 KB |
Output is correct |
3 |
Correct |
11 ms |
20564 KB |
Output is correct |
4 |
Correct |
11 ms |
20564 KB |
Output is correct |
5 |
Correct |
11 ms |
20564 KB |
Output is correct |
6 |
Correct |
11 ms |
20564 KB |
Output is correct |
7 |
Correct |
11 ms |
20532 KB |
Output is correct |
8 |
Correct |
11 ms |
20552 KB |
Output is correct |
9 |
Correct |
11 ms |
20660 KB |
Output is correct |
10 |
Correct |
11 ms |
20564 KB |
Output is correct |
11 |
Correct |
12 ms |
20656 KB |
Output is correct |
12 |
Correct |
11 ms |
20628 KB |
Output is correct |
13 |
Correct |
10 ms |
20632 KB |
Output is correct |
14 |
Correct |
15 ms |
20680 KB |
Output is correct |
15 |
Correct |
13 ms |
20620 KB |
Output is correct |
16 |
Correct |
15 ms |
20716 KB |
Output is correct |
17 |
Correct |
13 ms |
20692 KB |
Output is correct |
18 |
Correct |
12 ms |
20692 KB |
Output is correct |
19 |
Correct |
12 ms |
20692 KB |
Output is correct |
20 |
Correct |
12 ms |
20688 KB |
Output is correct |
21 |
Correct |
12 ms |
20692 KB |
Output is correct |
22 |
Correct |
14 ms |
20704 KB |
Output is correct |
23 |
Correct |
12 ms |
20692 KB |
Output is correct |
24 |
Correct |
12 ms |
20564 KB |
Output is correct |
25 |
Correct |
12 ms |
20684 KB |
Output is correct |
26 |
Correct |
12 ms |
20692 KB |
Output is correct |
27 |
Correct |
12 ms |
20692 KB |
Output is correct |
28 |
Correct |
12 ms |
20692 KB |
Output is correct |
29 |
Correct |
12 ms |
20692 KB |
Output is correct |
30 |
Correct |
16 ms |
20692 KB |
Output is correct |
31 |
Correct |
12 ms |
20688 KB |
Output is correct |
32 |
Correct |
13 ms |
20692 KB |
Output is correct |
33 |
Correct |
13 ms |
20692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
20564 KB |
Output is correct |
2 |
Correct |
11 ms |
20624 KB |
Output is correct |
3 |
Correct |
11 ms |
20564 KB |
Output is correct |
4 |
Correct |
11 ms |
20564 KB |
Output is correct |
5 |
Correct |
11 ms |
20564 KB |
Output is correct |
6 |
Correct |
11 ms |
20564 KB |
Output is correct |
7 |
Correct |
11 ms |
20532 KB |
Output is correct |
8 |
Correct |
11 ms |
20552 KB |
Output is correct |
9 |
Correct |
11 ms |
20660 KB |
Output is correct |
10 |
Correct |
11 ms |
20564 KB |
Output is correct |
11 |
Correct |
12 ms |
20656 KB |
Output is correct |
12 |
Correct |
11 ms |
20628 KB |
Output is correct |
13 |
Correct |
10 ms |
20632 KB |
Output is correct |
14 |
Correct |
15 ms |
20680 KB |
Output is correct |
15 |
Correct |
13 ms |
20620 KB |
Output is correct |
16 |
Correct |
15 ms |
20716 KB |
Output is correct |
17 |
Correct |
13 ms |
20692 KB |
Output is correct |
18 |
Correct |
12 ms |
20692 KB |
Output is correct |
19 |
Correct |
12 ms |
20692 KB |
Output is correct |
20 |
Correct |
12 ms |
20688 KB |
Output is correct |
21 |
Correct |
12 ms |
20692 KB |
Output is correct |
22 |
Correct |
14 ms |
20704 KB |
Output is correct |
23 |
Correct |
12 ms |
20692 KB |
Output is correct |
24 |
Correct |
12 ms |
20564 KB |
Output is correct |
25 |
Correct |
12 ms |
20684 KB |
Output is correct |
26 |
Correct |
12 ms |
20692 KB |
Output is correct |
27 |
Correct |
12 ms |
20692 KB |
Output is correct |
28 |
Correct |
12 ms |
20692 KB |
Output is correct |
29 |
Correct |
12 ms |
20692 KB |
Output is correct |
30 |
Correct |
16 ms |
20692 KB |
Output is correct |
31 |
Correct |
12 ms |
20688 KB |
Output is correct |
32 |
Correct |
13 ms |
20692 KB |
Output is correct |
33 |
Correct |
13 ms |
20692 KB |
Output is correct |
34 |
Correct |
22 ms |
20948 KB |
Output is correct |
35 |
Correct |
18 ms |
20968 KB |
Output is correct |
36 |
Correct |
18 ms |
20860 KB |
Output is correct |
37 |
Correct |
18 ms |
20852 KB |
Output is correct |
38 |
Correct |
17 ms |
20848 KB |
Output is correct |
39 |
Correct |
18 ms |
20848 KB |
Output is correct |
40 |
Correct |
20 ms |
20840 KB |
Output is correct |
41 |
Correct |
17 ms |
20820 KB |
Output is correct |
42 |
Correct |
18 ms |
20948 KB |
Output is correct |
43 |
Correct |
17 ms |
20908 KB |
Output is correct |
44 |
Correct |
22 ms |
20948 KB |
Output is correct |
45 |
Correct |
21 ms |
20800 KB |
Output is correct |
46 |
Correct |
19 ms |
20820 KB |
Output is correct |
47 |
Correct |
18 ms |
20872 KB |
Output is correct |
48 |
Correct |
18 ms |
20896 KB |
Output is correct |
49 |
Correct |
17 ms |
20924 KB |
Output is correct |
50 |
Correct |
16 ms |
20856 KB |
Output is correct |
51 |
Correct |
14 ms |
20808 KB |
Output is correct |
52 |
Correct |
17 ms |
20988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
20564 KB |
Output is correct |
2 |
Correct |
11 ms |
20624 KB |
Output is correct |
3 |
Correct |
11 ms |
20564 KB |
Output is correct |
4 |
Correct |
11 ms |
20564 KB |
Output is correct |
5 |
Correct |
11 ms |
20564 KB |
Output is correct |
6 |
Correct |
11 ms |
20564 KB |
Output is correct |
7 |
Correct |
11 ms |
20532 KB |
Output is correct |
8 |
Correct |
11 ms |
20552 KB |
Output is correct |
9 |
Correct |
11 ms |
20660 KB |
Output is correct |
10 |
Correct |
11 ms |
20564 KB |
Output is correct |
11 |
Correct |
12 ms |
20656 KB |
Output is correct |
12 |
Correct |
11 ms |
20628 KB |
Output is correct |
13 |
Correct |
10 ms |
20632 KB |
Output is correct |
14 |
Correct |
15 ms |
20680 KB |
Output is correct |
15 |
Correct |
13 ms |
20620 KB |
Output is correct |
16 |
Correct |
15 ms |
20716 KB |
Output is correct |
17 |
Correct |
13 ms |
20692 KB |
Output is correct |
18 |
Correct |
12 ms |
20692 KB |
Output is correct |
19 |
Correct |
12 ms |
20692 KB |
Output is correct |
20 |
Correct |
12 ms |
20688 KB |
Output is correct |
21 |
Correct |
12 ms |
20692 KB |
Output is correct |
22 |
Correct |
14 ms |
20704 KB |
Output is correct |
23 |
Correct |
12 ms |
20692 KB |
Output is correct |
24 |
Correct |
12 ms |
20564 KB |
Output is correct |
25 |
Correct |
12 ms |
20684 KB |
Output is correct |
26 |
Correct |
12 ms |
20692 KB |
Output is correct |
27 |
Correct |
12 ms |
20692 KB |
Output is correct |
28 |
Correct |
12 ms |
20692 KB |
Output is correct |
29 |
Correct |
12 ms |
20692 KB |
Output is correct |
30 |
Correct |
16 ms |
20692 KB |
Output is correct |
31 |
Correct |
12 ms |
20688 KB |
Output is correct |
32 |
Correct |
13 ms |
20692 KB |
Output is correct |
33 |
Correct |
13 ms |
20692 KB |
Output is correct |
34 |
Correct |
22 ms |
20948 KB |
Output is correct |
35 |
Correct |
18 ms |
20968 KB |
Output is correct |
36 |
Correct |
18 ms |
20860 KB |
Output is correct |
37 |
Correct |
18 ms |
20852 KB |
Output is correct |
38 |
Correct |
17 ms |
20848 KB |
Output is correct |
39 |
Correct |
18 ms |
20848 KB |
Output is correct |
40 |
Correct |
20 ms |
20840 KB |
Output is correct |
41 |
Correct |
17 ms |
20820 KB |
Output is correct |
42 |
Correct |
18 ms |
20948 KB |
Output is correct |
43 |
Correct |
17 ms |
20908 KB |
Output is correct |
44 |
Correct |
22 ms |
20948 KB |
Output is correct |
45 |
Correct |
21 ms |
20800 KB |
Output is correct |
46 |
Correct |
19 ms |
20820 KB |
Output is correct |
47 |
Correct |
18 ms |
20872 KB |
Output is correct |
48 |
Correct |
18 ms |
20896 KB |
Output is correct |
49 |
Correct |
17 ms |
20924 KB |
Output is correct |
50 |
Correct |
16 ms |
20856 KB |
Output is correct |
51 |
Correct |
14 ms |
20808 KB |
Output is correct |
52 |
Correct |
17 ms |
20988 KB |
Output is correct |
53 |
Correct |
1198 ms |
53396 KB |
Output is correct |
54 |
Correct |
1185 ms |
53112 KB |
Output is correct |
55 |
Correct |
1127 ms |
52036 KB |
Output is correct |
56 |
Correct |
1142 ms |
52024 KB |
Output is correct |
57 |
Correct |
935 ms |
51276 KB |
Output is correct |
58 |
Correct |
980 ms |
51300 KB |
Output is correct |
59 |
Correct |
1028 ms |
52552 KB |
Output is correct |
60 |
Correct |
992 ms |
50648 KB |
Output is correct |
61 |
Correct |
1104 ms |
50996 KB |
Output is correct |
62 |
Correct |
1061 ms |
51984 KB |
Output is correct |
63 |
Correct |
1112 ms |
52784 KB |
Output is correct |
64 |
Correct |
950 ms |
44980 KB |
Output is correct |
65 |
Correct |
1045 ms |
51400 KB |
Output is correct |
66 |
Correct |
945 ms |
42916 KB |
Output is correct |
67 |
Correct |
933 ms |
43140 KB |
Output is correct |
68 |
Correct |
876 ms |
41892 KB |
Output is correct |
69 |
Correct |
976 ms |
49628 KB |
Output is correct |
70 |
Correct |
848 ms |
44548 KB |
Output is correct |
71 |
Correct |
439 ms |
41604 KB |
Output is correct |
72 |
Correct |
429 ms |
41776 KB |
Output is correct |
73 |
Correct |
1068 ms |
54140 KB |
Output is correct |