#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
{
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;
};
long long ans = 0;
for (int i = 0; i <= n; i++)
{
ans = max(ans, solve(i));
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
600 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
464 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
452 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
468 KB |
Output is correct |
32 |
Correct |
5 ms |
600 KB |
Output is correct |
33 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
600 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
464 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
452 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
468 KB |
Output is correct |
32 |
Correct |
5 ms |
600 KB |
Output is correct |
33 |
Correct |
5 ms |
348 KB |
Output is correct |
34 |
Correct |
216 ms |
504 KB |
Output is correct |
35 |
Correct |
213 ms |
504 KB |
Output is correct |
36 |
Correct |
215 ms |
512 KB |
Output is correct |
37 |
Correct |
193 ms |
348 KB |
Output is correct |
38 |
Correct |
176 ms |
508 KB |
Output is correct |
39 |
Correct |
180 ms |
504 KB |
Output is correct |
40 |
Correct |
155 ms |
348 KB |
Output is correct |
41 |
Correct |
150 ms |
508 KB |
Output is correct |
42 |
Correct |
214 ms |
508 KB |
Output is correct |
43 |
Correct |
158 ms |
500 KB |
Output is correct |
44 |
Correct |
194 ms |
508 KB |
Output is correct |
45 |
Correct |
188 ms |
344 KB |
Output is correct |
46 |
Correct |
199 ms |
596 KB |
Output is correct |
47 |
Correct |
218 ms |
504 KB |
Output is correct |
48 |
Correct |
176 ms |
348 KB |
Output is correct |
49 |
Correct |
166 ms |
504 KB |
Output is correct |
50 |
Correct |
189 ms |
500 KB |
Output is correct |
51 |
Correct |
156 ms |
344 KB |
Output is correct |
52 |
Correct |
224 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 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 |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
600 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
464 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
452 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
468 KB |
Output is correct |
32 |
Correct |
5 ms |
600 KB |
Output is correct |
33 |
Correct |
5 ms |
348 KB |
Output is correct |
34 |
Correct |
216 ms |
504 KB |
Output is correct |
35 |
Correct |
213 ms |
504 KB |
Output is correct |
36 |
Correct |
215 ms |
512 KB |
Output is correct |
37 |
Correct |
193 ms |
348 KB |
Output is correct |
38 |
Correct |
176 ms |
508 KB |
Output is correct |
39 |
Correct |
180 ms |
504 KB |
Output is correct |
40 |
Correct |
155 ms |
348 KB |
Output is correct |
41 |
Correct |
150 ms |
508 KB |
Output is correct |
42 |
Correct |
214 ms |
508 KB |
Output is correct |
43 |
Correct |
158 ms |
500 KB |
Output is correct |
44 |
Correct |
194 ms |
508 KB |
Output is correct |
45 |
Correct |
188 ms |
344 KB |
Output is correct |
46 |
Correct |
199 ms |
596 KB |
Output is correct |
47 |
Correct |
218 ms |
504 KB |
Output is correct |
48 |
Correct |
176 ms |
348 KB |
Output is correct |
49 |
Correct |
166 ms |
504 KB |
Output is correct |
50 |
Correct |
189 ms |
500 KB |
Output is correct |
51 |
Correct |
156 ms |
344 KB |
Output is correct |
52 |
Correct |
224 ms |
348 KB |
Output is correct |
53 |
Execution timed out |
2011 ms |
6616 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |