#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
int sz;
vector<int> f;
void add(int i, int val) {
for (++i; i <= sz; i += i & -i) {
f[i] += val;
}
}
int get(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
res += f[i];
}
return res;
}
Fenwick(int sz_) : sz(sz_) {
f.resize(sz + 1, 0);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s;
cin >> n >> 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 get = [&](int k) {
k %= n;
vector<vector<int>> start(2 * n + 1), finish(2 * n + 1);
Fenwick fenw(2 * n);
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;
for (int i = 0; i < 2 * n; ++i) {
for (int j : start[i]) {
fenw.add(segments[j].first, 1);
}
for (int j : finish[i]) {
fenw.add(segments[j].first, -1);
}
for (int j : finish[i]) {
res += fenw.get(2 * n) - fenw.get(segments[j].first + 1);
}
}
return res;
};
if (n == 1) {
cout << get(0) << endl;
return 0;
}
vector<int> f(n + 1);
for (int i = 0; i < n + 1; ++i) {
f[i] = i % n;
}
if (get(f[0]) < get(f[1])) {
reverse(f.begin() + 1, f.end());
}
long long g0 = get(0);
int l = 0, r = n;
while (r - l > 1) {
int m = (l + r) / 2;
if (get(f[m]) < g0) {
l = m;
} else {
r = m;
}
}
r = n;
while (r - l > 5) {
int m = (l + r) / 2;
if (get(f[m]) < get(f[m + 1])) {
l = m;
} else {
r = m + 1;
}
}
long long ans = 0;
for (int k = l; k <= r; ++k) {
ans = max(ans, get(f[k]));
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
260 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
260 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
380 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
260 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
380 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
16 ms |
696 KB |
Output is correct |
35 |
Correct |
14 ms |
812 KB |
Output is correct |
36 |
Correct |
16 ms |
688 KB |
Output is correct |
37 |
Correct |
13 ms |
676 KB |
Output is correct |
38 |
Correct |
11 ms |
604 KB |
Output is correct |
39 |
Correct |
12 ms |
648 KB |
Output is correct |
40 |
Correct |
10 ms |
660 KB |
Output is correct |
41 |
Correct |
10 ms |
644 KB |
Output is correct |
42 |
Correct |
13 ms |
692 KB |
Output is correct |
43 |
Correct |
11 ms |
700 KB |
Output is correct |
44 |
Correct |
12 ms |
664 KB |
Output is correct |
45 |
Correct |
12 ms |
596 KB |
Output is correct |
46 |
Correct |
10 ms |
692 KB |
Output is correct |
47 |
Correct |
10 ms |
596 KB |
Output is correct |
48 |
Correct |
11 ms |
688 KB |
Output is correct |
49 |
Correct |
12 ms |
780 KB |
Output is correct |
50 |
Correct |
5 ms |
596 KB |
Output is correct |
51 |
Correct |
5 ms |
596 KB |
Output is correct |
52 |
Correct |
12 ms |
692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
260 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
380 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
16 ms |
696 KB |
Output is correct |
35 |
Correct |
14 ms |
812 KB |
Output is correct |
36 |
Correct |
16 ms |
688 KB |
Output is correct |
37 |
Correct |
13 ms |
676 KB |
Output is correct |
38 |
Correct |
11 ms |
604 KB |
Output is correct |
39 |
Correct |
12 ms |
648 KB |
Output is correct |
40 |
Correct |
10 ms |
660 KB |
Output is correct |
41 |
Correct |
10 ms |
644 KB |
Output is correct |
42 |
Correct |
13 ms |
692 KB |
Output is correct |
43 |
Correct |
11 ms |
700 KB |
Output is correct |
44 |
Correct |
12 ms |
664 KB |
Output is correct |
45 |
Correct |
12 ms |
596 KB |
Output is correct |
46 |
Correct |
10 ms |
692 KB |
Output is correct |
47 |
Correct |
10 ms |
596 KB |
Output is correct |
48 |
Correct |
11 ms |
688 KB |
Output is correct |
49 |
Correct |
12 ms |
780 KB |
Output is correct |
50 |
Correct |
5 ms |
596 KB |
Output is correct |
51 |
Correct |
5 ms |
596 KB |
Output is correct |
52 |
Correct |
12 ms |
692 KB |
Output is correct |
53 |
Execution timed out |
2077 ms |
37904 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |