#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 12, MOD = (int)1e9 + 7;
int n, t[N * 2];
string s;
vector<int> x, y;
void add(int i, int val) {
while(i <= n + n) {
t[i] += val;
i += i & -i;
}
}
ll get(int i) {
ll ret = 0;
while(i) {
ret += t[i];
i -= i & -i;
}
return ret;
}
ll get(int l, int r) {
return get(r) - get(l - 1);
}
ll mem[N];
ll calc(int k) {
if(mem[k] != -1) {
return mem[k];
}
for(int i = 1; i <= n + n; i++) {
t[i] = 0;
}
ll ret = 0;
vector<pair<int,int>> a;
for(int i = 0; i < n; i++) {
int j = (i + k) % n;
int l = x[i], r = y[j];
if(l > r) {
swap(l, r);
}
a.emplace_back(l, -1);
a.emplace_back(r, l);
}
sort(a.begin(), a.end());
for(auto [x, y]:a) {
if(y == -1) {
add(x, 1);
} else {
ret += get(y + 1, x);
add(y, -1);
}
}
return mem[k] = ret;
}
void test() {
memset(mem, -1, sizeof(mem));
cin >> n >> s;
for(int i = 0; i < n + n; i++) {
if(s[i] == 'W') {
x.push_back(i + 1);
} else {
y.push_back(i + 1);
}
}
if(n == 1) {
cout << 0 << '\n';
return;
}
if(calc(0) < calc(1)) {
int l = 1, r = n;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(calc(mid) > calc(mid - 1)) {
l = mid;
} else {
r = mid;
}
}
cout << calc(l) << '\n';
} else {
int l = -1, r = n - 1;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(calc(mid) > calc(mid + 1)) {
r = mid;
} else {
l = mid;
}
}
cout << calc(r) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2616 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2616 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2756 KB |
Output is correct |
19 |
Correct |
1 ms |
2812 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2840 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2616 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2756 KB |
Output is correct |
19 |
Correct |
1 ms |
2812 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2840 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
5 ms |
2908 KB |
Output is correct |
35 |
Correct |
5 ms |
2920 KB |
Output is correct |
36 |
Correct |
6 ms |
2908 KB |
Output is correct |
37 |
Correct |
6 ms |
2908 KB |
Output is correct |
38 |
Correct |
4 ms |
2908 KB |
Output is correct |
39 |
Correct |
4 ms |
2908 KB |
Output is correct |
40 |
Correct |
4 ms |
2908 KB |
Output is correct |
41 |
Correct |
4 ms |
2908 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2908 KB |
Output is correct |
44 |
Correct |
4 ms |
2812 KB |
Output is correct |
45 |
Correct |
5 ms |
2920 KB |
Output is correct |
46 |
Correct |
5 ms |
2916 KB |
Output is correct |
47 |
Correct |
5 ms |
2904 KB |
Output is correct |
48 |
Correct |
5 ms |
2908 KB |
Output is correct |
49 |
Correct |
4 ms |
2920 KB |
Output is correct |
50 |
Correct |
4 ms |
2908 KB |
Output is correct |
51 |
Correct |
4 ms |
2908 KB |
Output is correct |
52 |
Correct |
3 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2616 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2756 KB |
Output is correct |
19 |
Correct |
1 ms |
2812 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2840 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
5 ms |
2908 KB |
Output is correct |
35 |
Correct |
5 ms |
2920 KB |
Output is correct |
36 |
Correct |
6 ms |
2908 KB |
Output is correct |
37 |
Correct |
6 ms |
2908 KB |
Output is correct |
38 |
Correct |
4 ms |
2908 KB |
Output is correct |
39 |
Correct |
4 ms |
2908 KB |
Output is correct |
40 |
Correct |
4 ms |
2908 KB |
Output is correct |
41 |
Correct |
4 ms |
2908 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2908 KB |
Output is correct |
44 |
Correct |
4 ms |
2812 KB |
Output is correct |
45 |
Correct |
5 ms |
2920 KB |
Output is correct |
46 |
Correct |
5 ms |
2916 KB |
Output is correct |
47 |
Correct |
5 ms |
2904 KB |
Output is correct |
48 |
Correct |
5 ms |
2908 KB |
Output is correct |
49 |
Correct |
4 ms |
2920 KB |
Output is correct |
50 |
Correct |
4 ms |
2908 KB |
Output is correct |
51 |
Correct |
4 ms |
2908 KB |
Output is correct |
52 |
Correct |
3 ms |
2908 KB |
Output is correct |
53 |
Correct |
809 ms |
13976 KB |
Output is correct |
54 |
Correct |
803 ms |
13980 KB |
Output is correct |
55 |
Correct |
760 ms |
13624 KB |
Output is correct |
56 |
Correct |
797 ms |
13704 KB |
Output is correct |
57 |
Correct |
855 ms |
13780 KB |
Output is correct |
58 |
Correct |
894 ms |
14012 KB |
Output is correct |
59 |
Correct |
761 ms |
13688 KB |
Output is correct |
60 |
Correct |
794 ms |
13716 KB |
Output is correct |
61 |
Correct |
764 ms |
13796 KB |
Output is correct |
62 |
Correct |
791 ms |
13812 KB |
Output is correct |
63 |
Correct |
792 ms |
13744 KB |
Output is correct |
64 |
Correct |
962 ms |
13712 KB |
Output is correct |
65 |
Correct |
753 ms |
13356 KB |
Output is correct |
66 |
Correct |
939 ms |
13592 KB |
Output is correct |
67 |
Correct |
969 ms |
13632 KB |
Output is correct |
68 |
Correct |
965 ms |
13316 KB |
Output is correct |
69 |
Correct |
866 ms |
13928 KB |
Output is correct |
70 |
Correct |
816 ms |
13352 KB |
Output is correct |
71 |
Correct |
802 ms |
13924 KB |
Output is correct |
72 |
Correct |
791 ms |
14096 KB |
Output is correct |
73 |
Correct |
1149 ms |
13992 KB |
Output is correct |