/* Ignore Others Only Focus On Yourself! */
/* Im the Best! */
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
const int N = 4e5 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
int n, fen[N];
void add(int i, int x)
{
for(i += 3; i < N; i += i & -i)
{
fen[i] += x;
}
}
int get(int i, int ret = 0)
{
for(i += 3; i; i -= i & -i)
{
ret += fen[i];
}
return ret;
}
string s;
vector < int > W, B;
vector < pii > seg;
ll tot;
ll solve(int Shift)
{
ll ret = 0;
memset(fen, 0, sizeof fen);
seg.clear();
for(int i = 0; i < n; i ++)
{
int j = (i + Shift) % n;
seg.push_back(Mp(max(W[i], B[j]), min(W[i], B[j])));
}
sort(all(seg));
for(int i = 0; i < n; i ++)
{
int l = seg[i].S;
int now = get(l) - (lower_bound(all(seg), Mp(l + 1, -1)) - seg.begin());
ret += now;
add(l, 1);
}
tot = max(tot, ret);
return ret;
}
int main()
{
fast_io;
cin >> n >> s;
for(int i = 0; i < n << 1; i ++)
{
if(s[i] == 'W')
{
W.push_back(i);
}
else
{
B.push_back(i);
}
}
int l = 0, r = n - 1;
solve(0);
solve(n - 1);
while(r - l > 20)
{
int l1 = (l * 2 + r) / 3, l2 = (r * 2 + l) / 3;
ll L = solve(l1), R = solve(l2);
if(L < R)
{
l = l1;
}
else
{
r = l2;
}
}
for(int i = l; i <= r; i ++)
{
solve(i);
}
cout << tot;
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
5 ms |
1876 KB |
Output is correct |
16 |
Correct |
3 ms |
1876 KB |
Output is correct |
17 |
Correct |
4 ms |
1876 KB |
Output is correct |
18 |
Correct |
3 ms |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1876 KB |
Output is correct |
20 |
Correct |
3 ms |
1876 KB |
Output is correct |
21 |
Correct |
3 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
3 ms |
1876 KB |
Output is correct |
25 |
Correct |
3 ms |
1876 KB |
Output is correct |
26 |
Correct |
3 ms |
1876 KB |
Output is correct |
27 |
Correct |
4 ms |
1876 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
3 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1876 KB |
Output is correct |
33 |
Correct |
3 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
5 ms |
1876 KB |
Output is correct |
16 |
Correct |
3 ms |
1876 KB |
Output is correct |
17 |
Correct |
4 ms |
1876 KB |
Output is correct |
18 |
Correct |
3 ms |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1876 KB |
Output is correct |
20 |
Correct |
3 ms |
1876 KB |
Output is correct |
21 |
Correct |
3 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
3 ms |
1876 KB |
Output is correct |
25 |
Correct |
3 ms |
1876 KB |
Output is correct |
26 |
Correct |
3 ms |
1876 KB |
Output is correct |
27 |
Correct |
4 ms |
1876 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
3 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1876 KB |
Output is correct |
33 |
Correct |
3 ms |
1876 KB |
Output is correct |
34 |
Correct |
11 ms |
1876 KB |
Output is correct |
35 |
Correct |
10 ms |
1876 KB |
Output is correct |
36 |
Correct |
10 ms |
1876 KB |
Output is correct |
37 |
Correct |
9 ms |
1876 KB |
Output is correct |
38 |
Correct |
9 ms |
1876 KB |
Output is correct |
39 |
Correct |
10 ms |
1876 KB |
Output is correct |
40 |
Correct |
9 ms |
1876 KB |
Output is correct |
41 |
Correct |
9 ms |
1876 KB |
Output is correct |
42 |
Correct |
9 ms |
1948 KB |
Output is correct |
43 |
Correct |
10 ms |
1876 KB |
Output is correct |
44 |
Correct |
10 ms |
1876 KB |
Output is correct |
45 |
Correct |
9 ms |
1876 KB |
Output is correct |
46 |
Correct |
10 ms |
1940 KB |
Output is correct |
47 |
Correct |
8 ms |
1876 KB |
Output is correct |
48 |
Correct |
8 ms |
1876 KB |
Output is correct |
49 |
Correct |
9 ms |
1876 KB |
Output is correct |
50 |
Correct |
10 ms |
1876 KB |
Output is correct |
51 |
Correct |
11 ms |
1876 KB |
Output is correct |
52 |
Correct |
12 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
5 ms |
1876 KB |
Output is correct |
16 |
Correct |
3 ms |
1876 KB |
Output is correct |
17 |
Correct |
4 ms |
1876 KB |
Output is correct |
18 |
Correct |
3 ms |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1876 KB |
Output is correct |
20 |
Correct |
3 ms |
1876 KB |
Output is correct |
21 |
Correct |
3 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
3 ms |
1876 KB |
Output is correct |
25 |
Correct |
3 ms |
1876 KB |
Output is correct |
26 |
Correct |
3 ms |
1876 KB |
Output is correct |
27 |
Correct |
4 ms |
1876 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
3 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1876 KB |
Output is correct |
33 |
Correct |
3 ms |
1876 KB |
Output is correct |
34 |
Correct |
11 ms |
1876 KB |
Output is correct |
35 |
Correct |
10 ms |
1876 KB |
Output is correct |
36 |
Correct |
10 ms |
1876 KB |
Output is correct |
37 |
Correct |
9 ms |
1876 KB |
Output is correct |
38 |
Correct |
9 ms |
1876 KB |
Output is correct |
39 |
Correct |
10 ms |
1876 KB |
Output is correct |
40 |
Correct |
9 ms |
1876 KB |
Output is correct |
41 |
Correct |
9 ms |
1876 KB |
Output is correct |
42 |
Correct |
9 ms |
1948 KB |
Output is correct |
43 |
Correct |
10 ms |
1876 KB |
Output is correct |
44 |
Correct |
10 ms |
1876 KB |
Output is correct |
45 |
Correct |
9 ms |
1876 KB |
Output is correct |
46 |
Correct |
10 ms |
1940 KB |
Output is correct |
47 |
Correct |
8 ms |
1876 KB |
Output is correct |
48 |
Correct |
8 ms |
1876 KB |
Output is correct |
49 |
Correct |
9 ms |
1876 KB |
Output is correct |
50 |
Correct |
10 ms |
1876 KB |
Output is correct |
51 |
Correct |
11 ms |
1876 KB |
Output is correct |
52 |
Correct |
12 ms |
1876 KB |
Output is correct |
53 |
Correct |
1637 ms |
5980 KB |
Output is correct |
54 |
Correct |
1688 ms |
6432 KB |
Output is correct |
55 |
Correct |
1540 ms |
6296 KB |
Output is correct |
56 |
Correct |
1556 ms |
6360 KB |
Output is correct |
57 |
Correct |
1293 ms |
6340 KB |
Output is correct |
58 |
Correct |
1211 ms |
6372 KB |
Output is correct |
59 |
Correct |
1349 ms |
6324 KB |
Output is correct |
60 |
Correct |
1334 ms |
6240 KB |
Output is correct |
61 |
Correct |
1476 ms |
6364 KB |
Output is correct |
62 |
Correct |
1468 ms |
6364 KB |
Output is correct |
63 |
Correct |
1546 ms |
6408 KB |
Output is correct |
64 |
Correct |
1097 ms |
6248 KB |
Output is correct |
65 |
Correct |
1506 ms |
6268 KB |
Output is correct |
66 |
Correct |
1543 ms |
6248 KB |
Output is correct |
67 |
Correct |
1501 ms |
6248 KB |
Output is correct |
68 |
Correct |
1074 ms |
6120 KB |
Output is correct |
69 |
Correct |
1385 ms |
6364 KB |
Output is correct |
70 |
Correct |
1033 ms |
6244 KB |
Output is correct |
71 |
Correct |
1035 ms |
6376 KB |
Output is correct |
72 |
Correct |
1572 ms |
6376 KB |
Output is correct |
73 |
Correct |
1760 ms |
6364 KB |
Output is correct |