#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
typedef long long llong;
const int MAXN = 400000 + 10;
const int INF = 2e9;
int n;
int w[MAXN];
int b[MAXN];
char s[MAXN];
std::vector <std::pair <int,int>> v;
struct BIT
{
int tree[MAXN];
void reset()
{
std::fill(tree, tree + 2 * n + 1, 0);
}
void update(int pos, int val)
{
for (int idx = pos ; idx <= 2 * n ; idx += idx & (-idx))
{
tree[idx] += val;
}
}
int query(int pos)
{
int res = 0;
for (int idx = pos ; idx > 0 ; idx -= idx & (-idx))
{
res += tree[idx];
}
return res;
}
};
BIT tree;
llong solveFor(int k)
{
v.clear();
tree.reset();
for (int i = 1 ; i <= n ; ++i)
{
v.push_back({std::min(w[i], b[(i + k - 1) % n + 1]), std::max(w[i], b[(i + k - 1) % n + 1])});
}
llong res = 0;
std::sort(v.begin(), v.end());
for (int i = n - 1 ; i >= 0 ; --i)
{
res += tree.query(v[i].second);
tree.update(v[i].first + 1, + 1);
tree.update(v[i].second, -1);
}
return res;
}
void solve()
{
int cntW = 1;
int cntB = 1;
for (int i = 1 ; i <= 2 * n ; ++i)
{
if (s[i] == s[1]) w[cntW++] = i;
else b[cntB++] = i;
}
int l = 0, r = n - 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (solveFor(mid) - solveFor(mid - 1) >= 0) l = mid;
else r = mid;
}
llong res = std::max(solveFor(0), solveFor(n - 1));
std::cout << std::max(res, solveFor(l)) << '\n';
}
void input()
{
std::cin >> n;
std::cin >> s + 1;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
monochrome.cpp: In function 'void input()':
monochrome.cpp:96:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
96 | std::cin >> s + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4572 KB |
Output is correct |
29 |
Correct |
1 ms |
4568 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4572 KB |
Output is correct |
29 |
Correct |
1 ms |
4568 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4440 KB |
Output is correct |
34 |
Correct |
4 ms |
4440 KB |
Output is correct |
35 |
Correct |
4 ms |
4440 KB |
Output is correct |
36 |
Correct |
4 ms |
4440 KB |
Output is correct |
37 |
Correct |
4 ms |
4440 KB |
Output is correct |
38 |
Correct |
3 ms |
4444 KB |
Output is correct |
39 |
Correct |
3 ms |
4444 KB |
Output is correct |
40 |
Correct |
3 ms |
4612 KB |
Output is correct |
41 |
Correct |
3 ms |
4444 KB |
Output is correct |
42 |
Correct |
4 ms |
4620 KB |
Output is correct |
43 |
Correct |
4 ms |
4444 KB |
Output is correct |
44 |
Correct |
4 ms |
4444 KB |
Output is correct |
45 |
Correct |
3 ms |
4440 KB |
Output is correct |
46 |
Correct |
4 ms |
4444 KB |
Output is correct |
47 |
Correct |
3 ms |
4440 KB |
Output is correct |
48 |
Correct |
3 ms |
4444 KB |
Output is correct |
49 |
Correct |
3 ms |
4444 KB |
Output is correct |
50 |
Correct |
2 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
4 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4572 KB |
Output is correct |
29 |
Correct |
1 ms |
4568 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4440 KB |
Output is correct |
34 |
Correct |
4 ms |
4440 KB |
Output is correct |
35 |
Correct |
4 ms |
4440 KB |
Output is correct |
36 |
Correct |
4 ms |
4440 KB |
Output is correct |
37 |
Correct |
4 ms |
4440 KB |
Output is correct |
38 |
Correct |
3 ms |
4444 KB |
Output is correct |
39 |
Correct |
3 ms |
4444 KB |
Output is correct |
40 |
Correct |
3 ms |
4612 KB |
Output is correct |
41 |
Correct |
3 ms |
4444 KB |
Output is correct |
42 |
Correct |
4 ms |
4620 KB |
Output is correct |
43 |
Correct |
4 ms |
4444 KB |
Output is correct |
44 |
Correct |
4 ms |
4444 KB |
Output is correct |
45 |
Correct |
3 ms |
4440 KB |
Output is correct |
46 |
Correct |
4 ms |
4444 KB |
Output is correct |
47 |
Correct |
3 ms |
4440 KB |
Output is correct |
48 |
Correct |
3 ms |
4444 KB |
Output is correct |
49 |
Correct |
3 ms |
4444 KB |
Output is correct |
50 |
Correct |
2 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
4 ms |
4444 KB |
Output is correct |
53 |
Correct |
992 ms |
7128 KB |
Output is correct |
54 |
Correct |
983 ms |
7124 KB |
Output is correct |
55 |
Correct |
893 ms |
7124 KB |
Output is correct |
56 |
Correct |
888 ms |
7128 KB |
Output is correct |
57 |
Correct |
716 ms |
7128 KB |
Output is correct |
58 |
Correct |
790 ms |
7264 KB |
Output is correct |
59 |
Correct |
740 ms |
7128 KB |
Output is correct |
60 |
Correct |
805 ms |
7128 KB |
Output is correct |
61 |
Correct |
857 ms |
7128 KB |
Output is correct |
62 |
Correct |
762 ms |
7128 KB |
Output is correct |
63 |
Correct |
835 ms |
7124 KB |
Output is correct |
64 |
Correct |
757 ms |
7128 KB |
Output is correct |
65 |
Correct |
825 ms |
7128 KB |
Output is correct |
66 |
Correct |
618 ms |
7124 KB |
Output is correct |
67 |
Correct |
679 ms |
7124 KB |
Output is correct |
68 |
Correct |
627 ms |
7128 KB |
Output is correct |
69 |
Correct |
738 ms |
7128 KB |
Output is correct |
70 |
Correct |
787 ms |
7124 KB |
Output is correct |
71 |
Correct |
596 ms |
7128 KB |
Output is correct |
72 |
Correct |
600 ms |
7124 KB |
Output is correct |
73 |
Correct |
1024 ms |
7124 KB |
Output is correct |