#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll INF = 1e18;
struct Seg{
int tree[800800], sz;
void init(int n){
sz = n;
fill(tree, tree+sz*2, 0);
}
void update(int p, int x){
for (tree[p+=sz]+=x;p>1;p>>=1) tree[p>>1] = tree[p] + tree[p^1];
}
int query(int l, int r){
int ret = 0;
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret += tree[l++];
if (r&1) ret += tree[--r];
}
return ret;
}
}tree;
char a[400400];
int B[200200], W[200200], arr[400400];
vector<pair<int, int>> E;
ll calc(int n){
for (auto &p:E){
if (p.first > p.second) swap(p.first, p.second);
arr[p.first] = -1;
arr[p.second] = p.first;
}
tree.init(n+1);
ll ret = 0;
for (int i=1;i<=n;i++){
if (arr[i]==-1) tree.update(i, 1);
else{
tree.update(arr[i], -1);
ret += tree.query(arr[i]+1, i);
}
}
return ret;
}
ll calc2(int n, int z){
E.clear();
int pt = z;
for (int i=1;i<=n/2;i++){
E.emplace_back(B[i], W[pt]);
pt++;
if (pt>n/2) pt -= n/2;
}
return calc(n);
}
int main(){
int n;
scanf("%d", &n);
scanf("%s", a+1);
n *= 2;
int cb = 0, cw = 0;
for (int i=1;i<=n;i++){
if (a[i]=='B') B[++cb] = i;
else W[++cw] = i;
}
ll ans = 0;
if (n<=4000){
for (int z=1;z<=n/2;z++){
ans = max(ans, calc2(n, z));
}
}
else{
int l = 1, r = n/2;
while(r-l>=10){
int m1 = (l*2+r)/3, m2 = (l+r*2)/3;
if (calc2(n, m1)<calc2(n, m2)) l = m1+1;
else r = m2-1;
}
for (int z=l;z<=r;z++) ans = max(ans, calc2(n, z));
}
printf("%lld\n", ans);
return 0;
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
monochrome.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%s", a+1);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
8 ms |
332 KB |
Output is correct |
15 |
Correct |
8 ms |
332 KB |
Output is correct |
16 |
Correct |
8 ms |
336 KB |
Output is correct |
17 |
Correct |
7 ms |
336 KB |
Output is correct |
18 |
Correct |
5 ms |
332 KB |
Output is correct |
19 |
Correct |
6 ms |
332 KB |
Output is correct |
20 |
Correct |
5 ms |
332 KB |
Output is correct |
21 |
Correct |
5 ms |
336 KB |
Output is correct |
22 |
Correct |
6 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
5 ms |
332 KB |
Output is correct |
25 |
Correct |
5 ms |
332 KB |
Output is correct |
26 |
Correct |
6 ms |
332 KB |
Output is correct |
27 |
Correct |
6 ms |
332 KB |
Output is correct |
28 |
Correct |
6 ms |
332 KB |
Output is correct |
29 |
Correct |
5 ms |
332 KB |
Output is correct |
30 |
Correct |
6 ms |
340 KB |
Output is correct |
31 |
Correct |
6 ms |
332 KB |
Output is correct |
32 |
Correct |
6 ms |
332 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
8 ms |
332 KB |
Output is correct |
15 |
Correct |
8 ms |
332 KB |
Output is correct |
16 |
Correct |
8 ms |
336 KB |
Output is correct |
17 |
Correct |
7 ms |
336 KB |
Output is correct |
18 |
Correct |
5 ms |
332 KB |
Output is correct |
19 |
Correct |
6 ms |
332 KB |
Output is correct |
20 |
Correct |
5 ms |
332 KB |
Output is correct |
21 |
Correct |
5 ms |
336 KB |
Output is correct |
22 |
Correct |
6 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
5 ms |
332 KB |
Output is correct |
25 |
Correct |
5 ms |
332 KB |
Output is correct |
26 |
Correct |
6 ms |
332 KB |
Output is correct |
27 |
Correct |
6 ms |
332 KB |
Output is correct |
28 |
Correct |
6 ms |
332 KB |
Output is correct |
29 |
Correct |
5 ms |
332 KB |
Output is correct |
30 |
Correct |
6 ms |
340 KB |
Output is correct |
31 |
Correct |
6 ms |
332 KB |
Output is correct |
32 |
Correct |
6 ms |
332 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
387 ms |
396 KB |
Output is correct |
35 |
Correct |
383 ms |
408 KB |
Output is correct |
36 |
Correct |
357 ms |
392 KB |
Output is correct |
37 |
Correct |
344 ms |
396 KB |
Output is correct |
38 |
Correct |
236 ms |
332 KB |
Output is correct |
39 |
Correct |
267 ms |
332 KB |
Output is correct |
40 |
Correct |
265 ms |
400 KB |
Output is correct |
41 |
Correct |
259 ms |
384 KB |
Output is correct |
42 |
Correct |
360 ms |
396 KB |
Output is correct |
43 |
Correct |
290 ms |
332 KB |
Output is correct |
44 |
Correct |
278 ms |
388 KB |
Output is correct |
45 |
Correct |
282 ms |
332 KB |
Output is correct |
46 |
Correct |
299 ms |
396 KB |
Output is correct |
47 |
Correct |
310 ms |
452 KB |
Output is correct |
48 |
Correct |
284 ms |
400 KB |
Output is correct |
49 |
Correct |
225 ms |
332 KB |
Output is correct |
50 |
Correct |
268 ms |
392 KB |
Output is correct |
51 |
Correct |
266 ms |
396 KB |
Output is correct |
52 |
Correct |
273 ms |
396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
8 ms |
332 KB |
Output is correct |
15 |
Correct |
8 ms |
332 KB |
Output is correct |
16 |
Correct |
8 ms |
336 KB |
Output is correct |
17 |
Correct |
7 ms |
336 KB |
Output is correct |
18 |
Correct |
5 ms |
332 KB |
Output is correct |
19 |
Correct |
6 ms |
332 KB |
Output is correct |
20 |
Correct |
5 ms |
332 KB |
Output is correct |
21 |
Correct |
5 ms |
336 KB |
Output is correct |
22 |
Correct |
6 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
5 ms |
332 KB |
Output is correct |
25 |
Correct |
5 ms |
332 KB |
Output is correct |
26 |
Correct |
6 ms |
332 KB |
Output is correct |
27 |
Correct |
6 ms |
332 KB |
Output is correct |
28 |
Correct |
6 ms |
332 KB |
Output is correct |
29 |
Correct |
5 ms |
332 KB |
Output is correct |
30 |
Correct |
6 ms |
340 KB |
Output is correct |
31 |
Correct |
6 ms |
332 KB |
Output is correct |
32 |
Correct |
6 ms |
332 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
387 ms |
396 KB |
Output is correct |
35 |
Correct |
383 ms |
408 KB |
Output is correct |
36 |
Correct |
357 ms |
392 KB |
Output is correct |
37 |
Correct |
344 ms |
396 KB |
Output is correct |
38 |
Correct |
236 ms |
332 KB |
Output is correct |
39 |
Correct |
267 ms |
332 KB |
Output is correct |
40 |
Correct |
265 ms |
400 KB |
Output is correct |
41 |
Correct |
259 ms |
384 KB |
Output is correct |
42 |
Correct |
360 ms |
396 KB |
Output is correct |
43 |
Correct |
290 ms |
332 KB |
Output is correct |
44 |
Correct |
278 ms |
388 KB |
Output is correct |
45 |
Correct |
282 ms |
332 KB |
Output is correct |
46 |
Correct |
299 ms |
396 KB |
Output is correct |
47 |
Correct |
310 ms |
452 KB |
Output is correct |
48 |
Correct |
284 ms |
400 KB |
Output is correct |
49 |
Correct |
225 ms |
332 KB |
Output is correct |
50 |
Correct |
268 ms |
392 KB |
Output is correct |
51 |
Correct |
266 ms |
396 KB |
Output is correct |
52 |
Correct |
273 ms |
396 KB |
Output is correct |
53 |
Correct |
1392 ms |
8560 KB |
Output is correct |
54 |
Correct |
1395 ms |
9012 KB |
Output is correct |
55 |
Correct |
1290 ms |
8556 KB |
Output is correct |
56 |
Correct |
1333 ms |
8696 KB |
Output is correct |
57 |
Correct |
1018 ms |
8688 KB |
Output is correct |
58 |
Correct |
1065 ms |
8996 KB |
Output is correct |
59 |
Correct |
1046 ms |
8636 KB |
Output is correct |
60 |
Correct |
1068 ms |
8596 KB |
Output is correct |
61 |
Correct |
1134 ms |
8692 KB |
Output is correct |
62 |
Correct |
1214 ms |
8764 KB |
Output is correct |
63 |
Correct |
1283 ms |
8636 KB |
Output is correct |
64 |
Correct |
1067 ms |
8636 KB |
Output is correct |
65 |
Correct |
1161 ms |
8300 KB |
Output is correct |
66 |
Correct |
1151 ms |
8508 KB |
Output is correct |
67 |
Correct |
1151 ms |
8508 KB |
Output is correct |
68 |
Correct |
1076 ms |
8172 KB |
Output is correct |
69 |
Correct |
1027 ms |
8972 KB |
Output is correct |
70 |
Correct |
927 ms |
8252 KB |
Output is correct |
71 |
Correct |
1029 ms |
9008 KB |
Output is correct |
72 |
Correct |
1022 ms |
8996 KB |
Output is correct |
73 |
Correct |
1103 ms |
9016 KB |
Output is correct |