#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;
#define x first
#define y second
#define all(v) v.begin(),v.end()
namespace BIT {
int n;
vint d;
void i(int n_) { n = n_; d = vint(n + 1); }
void u(int x, int v){ for(x++; x <= n; x += x & -x) d[x] += v; }
int g(int x){ int r = 0; for(x++; x; x &= x - 1) r += d[x]; return r; }
int g(int s, int e) { return g(e) - g(s - 1); }
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
vint w, b;
for(int i = 0; i < 2 * n; i++) (s[i] == 'W' ? w : b).push_back(i);
ll ans = 0;
vll memo(n, -1LL);
auto f = [&](int t) {
if(memo[t] >= 0) return memo[t];
vint mat(2 * n);
for(int i = 0; i < n; i++) {
int A = w[i], B = b[(i + t) % n];
mat[A] = B;
mat[B] = A;
}
BIT::i(2 * n);
ll cur = 0;
for(int i = 0; i < 2 * n; i++) {
if(i < mat[i]) BIT::u(i, 1);
else {
cur += BIT::g(mat[i] + 1, i - 1);
BIT::u(mat[i], -1);
}
}
ans = max(ans, cur);
return memo[t] = cur;
};
if(n <= 4000) {
for(int i = 0; i < n; i++) f(i);
cout << ans << '\n';
}
else {
int st = (f(0) < f(1));
int en = (f(n - 2) < f(n - 1));
if(st != en) {
int l = 0, r = n - 2;
while(l < r) {
int m = (l + r + 1) / 2;
if(f(m) < f(m + 1)) l = m;
else r = m - 1;
}
}
else if(st == 0) {
int l = 0, r = n - 2;
for(; ; l += (n / 20)) if(f(l) < f(l + 1)) break;
while(l < r) {
int m = (l + r + 1) / 2;
if(f(m) < f(m + 1)) l = m;
else r = m - 1;
}
}
else{
int l = 0, r = n - 2;
for(; ; r -= (n / 20)) if(f(r) >= f(r + 1)) break;
while(l < r) {
int m = (l + r + 1) / 2;
if(f(m) < f(m + 1)) l = m;
else r = m - 1;
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
283 ms |
384 KB |
Output is correct |
35 |
Correct |
280 ms |
384 KB |
Output is correct |
36 |
Correct |
270 ms |
472 KB |
Output is correct |
37 |
Correct |
251 ms |
384 KB |
Output is correct |
38 |
Correct |
197 ms |
476 KB |
Output is correct |
39 |
Correct |
226 ms |
384 KB |
Output is correct |
40 |
Correct |
200 ms |
384 KB |
Output is correct |
41 |
Correct |
195 ms |
504 KB |
Output is correct |
42 |
Correct |
268 ms |
384 KB |
Output is correct |
43 |
Correct |
235 ms |
384 KB |
Output is correct |
44 |
Correct |
211 ms |
408 KB |
Output is correct |
45 |
Correct |
225 ms |
504 KB |
Output is correct |
46 |
Correct |
256 ms |
476 KB |
Output is correct |
47 |
Correct |
240 ms |
504 KB |
Output is correct |
48 |
Correct |
234 ms |
480 KB |
Output is correct |
49 |
Correct |
194 ms |
504 KB |
Output is correct |
50 |
Correct |
227 ms |
384 KB |
Output is correct |
51 |
Correct |
227 ms |
472 KB |
Output is correct |
52 |
Correct |
231 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
283 ms |
384 KB |
Output is correct |
35 |
Correct |
280 ms |
384 KB |
Output is correct |
36 |
Correct |
270 ms |
472 KB |
Output is correct |
37 |
Correct |
251 ms |
384 KB |
Output is correct |
38 |
Correct |
197 ms |
476 KB |
Output is correct |
39 |
Correct |
226 ms |
384 KB |
Output is correct |
40 |
Correct |
200 ms |
384 KB |
Output is correct |
41 |
Correct |
195 ms |
504 KB |
Output is correct |
42 |
Correct |
268 ms |
384 KB |
Output is correct |
43 |
Correct |
235 ms |
384 KB |
Output is correct |
44 |
Correct |
211 ms |
408 KB |
Output is correct |
45 |
Correct |
225 ms |
504 KB |
Output is correct |
46 |
Correct |
256 ms |
476 KB |
Output is correct |
47 |
Correct |
240 ms |
504 KB |
Output is correct |
48 |
Correct |
234 ms |
480 KB |
Output is correct |
49 |
Correct |
194 ms |
504 KB |
Output is correct |
50 |
Correct |
227 ms |
384 KB |
Output is correct |
51 |
Correct |
227 ms |
472 KB |
Output is correct |
52 |
Correct |
231 ms |
504 KB |
Output is correct |
53 |
Correct |
720 ms |
8624 KB |
Output is correct |
54 |
Correct |
714 ms |
8708 KB |
Output is correct |
55 |
Correct |
688 ms |
8316 KB |
Output is correct |
56 |
Correct |
682 ms |
8352 KB |
Output is correct |
57 |
Correct |
695 ms |
8472 KB |
Output is correct |
58 |
Correct |
795 ms |
8652 KB |
Output is correct |
59 |
Correct |
661 ms |
8276 KB |
Output is correct |
60 |
Correct |
660 ms |
8376 KB |
Output is correct |
61 |
Correct |
673 ms |
8348 KB |
Output is correct |
62 |
Correct |
669 ms |
8468 KB |
Output is correct |
63 |
Correct |
672 ms |
8364 KB |
Output is correct |
64 |
Correct |
844 ms |
8384 KB |
Output is correct |
65 |
Correct |
620 ms |
8012 KB |
Output is correct |
66 |
Correct |
925 ms |
8180 KB |
Output is correct |
67 |
Correct |
936 ms |
8212 KB |
Output is correct |
68 |
Correct |
895 ms |
7928 KB |
Output is correct |
69 |
Correct |
743 ms |
8556 KB |
Output is correct |
70 |
Correct |
754 ms |
7876 KB |
Output is correct |
71 |
Correct |
664 ms |
8644 KB |
Output is correct |
72 |
Correct |
663 ms |
8548 KB |
Output is correct |
73 |
Correct |
682 ms |
8632 KB |
Output is correct |