#include <bits/stdc++.h>
using namespace std;
const int BASE = 1 << 12;
int TREE[BASE << 1];
int n;
string monochrome;
queue<int> BPOS;
queue<int> WPOS;
void add(int v, int val){
v+=BASE;
TREE[v] += val;
v/=2;
while(v > 0){
int l = 2*v, r = 2*v+1;
TREE[v] = TREE[l] + TREE[r];
v/=2;
}
}
int query(int a, int b){
a += BASE - 1;
b += BASE + 1;
int res = 0;
while(a/2 != b/2){
if(!(a&1)) res += TREE[a+1];
if(b&1) res += TREE[b-1];
a/=2; b/=2;
}
return res;
}
void treeclear(){
for(int i = 0; i < (BASE << 1); i++)
TREE[i] = 0;
}
int solve(int wsp){
treeclear();
for(int i = n+wsp; i >= 1 + wsp; i--){
if(monochrome[i] == 'B')
BPOS.push(i);
else if(monochrome[i] == 'W')
WPOS.push(i);
}
int res = 0;
for(int i = 2*n+wsp; i >= n+1 + wsp; i--){
if(monochrome[i] == 'B'){
int v = WPOS.front();
WPOS.pop();
//cout << "query: " << query(v, BASE-1) << "\n";
res += query(v, BASE-1);
add(v, 1);
}
else if(monochrome[i] == 'W'){
int v = BPOS.front();
BPOS.pop();
//cout << "query: " << query(v, BASE-1) << "\n";
res += query(v, BASE-1);
add(v, 1);
}
}
return res;
}
int best(){
int res = 0;
for(int i = 0; i <= n; i++)
res = max(res, solve(i));
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
cin >> monochrome;
monochrome = monochrome + monochrome;
monochrome = "#" + monochrome;
/*
for(int i = n; i >= 1; i--){
if(monochrome[i] == 'B')
BPOS.push(i);
else if(monochrome[i] == 'W')
WPOS.push(i);
}
*/
cout << best() << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
496 KB |
Output is correct |
17 |
Correct |
5 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
600 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
488 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
5 ms |
496 KB |
Output is correct |
32 |
Correct |
5 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
496 KB |
Output is correct |
17 |
Correct |
5 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
600 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
488 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
5 ms |
496 KB |
Output is correct |
32 |
Correct |
5 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
34 |
Correct |
292 ms |
480 KB |
Output is correct |
35 |
Correct |
287 ms |
496 KB |
Output is correct |
36 |
Correct |
277 ms |
596 KB |
Output is correct |
37 |
Correct |
256 ms |
544 KB |
Output is correct |
38 |
Correct |
241 ms |
348 KB |
Output is correct |
39 |
Correct |
271 ms |
488 KB |
Output is correct |
40 |
Correct |
227 ms |
484 KB |
Output is correct |
41 |
Correct |
206 ms |
488 KB |
Output is correct |
42 |
Correct |
273 ms |
348 KB |
Output is correct |
43 |
Correct |
279 ms |
344 KB |
Output is correct |
44 |
Correct |
252 ms |
484 KB |
Output is correct |
45 |
Correct |
246 ms |
344 KB |
Output is correct |
46 |
Correct |
269 ms |
348 KB |
Output is correct |
47 |
Correct |
265 ms |
348 KB |
Output is correct |
48 |
Correct |
300 ms |
344 KB |
Output is correct |
49 |
Correct |
244 ms |
492 KB |
Output is correct |
50 |
Correct |
287 ms |
348 KB |
Output is correct |
51 |
Correct |
287 ms |
348 KB |
Output is correct |
52 |
Correct |
296 ms |
484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
496 KB |
Output is correct |
17 |
Correct |
5 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
600 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
488 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
5 ms |
496 KB |
Output is correct |
32 |
Correct |
5 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
34 |
Correct |
292 ms |
480 KB |
Output is correct |
35 |
Correct |
287 ms |
496 KB |
Output is correct |
36 |
Correct |
277 ms |
596 KB |
Output is correct |
37 |
Correct |
256 ms |
544 KB |
Output is correct |
38 |
Correct |
241 ms |
348 KB |
Output is correct |
39 |
Correct |
271 ms |
488 KB |
Output is correct |
40 |
Correct |
227 ms |
484 KB |
Output is correct |
41 |
Correct |
206 ms |
488 KB |
Output is correct |
42 |
Correct |
273 ms |
348 KB |
Output is correct |
43 |
Correct |
279 ms |
344 KB |
Output is correct |
44 |
Correct |
252 ms |
484 KB |
Output is correct |
45 |
Correct |
246 ms |
344 KB |
Output is correct |
46 |
Correct |
269 ms |
348 KB |
Output is correct |
47 |
Correct |
265 ms |
348 KB |
Output is correct |
48 |
Correct |
300 ms |
344 KB |
Output is correct |
49 |
Correct |
244 ms |
492 KB |
Output is correct |
50 |
Correct |
287 ms |
348 KB |
Output is correct |
51 |
Correct |
287 ms |
348 KB |
Output is correct |
52 |
Correct |
296 ms |
484 KB |
Output is correct |
53 |
Runtime error |
5 ms |
4304 KB |
Execution killed with signal 11 |
54 |
Halted |
0 ms |
0 KB |
- |