#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define int long long
using namespace std;
const int MAXN = 1e6 + 10;
int n, blue[MAXN], red[MAXN], l1_blue[MAXN], r1_blue[MAXN], l2_blue[MAXN], r2_blue[MAXN];
int l1_red[MAXN], r1_red[MAXN], l2_red[MAXN], r2_red[MAXN];
int closest_left_red[MAXN], closest_left_blue[MAXN];
ll ans[MAXN];
char a[MAXN];
void precalc() {
for (int i = 1; i <= n; i++) {
blue[i + n] = blue[i] + 2 * n;
red[i + n] = red[i] + 2 * n;
}
int lastred = 0, lastblue = 0;
for (int i = 1; i <= 4 * n; i++) {
int cur = i; if (i > 2 * n) cur -= 2 * n;
if (a[cur] == 'B') lastblue++; else lastred++;
closest_left_red[i] = lastred;
closest_left_blue[i] = lastblue;
}
for (int i = 1; i <= n; i++) {
l1_blue[i] = closest_left_red[blue[i]] + 1;
r1_blue[i] = closest_left_red[(blue[i] + blue[i + n]) / 2];
l2_blue[i] = r1_blue[i] + 1;
r2_blue[i] = closest_left_red[blue[i + n]];
l1_red[i] = closest_left_blue[red[i]] + 1;
int tmp = (red[i] + red[i + n]) / 2;
if (blue[closest_left_blue[tmp]] == tmp) {
r1_red[i] = closest_left_blue[tmp] - 1;
l2_red[i] = closest_left_blue[tmp];
r2_red[i] = closest_left_blue[red[i + n]];
} else {
r1_red[i] = closest_left_blue[(red[i] + red[i + n]) / 2];
l2_red[i] = r1_red[i] + 1;
r2_red[i] = closest_left_blue[red[i + n]];
}
}
}
void add(int l, int r, int x) {
if (l > r)
return;
l = (l + 2 * n) % n;
r = (r + 2 * n) % n;
if (l <= r) {
ans[l] += x;
ans[r + 1] -= x;
} else {
ans[l] += x;
ans[1] += x;
ans[r + 1] -= x;
}
}
main() {
// freopen("input.txt", "r", stdin);
cin >> n; int b = 0, r = 0;
for (int i = 1; i <= 2 * n; i++) {
cin >> a[i];
if (a[i] == 'B') {
blue[++b] = i;
} else {
red[++r] = i;
}
}
precalc();
for (int i = 1; i <= n; i++) {
if (l1_blue[i] <= r1_blue[i]) {
add(l1_blue[i] - i, r1_blue[i] - i, -blue[i]);
}
if (l2_blue[i] <= r2_blue[i]) {
add(l2_blue[i] - i, r2_blue[i] - i, blue[i + n]);
}
if (l1_red[i] <= r1_red[i]) {
add(i - min(n, r1_red[i]), i - l1_red[i], -red[i + n]);
add(i - r1_red[i], i - max(n + 1, l1_red[i]), -red[i]);
}
if (l2_red[i] <= r2_red[i]) {
add(i - min(n, r2_red[i]), i - l2_red[i], red[i + n]);
add(i - r2_red[i], i - max(n + 1, l2_red[i]), red[i]);
}
}
ans[0] -= n;
ll anss = 0, cur = 0;
for (int i = 0; i < n; i++) {
cur += ans[i];
anss = max(anss, cur / 2);
}
cout << anss;
}
Compilation message
monochrome.cpp:65:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main() {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |