#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int ans;
int n;
string s;
vector<int> a, b;
int bit[400005];
void clr() {
for (int i = 1; i <= 2 * n; i++) {
bit[i] = 0;
}
}
void upd(int v, int k) {
while (v <= 2 * n) {
bit[v] += k;
v += (v & (-v));
}
}
int qry(int v) {
int res = 0;
while (v) {
res += bit[v];
v -= (v & (-v));
}
return res;
}
int sum(int l, int r) {
return qry(r) - qry(l - 1);
}
int calc(int k) {
int res = 0;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
v.push_back({a[i], b[(i + k) % n]});
}
for (int i = 0; i < v.size(); i++) {
if (v[i].first > v[i].second) {
swap(v[i].first, v[i].second);
}
}
sort(v.begin(), v.end());
clr();
for (int i = 0; i < v.size(); i++) {
res += sum(v[i].first, v[i].second);
upd(v[i].second, 1);
}
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'W') {
a.push_back(i + 1);
} else {
b.push_back(i + 1);
}
}
ans = max(calc(0), calc(n - 1));
int l = 0, r = n - 2;
while (l <= r) {
int m = (l + r) / 2;
int a = calc(m);
int b = calc(m + 1);
ans = max(ans, max(a, b));
if (a < b) {
l = m + 1;
} else {
r = m - 1;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
monochrome.cpp: In function 'long long int calc(long long int)':
monochrome.cpp:45:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
monochrome.cpp:52:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
5 ms |
332 KB |
Output is correct |
35 |
Correct |
6 ms |
460 KB |
Output is correct |
36 |
Correct |
5 ms |
332 KB |
Output is correct |
37 |
Correct |
5 ms |
456 KB |
Output is correct |
38 |
Correct |
4 ms |
332 KB |
Output is correct |
39 |
Correct |
5 ms |
332 KB |
Output is correct |
40 |
Correct |
4 ms |
332 KB |
Output is correct |
41 |
Correct |
4 ms |
332 KB |
Output is correct |
42 |
Correct |
5 ms |
460 KB |
Output is correct |
43 |
Correct |
5 ms |
332 KB |
Output is correct |
44 |
Correct |
5 ms |
452 KB |
Output is correct |
45 |
Correct |
4 ms |
452 KB |
Output is correct |
46 |
Correct |
5 ms |
332 KB |
Output is correct |
47 |
Correct |
6 ms |
452 KB |
Output is correct |
48 |
Correct |
4 ms |
460 KB |
Output is correct |
49 |
Correct |
4 ms |
460 KB |
Output is correct |
50 |
Correct |
5 ms |
332 KB |
Output is correct |
51 |
Correct |
3 ms |
452 KB |
Output is correct |
52 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
5 ms |
332 KB |
Output is correct |
35 |
Correct |
6 ms |
460 KB |
Output is correct |
36 |
Correct |
5 ms |
332 KB |
Output is correct |
37 |
Correct |
5 ms |
456 KB |
Output is correct |
38 |
Correct |
4 ms |
332 KB |
Output is correct |
39 |
Correct |
5 ms |
332 KB |
Output is correct |
40 |
Correct |
4 ms |
332 KB |
Output is correct |
41 |
Correct |
4 ms |
332 KB |
Output is correct |
42 |
Correct |
5 ms |
460 KB |
Output is correct |
43 |
Correct |
5 ms |
332 KB |
Output is correct |
44 |
Correct |
5 ms |
452 KB |
Output is correct |
45 |
Correct |
4 ms |
452 KB |
Output is correct |
46 |
Correct |
5 ms |
332 KB |
Output is correct |
47 |
Correct |
6 ms |
452 KB |
Output is correct |
48 |
Correct |
4 ms |
460 KB |
Output is correct |
49 |
Correct |
4 ms |
460 KB |
Output is correct |
50 |
Correct |
5 ms |
332 KB |
Output is correct |
51 |
Correct |
3 ms |
452 KB |
Output is correct |
52 |
Correct |
5 ms |
460 KB |
Output is correct |
53 |
Correct |
1203 ms |
14220 KB |
Output is correct |
54 |
Correct |
1219 ms |
14880 KB |
Output is correct |
55 |
Correct |
1075 ms |
14280 KB |
Output is correct |
56 |
Correct |
1121 ms |
14348 KB |
Output is correct |
57 |
Correct |
946 ms |
14524 KB |
Output is correct |
58 |
Correct |
908 ms |
14688 KB |
Output is correct |
59 |
Correct |
960 ms |
14332 KB |
Output is correct |
60 |
Correct |
988 ms |
14340 KB |
Output is correct |
61 |
Correct |
1103 ms |
14444 KB |
Output is correct |
62 |
Correct |
1016 ms |
14604 KB |
Output is correct |
63 |
Correct |
1086 ms |
14292 KB |
Output is correct |
64 |
Correct |
847 ms |
14324 KB |
Output is correct |
65 |
Correct |
1078 ms |
13888 KB |
Output is correct |
66 |
Correct |
999 ms |
14216 KB |
Output is correct |
67 |
Correct |
1040 ms |
14168 KB |
Output is correct |
68 |
Correct |
821 ms |
13636 KB |
Output is correct |
69 |
Correct |
917 ms |
14664 KB |
Output is correct |
70 |
Correct |
865 ms |
13724 KB |
Output is correct |
71 |
Correct |
1229 ms |
14780 KB |
Output is correct |
72 |
Correct |
796 ms |
14776 KB |
Output is correct |
73 |
Correct |
1207 ms |
14884 KB |
Output is correct |