/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
string s;
vector <int> v[2];
map <pair <int, int>, int> Fen;
void update (int x, int y) {
x++; y++;
for (int i = x; i <= N * 2; i += i & -i) {
for (int j = y; j <= N * 2; j += j & -j) {
Fen[{i, j}]++;
}
}
}
int query (int x, int y) {
x++; y++;
int cnt = 0;
for (int i = x; i >= 1; i -= i & -i) {
for (int j = y; j >= 1; j -= j & -j) {
cnt += Fen[{i, j}];
}
}
return cnt;
}
int intersections (int x, int y) {
return query(x, y) - query(x, x);
}
ll solve (int offs) {
Fen.clear();
vector <pair <int, int>> segments;
for (int i = 0; i < N; i++) {
int j = (i + offs) % N;
segments.push_back(make_pair(v[0][i], v[1][j]));
if (segments.back().first > segments.back().second) {
swap(segments.back().first, segments.back().second);
}
}
sort(segments.begin(), segments.end());
ll cnt = 0;
for (pair <int, int> p : segments) {
cnt += intersections(p.first, p.second);
update(p.first, p.second);
}
return cnt;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
cin >> s;
for (int i = 0; i < N * 2; i++) {
v[(s[i] == 'B')].push_back(i);
}
int l = 0, r = N - 1;
while (l < r) {
int mid = (l + r) / 2;
if (solve(mid) < solve(mid + 1)) {
l = mid + 1;
} else {
r = mid;
}
}
cout << solve(l) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
38 ms |
812 KB |
Output is correct |
15 |
Correct |
38 ms |
716 KB |
Output is correct |
16 |
Correct |
41 ms |
716 KB |
Output is correct |
17 |
Correct |
37 ms |
716 KB |
Output is correct |
18 |
Correct |
31 ms |
588 KB |
Output is correct |
19 |
Correct |
35 ms |
720 KB |
Output is correct |
20 |
Correct |
35 ms |
708 KB |
Output is correct |
21 |
Correct |
32 ms |
712 KB |
Output is correct |
22 |
Correct |
37 ms |
728 KB |
Output is correct |
23 |
Correct |
20 ms |
588 KB |
Output is correct |
24 |
Correct |
32 ms |
716 KB |
Output is correct |
25 |
Correct |
28 ms |
768 KB |
Output is correct |
26 |
Correct |
33 ms |
716 KB |
Output is correct |
27 |
Correct |
36 ms |
716 KB |
Output is correct |
28 |
Correct |
35 ms |
716 KB |
Output is correct |
29 |
Correct |
31 ms |
696 KB |
Output is correct |
30 |
Correct |
31 ms |
588 KB |
Output is correct |
31 |
Correct |
39 ms |
744 KB |
Output is correct |
32 |
Correct |
38 ms |
744 KB |
Output is correct |
33 |
Correct |
40 ms |
892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
38 ms |
812 KB |
Output is correct |
15 |
Correct |
38 ms |
716 KB |
Output is correct |
16 |
Correct |
41 ms |
716 KB |
Output is correct |
17 |
Correct |
37 ms |
716 KB |
Output is correct |
18 |
Correct |
31 ms |
588 KB |
Output is correct |
19 |
Correct |
35 ms |
720 KB |
Output is correct |
20 |
Correct |
35 ms |
708 KB |
Output is correct |
21 |
Correct |
32 ms |
712 KB |
Output is correct |
22 |
Correct |
37 ms |
728 KB |
Output is correct |
23 |
Correct |
20 ms |
588 KB |
Output is correct |
24 |
Correct |
32 ms |
716 KB |
Output is correct |
25 |
Correct |
28 ms |
768 KB |
Output is correct |
26 |
Correct |
33 ms |
716 KB |
Output is correct |
27 |
Correct |
36 ms |
716 KB |
Output is correct |
28 |
Correct |
35 ms |
716 KB |
Output is correct |
29 |
Correct |
31 ms |
696 KB |
Output is correct |
30 |
Correct |
31 ms |
588 KB |
Output is correct |
31 |
Correct |
39 ms |
744 KB |
Output is correct |
32 |
Correct |
38 ms |
744 KB |
Output is correct |
33 |
Correct |
40 ms |
892 KB |
Output is correct |
34 |
Correct |
540 ms |
4936 KB |
Output is correct |
35 |
Correct |
545 ms |
4940 KB |
Output is correct |
36 |
Correct |
548 ms |
4888 KB |
Output is correct |
37 |
Correct |
535 ms |
4668 KB |
Output is correct |
38 |
Correct |
467 ms |
3832 KB |
Output is correct |
39 |
Correct |
513 ms |
4036 KB |
Output is correct |
40 |
Correct |
456 ms |
3816 KB |
Output is correct |
41 |
Correct |
456 ms |
3824 KB |
Output is correct |
42 |
Correct |
531 ms |
4804 KB |
Output is correct |
43 |
Correct |
535 ms |
4652 KB |
Output is correct |
44 |
Correct |
503 ms |
5188 KB |
Output is correct |
45 |
Correct |
497 ms |
4100 KB |
Output is correct |
46 |
Correct |
531 ms |
4248 KB |
Output is correct |
47 |
Correct |
533 ms |
4248 KB |
Output is correct |
48 |
Correct |
510 ms |
4164 KB |
Output is correct |
49 |
Correct |
425 ms |
3776 KB |
Output is correct |
50 |
Correct |
523 ms |
4192 KB |
Output is correct |
51 |
Correct |
526 ms |
4160 KB |
Output is correct |
52 |
Correct |
520 ms |
5620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
38 ms |
812 KB |
Output is correct |
15 |
Correct |
38 ms |
716 KB |
Output is correct |
16 |
Correct |
41 ms |
716 KB |
Output is correct |
17 |
Correct |
37 ms |
716 KB |
Output is correct |
18 |
Correct |
31 ms |
588 KB |
Output is correct |
19 |
Correct |
35 ms |
720 KB |
Output is correct |
20 |
Correct |
35 ms |
708 KB |
Output is correct |
21 |
Correct |
32 ms |
712 KB |
Output is correct |
22 |
Correct |
37 ms |
728 KB |
Output is correct |
23 |
Correct |
20 ms |
588 KB |
Output is correct |
24 |
Correct |
32 ms |
716 KB |
Output is correct |
25 |
Correct |
28 ms |
768 KB |
Output is correct |
26 |
Correct |
33 ms |
716 KB |
Output is correct |
27 |
Correct |
36 ms |
716 KB |
Output is correct |
28 |
Correct |
35 ms |
716 KB |
Output is correct |
29 |
Correct |
31 ms |
696 KB |
Output is correct |
30 |
Correct |
31 ms |
588 KB |
Output is correct |
31 |
Correct |
39 ms |
744 KB |
Output is correct |
32 |
Correct |
38 ms |
744 KB |
Output is correct |
33 |
Correct |
40 ms |
892 KB |
Output is correct |
34 |
Correct |
540 ms |
4936 KB |
Output is correct |
35 |
Correct |
545 ms |
4940 KB |
Output is correct |
36 |
Correct |
548 ms |
4888 KB |
Output is correct |
37 |
Correct |
535 ms |
4668 KB |
Output is correct |
38 |
Correct |
467 ms |
3832 KB |
Output is correct |
39 |
Correct |
513 ms |
4036 KB |
Output is correct |
40 |
Correct |
456 ms |
3816 KB |
Output is correct |
41 |
Correct |
456 ms |
3824 KB |
Output is correct |
42 |
Correct |
531 ms |
4804 KB |
Output is correct |
43 |
Correct |
535 ms |
4652 KB |
Output is correct |
44 |
Correct |
503 ms |
5188 KB |
Output is correct |
45 |
Correct |
497 ms |
4100 KB |
Output is correct |
46 |
Correct |
531 ms |
4248 KB |
Output is correct |
47 |
Correct |
533 ms |
4248 KB |
Output is correct |
48 |
Correct |
510 ms |
4164 KB |
Output is correct |
49 |
Correct |
425 ms |
3776 KB |
Output is correct |
50 |
Correct |
523 ms |
4192 KB |
Output is correct |
51 |
Correct |
526 ms |
4160 KB |
Output is correct |
52 |
Correct |
520 ms |
5620 KB |
Output is correct |
53 |
Execution timed out |
2084 ms |
224236 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |