#ifdef LC
#include "pch.h"
#else
#include <bits/stdc++.h>
#endif
using namespace std;
using ll = long long;
#define int ll
#define all(x) x.begin(), x.end()
#define x first
#define y second
const int INPUT_BUF = 1 << 12;
const int OUTPUT_BUF = 1 << 16;
char input[INPUT_BUF];
char output[OUTPUT_BUF];
int input_ptr = 0;
int input_len = 0;
int output_ptr = 0;
inline
bool check_eof() {
if (input_ptr == input_len) {
input_ptr = 0;
input_len = fread(input, 1, INPUT_BUF, stdin);
}
return input_ptr == input_len;
}
inline
char get_char() {
assert(!check_eof());
return input[input_ptr++];
}
inline
char read_char() {
char c = get_char();
while (c <= ' ') {
c = get_char();
}
return c;
}
inline
int read_int() {
int sgn = +1;
char c = read_char();
if (c == '-') {
sgn = -1;
c = get_char();
}
int x = 0;
while ('0' <= c && c <= '9') {
x = 10 * x + c - '0';
c = get_char();
}
return x * sgn;
}
inline
__attribute__((destructor))
void flush_output() {
fwrite(output, 1, output_ptr, stdout);
output_ptr = 0;
}
inline
void write(char c) {
if (output_ptr == OUTPUT_BUF) {
flush_output();
}
output[output_ptr++] = c;
}
inline
void write(signed x) {
static const int LEN = 20;
static char tmp[LEN];
int n = 0;
if (x < 0) {
write('-');
x *= -1;
}
while (!n || x) {
tmp[n++] = (char)(x % 10 + '0');
x /= 10;
}
while (n--) {
write(tmp[n]);
}
}
inline
void write(long long x) {
static const int LEN = 20;
static char tmp[LEN];
int n = 0;
if (x < 0) {
write('-');
x *= -1;
}
while (!n || x) {
tmp[n++] = (char)(x % 10 + '0');
x /= 10;
}
while (n--) {
write(tmp[n]);
}
}
inline
void write(string s) {
for (char c : s) {
write(c);
}
}
inline
void write(const char *s) {
for (const char *ptr = s; *ptr; ++ptr) {
write(*ptr);
}
}
const int N = 4e5 + 10;
int n;
char s[N];
vector<int> g[2];
int fen[N];
void clear() {
fill_n(fen, N, 0);
}
void add(int i) {
for (; i < N; i |= i + 1) {
++fen[i];
}
}
int sum(int i) {
int s = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) {
s += fen[i];
}
return s;
}
int go[N];
int mem[N];
int counter = 0;
int func(int k) {
++counter;
if (mem[k] != 0) {
return mem[k];
}
int s = 0;
for (int i = 0; i < (int)g[0].size(); ++i) {
int j = (i + k) % (int)g[1].size();
go[g[0][i]] = g[1][j];
go[g[1][j]] = g[0][i];
}
clear();
for (int i = n - 1; i >= 0; --i) {
if (go[i] <= i) {
s += sum(i) - sum(go[i]);
add(go[i]);
}
}
clear();
for (int i = 0; i < n; ++i) {
if (i <= go[i]) {
s += sum(go[i]) - sum(i);
add(go[i]);
}
}
return mem[k] = -(s / 2);
}
int der(int k) {
return func((k + 1) % n) - func(k);
}
int32_t main() {
#ifdef LC
assert(freopen("input.txt", "r", stdin));
//assert(freopen("output.txt", "w", stdout));
#endif
ios::sync_with_stdio(0); cin.tie(0);
n = read_int();
n *= 2;
for (int i = 0; i < n; ++i) {
if ((s[i] = read_char()) == 'W') {
g[0].push_back(i);
} else {
g[1].push_back(i);
}
}
/*
for (int i = 0; i < n / 2; ++i) {
cerr << func(i) << " ";
}
cerr << endl;
*/
int ans = min(func(0), func(n / 2 - 1));
if (n <= 4) {
ans = 0;
for (int i = 0; i < n / 2; ++i) {
ans = min(ans, func(i));
}
} else if (der(0) < 0) {
int lef = 0, rig = n / 2 - 1;
int bound = max(func(lef), func(rig));
while (rig - lef > 1) {
int mid = (lef + rig) / 2;
if (der(mid) < 0 && func(mid) <= bound) {
lef = mid;
} else {
rig = mid;
}
}
ans = min(ans, func(rig));
} else {
int lef = 0, rig = n / 2 - 1;
int bound = min(func(lef), func(rig));
while (rig - lef > 1) {
int mid = (lef + rig) / 2;
if (der(mid) < 0 || func(mid) >= bound) {
lef = mid;
} else {
rig = mid;
}
}
ans = min(ans, func(rig));
}
ans *= -1;
write(ans);
write('\n');
cerr << counter << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
4 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3456 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
5 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
5 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
6 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
4 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3456 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
5 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
5 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
6 ms |
3456 KB |
Output is correct |
14 |
Correct |
10 ms |
3456 KB |
Output is correct |
15 |
Correct |
10 ms |
3456 KB |
Output is correct |
16 |
Correct |
14 ms |
3456 KB |
Output is correct |
17 |
Correct |
9 ms |
3456 KB |
Output is correct |
18 |
Correct |
9 ms |
3456 KB |
Output is correct |
19 |
Correct |
9 ms |
3456 KB |
Output is correct |
20 |
Correct |
9 ms |
3456 KB |
Output is correct |
21 |
Correct |
9 ms |
3456 KB |
Output is correct |
22 |
Correct |
9 ms |
3456 KB |
Output is correct |
23 |
Correct |
9 ms |
3456 KB |
Output is correct |
24 |
Correct |
9 ms |
3456 KB |
Output is correct |
25 |
Correct |
9 ms |
3456 KB |
Output is correct |
26 |
Correct |
9 ms |
3456 KB |
Output is correct |
27 |
Correct |
9 ms |
3456 KB |
Output is correct |
28 |
Correct |
10 ms |
3456 KB |
Output is correct |
29 |
Correct |
9 ms |
3456 KB |
Output is correct |
30 |
Correct |
9 ms |
3456 KB |
Output is correct |
31 |
Correct |
10 ms |
3456 KB |
Output is correct |
32 |
Correct |
10 ms |
3456 KB |
Output is correct |
33 |
Correct |
11 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
4 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3456 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
5 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
5 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
6 ms |
3456 KB |
Output is correct |
14 |
Correct |
10 ms |
3456 KB |
Output is correct |
15 |
Correct |
10 ms |
3456 KB |
Output is correct |
16 |
Correct |
14 ms |
3456 KB |
Output is correct |
17 |
Correct |
9 ms |
3456 KB |
Output is correct |
18 |
Correct |
9 ms |
3456 KB |
Output is correct |
19 |
Correct |
9 ms |
3456 KB |
Output is correct |
20 |
Correct |
9 ms |
3456 KB |
Output is correct |
21 |
Correct |
9 ms |
3456 KB |
Output is correct |
22 |
Correct |
9 ms |
3456 KB |
Output is correct |
23 |
Correct |
9 ms |
3456 KB |
Output is correct |
24 |
Correct |
9 ms |
3456 KB |
Output is correct |
25 |
Correct |
9 ms |
3456 KB |
Output is correct |
26 |
Correct |
9 ms |
3456 KB |
Output is correct |
27 |
Correct |
9 ms |
3456 KB |
Output is correct |
28 |
Correct |
10 ms |
3456 KB |
Output is correct |
29 |
Correct |
9 ms |
3456 KB |
Output is correct |
30 |
Correct |
9 ms |
3456 KB |
Output is correct |
31 |
Correct |
10 ms |
3456 KB |
Output is correct |
32 |
Correct |
10 ms |
3456 KB |
Output is correct |
33 |
Correct |
11 ms |
3456 KB |
Output is correct |
34 |
Correct |
17 ms |
3584 KB |
Output is correct |
35 |
Correct |
16 ms |
3584 KB |
Output is correct |
36 |
Correct |
17 ms |
3584 KB |
Output is correct |
37 |
Correct |
16 ms |
3584 KB |
Output is correct |
38 |
Correct |
16 ms |
3584 KB |
Output is correct |
39 |
Correct |
17 ms |
3584 KB |
Output is correct |
40 |
Correct |
16 ms |
3584 KB |
Output is correct |
41 |
Correct |
15 ms |
3584 KB |
Output is correct |
42 |
Correct |
16 ms |
3584 KB |
Output is correct |
43 |
Correct |
17 ms |
3712 KB |
Output is correct |
44 |
Correct |
17 ms |
3584 KB |
Output is correct |
45 |
Correct |
15 ms |
3584 KB |
Output is correct |
46 |
Correct |
18 ms |
3712 KB |
Output is correct |
47 |
Correct |
17 ms |
3636 KB |
Output is correct |
48 |
Correct |
16 ms |
3584 KB |
Output is correct |
49 |
Correct |
17 ms |
3584 KB |
Output is correct |
50 |
Correct |
24 ms |
3584 KB |
Output is correct |
51 |
Correct |
16 ms |
3584 KB |
Output is correct |
52 |
Correct |
18 ms |
3584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
4 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3456 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
5 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
5 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
6 ms |
3456 KB |
Output is correct |
14 |
Correct |
10 ms |
3456 KB |
Output is correct |
15 |
Correct |
10 ms |
3456 KB |
Output is correct |
16 |
Correct |
14 ms |
3456 KB |
Output is correct |
17 |
Correct |
9 ms |
3456 KB |
Output is correct |
18 |
Correct |
9 ms |
3456 KB |
Output is correct |
19 |
Correct |
9 ms |
3456 KB |
Output is correct |
20 |
Correct |
9 ms |
3456 KB |
Output is correct |
21 |
Correct |
9 ms |
3456 KB |
Output is correct |
22 |
Correct |
9 ms |
3456 KB |
Output is correct |
23 |
Correct |
9 ms |
3456 KB |
Output is correct |
24 |
Correct |
9 ms |
3456 KB |
Output is correct |
25 |
Correct |
9 ms |
3456 KB |
Output is correct |
26 |
Correct |
9 ms |
3456 KB |
Output is correct |
27 |
Correct |
9 ms |
3456 KB |
Output is correct |
28 |
Correct |
10 ms |
3456 KB |
Output is correct |
29 |
Correct |
9 ms |
3456 KB |
Output is correct |
30 |
Correct |
9 ms |
3456 KB |
Output is correct |
31 |
Correct |
10 ms |
3456 KB |
Output is correct |
32 |
Correct |
10 ms |
3456 KB |
Output is correct |
33 |
Correct |
11 ms |
3456 KB |
Output is correct |
34 |
Correct |
17 ms |
3584 KB |
Output is correct |
35 |
Correct |
16 ms |
3584 KB |
Output is correct |
36 |
Correct |
17 ms |
3584 KB |
Output is correct |
37 |
Correct |
16 ms |
3584 KB |
Output is correct |
38 |
Correct |
16 ms |
3584 KB |
Output is correct |
39 |
Correct |
17 ms |
3584 KB |
Output is correct |
40 |
Correct |
16 ms |
3584 KB |
Output is correct |
41 |
Correct |
15 ms |
3584 KB |
Output is correct |
42 |
Correct |
16 ms |
3584 KB |
Output is correct |
43 |
Correct |
17 ms |
3712 KB |
Output is correct |
44 |
Correct |
17 ms |
3584 KB |
Output is correct |
45 |
Correct |
15 ms |
3584 KB |
Output is correct |
46 |
Correct |
18 ms |
3712 KB |
Output is correct |
47 |
Correct |
17 ms |
3636 KB |
Output is correct |
48 |
Correct |
16 ms |
3584 KB |
Output is correct |
49 |
Correct |
17 ms |
3584 KB |
Output is correct |
50 |
Correct |
24 ms |
3584 KB |
Output is correct |
51 |
Correct |
16 ms |
3584 KB |
Output is correct |
52 |
Correct |
18 ms |
3584 KB |
Output is correct |
53 |
Correct |
866 ms |
10352 KB |
Output is correct |
54 |
Correct |
883 ms |
11008 KB |
Output is correct |
55 |
Correct |
806 ms |
10468 KB |
Output is correct |
56 |
Correct |
825 ms |
10468 KB |
Output is correct |
57 |
Correct |
814 ms |
10588 KB |
Output is correct |
58 |
Correct |
821 ms |
11488 KB |
Output is correct |
59 |
Correct |
774 ms |
10580 KB |
Output is correct |
60 |
Correct |
854 ms |
10584 KB |
Output is correct |
61 |
Correct |
834 ms |
10540 KB |
Output is correct |
62 |
Correct |
840 ms |
10612 KB |
Output is correct |
63 |
Correct |
795 ms |
10576 KB |
Output is correct |
64 |
Correct |
774 ms |
10472 KB |
Output is correct |
65 |
Correct |
740 ms |
10324 KB |
Output is correct |
66 |
Correct |
790 ms |
10504 KB |
Output is correct |
67 |
Correct |
807 ms |
10484 KB |
Output is correct |
68 |
Correct |
750 ms |
10356 KB |
Output is correct |
69 |
Correct |
803 ms |
10848 KB |
Output is correct |
70 |
Correct |
768 ms |
10224 KB |
Output is correct |
71 |
Correct |
834 ms |
10980 KB |
Output is correct |
72 |
Correct |
819 ms |
10984 KB |
Output is correct |
73 |
Correct |
881 ms |
10844 KB |
Output is correct |