#include <bits/stdc++.h>
using namespace std;
struct BIT
{
int n;
vector<int> bit;
BIT(int n_) : n(n_), bit(n + 1) {}
void add(int i, int u)
{
for (; i <= n; i += i & -i)
{
bit[i] += u;
}
}
int query(int i)
{
int ret = 0;
for (; i > 0; i -= i & -i)
{
ret += bit[i];
}
return ret;
}
int query(int l, int r)
{
return query(r) - query(l);
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
int N = 2 * n;
string a;
cin >> a;
vector<int> b, c;
for (int i = 0; i < N; i++)
{
if (a[i] == 'B')
{
b.push_back(i);
}
else
{
c.push_back(i);
}
}
auto solve = [&](int t) -> long long
{
if (t < 0 || t > n)
{
return 0;
}
vector<pair<int, int>> res;
for (int i = 0; i < t; i++)
{
res.emplace_back(min(b[i], c[n - t + i]), max(b[i], c[n - t + i]));
}
for (int i = t; i < n; i++)
{
res.emplace_back(min(b[i], c[i - t]), max(b[i], c[i - t]));
}
sort(res.begin(), res.end(), [&](pair<int, int> x, pair<int, int> y)
{
if (x.second != y.second)
{
return x.second < y.second;
}
return x.first < y.first;
});
long long ret = 0;
BIT bit(2 * n);
for (auto &[l, r] : res)
{
ret += bit.query(l, r);
bit.add(l + 1, -1);
bit.add(r + 1, 1);
}
return ret;
};
int l = 0, r = n;
while (l < r - 1)
{
int mid = (l + r) / 2;
if (solve(mid) < solve(mid + 1))
{
l = mid;
}
else
{
r = mid;
}
}
long long ans = max(solve(l), solve(l + 1));
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 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 |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 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 |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 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 |
3 ms |
344 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
3 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
348 KB |
Output is correct |
40 |
Correct |
3 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
348 KB |
Output is correct |
42 |
Correct |
5 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
600 KB |
Output is correct |
44 |
Correct |
3 ms |
528 KB |
Output is correct |
45 |
Correct |
3 ms |
348 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
4 ms |
348 KB |
Output is correct |
48 |
Correct |
3 ms |
348 KB |
Output is correct |
49 |
Correct |
3 ms |
348 KB |
Output is correct |
50 |
Correct |
4 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
348 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 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 |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 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 |
3 ms |
344 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
3 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
348 KB |
Output is correct |
40 |
Correct |
3 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
348 KB |
Output is correct |
42 |
Correct |
5 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
600 KB |
Output is correct |
44 |
Correct |
3 ms |
528 KB |
Output is correct |
45 |
Correct |
3 ms |
348 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
4 ms |
348 KB |
Output is correct |
48 |
Correct |
3 ms |
348 KB |
Output is correct |
49 |
Correct |
3 ms |
348 KB |
Output is correct |
50 |
Correct |
4 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
348 KB |
Output is correct |
52 |
Correct |
3 ms |
348 KB |
Output is correct |
53 |
Correct |
1053 ms |
6152 KB |
Output is correct |
54 |
Correct |
992 ms |
6644 KB |
Output is correct |
55 |
Correct |
891 ms |
6428 KB |
Output is correct |
56 |
Correct |
944 ms |
6472 KB |
Output is correct |
57 |
Correct |
771 ms |
6456 KB |
Output is correct |
58 |
Correct |
815 ms |
6544 KB |
Output is correct |
59 |
Correct |
888 ms |
6436 KB |
Output is correct |
60 |
Correct |
832 ms |
6736 KB |
Output is correct |
61 |
Correct |
916 ms |
6352 KB |
Output is correct |
62 |
Correct |
845 ms |
6400 KB |
Output is correct |
63 |
Correct |
939 ms |
7160 KB |
Output is correct |
64 |
Correct |
997 ms |
6392 KB |
Output is correct |
65 |
Correct |
879 ms |
6324 KB |
Output is correct |
66 |
Correct |
690 ms |
6228 KB |
Output is correct |
67 |
Correct |
696 ms |
6236 KB |
Output is correct |
68 |
Correct |
893 ms |
6240 KB |
Output is correct |
69 |
Correct |
875 ms |
6396 KB |
Output is correct |
70 |
Correct |
733 ms |
6076 KB |
Output is correct |
71 |
Correct |
1197 ms |
6656 KB |
Output is correct |
72 |
Correct |
731 ms |
6452 KB |
Output is correct |
73 |
Correct |
1108 ms |
6436 KB |
Output is correct |