//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
const int nmax = 4e5 + 5, N = 1e5;
const ll oo = 1e9;
const int lg = 19, M = 2, mod = 1e6;
#define pii pair<ll, ll>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
#define endl "\n"
#define task "code"
using namespace std;
int n;
string s;
vector<int> one, two;
vector<pii> tmp;
struct holder{
vector<int> tmp;
int f[nmax];
void update(int x, int val){
for(; x <= 2 *n; x += x&-x) f[x] += val, tmp.push_back(x);
}
int get(int x){
int res = 0;
for(; x ; x -= x&-x) res += f[x];
return res;
}
void clear(){
for(auto p : tmp) f[p] = 0;
tmp.clear();
}
}F, G;
ll get(int x){
tmp.clear();
for(int i = 0; i < n; ++i){
int u = one[i];
int v = two[(i + x + n * 10) % n];
if(u > v) swap(u, v);
tmp.push_back({u, v});
// cout << u << ' ' << v << endl;
}
sort(tmp.begin(), tmp.end());
// for(auto [u, v] : tmp) cout << u << ' ' << v << endl;
ll ans = 0;
for(int i = tmp.size() - 1; i >= 0; --i){
ans += G.get(tmp[i].se) - F.get(tmp[i].se);
F.update(tmp[i].se, 1);
G.update(tmp[i].fi, 1);
}
F.clear();G.clear();
return ans;
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
cin >> n;
cin >> s;
for(int i = 0; i < s.size(); ++i){
if(s[i] == 'W') one.push_back(i + 1);
else two.push_back(i + 1);
}
// cout << get(0);
// for(int i = 0; i < n; ++i) cout << get(i) << ' ';
int l = 0, r = n - 2, kq = -1;
while(l <= r){
int mid = r + l >> 1;
if(get(mid) <= get(mid + 1)){
kq = mid;
l = mid + 1;
}
else r = mid - 1;
}
ll ans = 0;
for(int i = kq - 10; i <= kq + 10; ++i) ans = max(ans, get(i));
cout << ans;
}
/*
5
5 2 1 5 3
3
1 4
2 5
1 5
4
2 1 5 3
1
1 4
*/
Compilation message
monochrome.cpp:59:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
59 | main(){
| ^~~~
monochrome.cpp: In function 'int main()':
monochrome.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i = 0; i < s.size(); ++i){
| ~~^~~~~~~~~~
monochrome.cpp:74:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int mid = r + l >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
480 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
480 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
7 ms |
604 KB |
Output is correct |
36 |
Correct |
7 ms |
604 KB |
Output is correct |
37 |
Correct |
7 ms |
604 KB |
Output is correct |
38 |
Correct |
5 ms |
600 KB |
Output is correct |
39 |
Correct |
6 ms |
604 KB |
Output is correct |
40 |
Correct |
5 ms |
604 KB |
Output is correct |
41 |
Correct |
5 ms |
604 KB |
Output is correct |
42 |
Correct |
7 ms |
604 KB |
Output is correct |
43 |
Correct |
6 ms |
604 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
6 ms |
604 KB |
Output is correct |
46 |
Correct |
7 ms |
604 KB |
Output is correct |
47 |
Correct |
5 ms |
604 KB |
Output is correct |
48 |
Correct |
5 ms |
604 KB |
Output is correct |
49 |
Correct |
5 ms |
604 KB |
Output is correct |
50 |
Correct |
7 ms |
772 KB |
Output is correct |
51 |
Correct |
4 ms |
780 KB |
Output is correct |
52 |
Correct |
7 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
480 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
7 ms |
604 KB |
Output is correct |
36 |
Correct |
7 ms |
604 KB |
Output is correct |
37 |
Correct |
7 ms |
604 KB |
Output is correct |
38 |
Correct |
5 ms |
600 KB |
Output is correct |
39 |
Correct |
6 ms |
604 KB |
Output is correct |
40 |
Correct |
5 ms |
604 KB |
Output is correct |
41 |
Correct |
5 ms |
604 KB |
Output is correct |
42 |
Correct |
7 ms |
604 KB |
Output is correct |
43 |
Correct |
6 ms |
604 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
6 ms |
604 KB |
Output is correct |
46 |
Correct |
7 ms |
604 KB |
Output is correct |
47 |
Correct |
5 ms |
604 KB |
Output is correct |
48 |
Correct |
5 ms |
604 KB |
Output is correct |
49 |
Correct |
5 ms |
604 KB |
Output is correct |
50 |
Correct |
7 ms |
772 KB |
Output is correct |
51 |
Correct |
4 ms |
780 KB |
Output is correct |
52 |
Correct |
7 ms |
600 KB |
Output is correct |
53 |
Correct |
1470 ms |
30196 KB |
Output is correct |
54 |
Correct |
1451 ms |
23856 KB |
Output is correct |
55 |
Correct |
1371 ms |
22464 KB |
Output is correct |
56 |
Correct |
1355 ms |
22616 KB |
Output is correct |
57 |
Correct |
1085 ms |
22620 KB |
Output is correct |
58 |
Correct |
991 ms |
23444 KB |
Output is correct |
59 |
Correct |
1079 ms |
29480 KB |
Output is correct |
60 |
Correct |
1156 ms |
22624 KB |
Output is correct |
61 |
Correct |
1309 ms |
29520 KB |
Output is correct |
62 |
Correct |
1217 ms |
22932 KB |
Output is correct |
63 |
Correct |
1282 ms |
22576 KB |
Output is correct |
64 |
Correct |
979 ms |
22596 KB |
Output is correct |
65 |
Correct |
1292 ms |
28664 KB |
Output is correct |
66 |
Correct |
1150 ms |
29508 KB |
Output is correct |
67 |
Correct |
1251 ms |
29648 KB |
Output is correct |
68 |
Correct |
944 ms |
21412 KB |
Output is correct |
69 |
Correct |
1004 ms |
29840 KB |
Output is correct |
70 |
Correct |
977 ms |
28112 KB |
Output is correct |
71 |
Correct |
1420 ms |
22920 KB |
Output is correct |
72 |
Correct |
941 ms |
23004 KB |
Output is correct |
73 |
Correct |
1508 ms |
38612 KB |
Output is correct |