/* 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 > 100)
{
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 */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1860 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1860 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
7 ms |
1876 KB |
Output is correct |
15 |
Correct |
8 ms |
1876 KB |
Output is correct |
16 |
Correct |
8 ms |
1900 KB |
Output is correct |
17 |
Correct |
8 ms |
1904 KB |
Output is correct |
18 |
Correct |
6 ms |
1876 KB |
Output is correct |
19 |
Correct |
6 ms |
1876 KB |
Output is correct |
20 |
Correct |
7 ms |
1900 KB |
Output is correct |
21 |
Correct |
7 ms |
1876 KB |
Output is correct |
22 |
Correct |
7 ms |
1908 KB |
Output is correct |
23 |
Correct |
7 ms |
1892 KB |
Output is correct |
24 |
Correct |
8 ms |
1892 KB |
Output is correct |
25 |
Correct |
7 ms |
1892 KB |
Output is correct |
26 |
Correct |
9 ms |
1876 KB |
Output is correct |
27 |
Correct |
8 ms |
1904 KB |
Output is correct |
28 |
Correct |
7 ms |
1876 KB |
Output is correct |
29 |
Correct |
6 ms |
1876 KB |
Output is correct |
30 |
Correct |
8 ms |
1876 KB |
Output is correct |
31 |
Correct |
8 ms |
1896 KB |
Output is correct |
32 |
Correct |
7 ms |
1896 KB |
Output is correct |
33 |
Correct |
10 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1860 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
7 ms |
1876 KB |
Output is correct |
15 |
Correct |
8 ms |
1876 KB |
Output is correct |
16 |
Correct |
8 ms |
1900 KB |
Output is correct |
17 |
Correct |
8 ms |
1904 KB |
Output is correct |
18 |
Correct |
6 ms |
1876 KB |
Output is correct |
19 |
Correct |
6 ms |
1876 KB |
Output is correct |
20 |
Correct |
7 ms |
1900 KB |
Output is correct |
21 |
Correct |
7 ms |
1876 KB |
Output is correct |
22 |
Correct |
7 ms |
1908 KB |
Output is correct |
23 |
Correct |
7 ms |
1892 KB |
Output is correct |
24 |
Correct |
8 ms |
1892 KB |
Output is correct |
25 |
Correct |
7 ms |
1892 KB |
Output is correct |
26 |
Correct |
9 ms |
1876 KB |
Output is correct |
27 |
Correct |
8 ms |
1904 KB |
Output is correct |
28 |
Correct |
7 ms |
1876 KB |
Output is correct |
29 |
Correct |
6 ms |
1876 KB |
Output is correct |
30 |
Correct |
8 ms |
1876 KB |
Output is correct |
31 |
Correct |
8 ms |
1896 KB |
Output is correct |
32 |
Correct |
7 ms |
1896 KB |
Output is correct |
33 |
Correct |
10 ms |
1876 KB |
Output is correct |
34 |
Correct |
20 ms |
1876 KB |
Output is correct |
35 |
Correct |
31 ms |
1948 KB |
Output is correct |
36 |
Correct |
22 ms |
1876 KB |
Output is correct |
37 |
Correct |
20 ms |
1948 KB |
Output is correct |
38 |
Correct |
21 ms |
1948 KB |
Output is correct |
39 |
Correct |
19 ms |
1948 KB |
Output is correct |
40 |
Correct |
18 ms |
1948 KB |
Output is correct |
41 |
Correct |
16 ms |
1952 KB |
Output is correct |
42 |
Correct |
21 ms |
1944 KB |
Output is correct |
43 |
Correct |
24 ms |
1876 KB |
Output is correct |
44 |
Correct |
21 ms |
1952 KB |
Output is correct |
45 |
Correct |
20 ms |
1948 KB |
Output is correct |
46 |
Correct |
21 ms |
1944 KB |
Output is correct |
47 |
Correct |
18 ms |
1920 KB |
Output is correct |
48 |
Correct |
25 ms |
1876 KB |
Output is correct |
49 |
Correct |
24 ms |
1944 KB |
Output is correct |
50 |
Correct |
22 ms |
1944 KB |
Output is correct |
51 |
Correct |
22 ms |
1944 KB |
Output is correct |
52 |
Correct |
21 ms |
1948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1860 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
7 ms |
1876 KB |
Output is correct |
15 |
Correct |
8 ms |
1876 KB |
Output is correct |
16 |
Correct |
8 ms |
1900 KB |
Output is correct |
17 |
Correct |
8 ms |
1904 KB |
Output is correct |
18 |
Correct |
6 ms |
1876 KB |
Output is correct |
19 |
Correct |
6 ms |
1876 KB |
Output is correct |
20 |
Correct |
7 ms |
1900 KB |
Output is correct |
21 |
Correct |
7 ms |
1876 KB |
Output is correct |
22 |
Correct |
7 ms |
1908 KB |
Output is correct |
23 |
Correct |
7 ms |
1892 KB |
Output is correct |
24 |
Correct |
8 ms |
1892 KB |
Output is correct |
25 |
Correct |
7 ms |
1892 KB |
Output is correct |
26 |
Correct |
9 ms |
1876 KB |
Output is correct |
27 |
Correct |
8 ms |
1904 KB |
Output is correct |
28 |
Correct |
7 ms |
1876 KB |
Output is correct |
29 |
Correct |
6 ms |
1876 KB |
Output is correct |
30 |
Correct |
8 ms |
1876 KB |
Output is correct |
31 |
Correct |
8 ms |
1896 KB |
Output is correct |
32 |
Correct |
7 ms |
1896 KB |
Output is correct |
33 |
Correct |
10 ms |
1876 KB |
Output is correct |
34 |
Correct |
20 ms |
1876 KB |
Output is correct |
35 |
Correct |
31 ms |
1948 KB |
Output is correct |
36 |
Correct |
22 ms |
1876 KB |
Output is correct |
37 |
Correct |
20 ms |
1948 KB |
Output is correct |
38 |
Correct |
21 ms |
1948 KB |
Output is correct |
39 |
Correct |
19 ms |
1948 KB |
Output is correct |
40 |
Correct |
18 ms |
1948 KB |
Output is correct |
41 |
Correct |
16 ms |
1952 KB |
Output is correct |
42 |
Correct |
21 ms |
1944 KB |
Output is correct |
43 |
Correct |
24 ms |
1876 KB |
Output is correct |
44 |
Correct |
21 ms |
1952 KB |
Output is correct |
45 |
Correct |
20 ms |
1948 KB |
Output is correct |
46 |
Correct |
21 ms |
1944 KB |
Output is correct |
47 |
Correct |
18 ms |
1920 KB |
Output is correct |
48 |
Correct |
25 ms |
1876 KB |
Output is correct |
49 |
Correct |
24 ms |
1944 KB |
Output is correct |
50 |
Correct |
22 ms |
1944 KB |
Output is correct |
51 |
Correct |
22 ms |
1944 KB |
Output is correct |
52 |
Correct |
21 ms |
1948 KB |
Output is correct |
53 |
Execution timed out |
2059 ms |
5980 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |