#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(string s) {
int n = (int) s.size() / 2;
vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] != s[0]) {
a.push_back(i);
}
else {
b.push_back(i);
}
}
vector<ll> cnt(n, -n);
function<void(int, int, int)> add = [&] (int l, int r, int x) {
if (l <= r) {
for (int j = l; j <= r; j++) {
cnt[j] += x;
}
} else {
for (int j = l; j < n; j++) {
cnt[j] += x;
}
for (int j = 0; j <= r; j++) {
cnt[j] += x;
}
}
};
int pt = 0, pt2 = 0, pt3 = 0;
for (int i = 0; i < n; i++) {
while (pt < n && b[pt] < a[i]) {
pt++;
}
while (pt2 < n && a[i] >= n + b[pt2]) {
pt2++;
}
while (pt3 < n && b[pt3] < n + a[i]) {
pt3++;
}
if (pt < pt3) {
add((n - i + pt) % n, (n - i + pt3 - 1) % n, -a[i]);
}
if (pt3 < n) {
add((n - i + pt3) % n, (n - i + n - 1) % n, 2 * n + a[i]);
}
if (0 < pt2) {
add((n - i) % n, (n - i + pt2 - 1) % n, 2 * n - a[i]);
}
if (pt2 < pt) {
add((n - i + pt2) % n, (n - i + pt - 1) % n, a[i]);
}
}
pt = 0;
for (int j = 0; j < n; j++) {
while (pt < n && a[pt] < b[j]) {
pt++;
}
for (int i = 0; i < pt; i++) {
if (b[j] < n + a[i]) {
cnt[(n - i + j) % n] += b[j];
} else {
cnt[(n - i + j) % n] -= b[j];
}
}
for (int i = pt; i < n; i++) {
if (a[i] < n + b[j]) {
cnt[(n - i + j) % n] -= b[j];
} else {
cnt[(n - i + j) % n] += b[j];
}
}
}
ll sol = 0;
for (auto &x : cnt) {
sol = max(sol, x);
}
return sol /2;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
if (0) {
assert(solve("WWWBWBBBBWWBWWBWWBBWWBBBWBBBWWBW") == 105);
assert(solve("WBBBWBBWWBWWBWWBWBWB") == 41);
assert(solve("BWBWBBWBWW") == 8);
assert(solve("BBWWBW") == 2);
cout << "ok!\n";
exit(0);
}
int n;
string s;
cin >> n >> s;
cout << solve(s) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
2 ms |
316 KB |
Output is correct |
33 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
2 ms |
316 KB |
Output is correct |
33 |
Correct |
1 ms |
308 KB |
Output is correct |
34 |
Correct |
21 ms |
428 KB |
Output is correct |
35 |
Correct |
22 ms |
332 KB |
Output is correct |
36 |
Correct |
21 ms |
332 KB |
Output is correct |
37 |
Correct |
21 ms |
332 KB |
Output is correct |
38 |
Correct |
18 ms |
304 KB |
Output is correct |
39 |
Correct |
20 ms |
312 KB |
Output is correct |
40 |
Correct |
17 ms |
352 KB |
Output is correct |
41 |
Correct |
16 ms |
332 KB |
Output is correct |
42 |
Correct |
21 ms |
312 KB |
Output is correct |
43 |
Correct |
20 ms |
332 KB |
Output is correct |
44 |
Correct |
18 ms |
332 KB |
Output is correct |
45 |
Correct |
20 ms |
332 KB |
Output is correct |
46 |
Correct |
23 ms |
364 KB |
Output is correct |
47 |
Correct |
20 ms |
364 KB |
Output is correct |
48 |
Correct |
21 ms |
332 KB |
Output is correct |
49 |
Correct |
18 ms |
332 KB |
Output is correct |
50 |
Correct |
21 ms |
332 KB |
Output is correct |
51 |
Correct |
21 ms |
332 KB |
Output is correct |
52 |
Correct |
22 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
2 ms |
316 KB |
Output is correct |
33 |
Correct |
1 ms |
308 KB |
Output is correct |
34 |
Correct |
21 ms |
428 KB |
Output is correct |
35 |
Correct |
22 ms |
332 KB |
Output is correct |
36 |
Correct |
21 ms |
332 KB |
Output is correct |
37 |
Correct |
21 ms |
332 KB |
Output is correct |
38 |
Correct |
18 ms |
304 KB |
Output is correct |
39 |
Correct |
20 ms |
312 KB |
Output is correct |
40 |
Correct |
17 ms |
352 KB |
Output is correct |
41 |
Correct |
16 ms |
332 KB |
Output is correct |
42 |
Correct |
21 ms |
312 KB |
Output is correct |
43 |
Correct |
20 ms |
332 KB |
Output is correct |
44 |
Correct |
18 ms |
332 KB |
Output is correct |
45 |
Correct |
20 ms |
332 KB |
Output is correct |
46 |
Correct |
23 ms |
364 KB |
Output is correct |
47 |
Correct |
20 ms |
364 KB |
Output is correct |
48 |
Correct |
21 ms |
332 KB |
Output is correct |
49 |
Correct |
18 ms |
332 KB |
Output is correct |
50 |
Correct |
21 ms |
332 KB |
Output is correct |
51 |
Correct |
21 ms |
332 KB |
Output is correct |
52 |
Correct |
22 ms |
332 KB |
Output is correct |
53 |
Execution timed out |
2072 ms |
4944 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |