#include "bits/stdc++.h"
using namespace std;
const int N=400001;
int n;
string s;
int r[N], l[N];
int sum[4*N];
void upd(int i, int x, int lx, int rx){
sum[x]++;
if(rx-lx==1) return;
int m=(lx+rx)/2;
if(i<m) upd(i, 2*x+1, lx, m);
else upd(i, 2*x+2, m, rx);
}
void upd(int i) { upd(i, 0, 0, 2*n+1); }
int get_sum(int l, int r, int x, int lx, int rx){
if(lx>=r || rx<=l) return 0;
if(lx>=l && rx<=r) return sum[x];
int m=(lx+rx)/2;
return get_sum(l, r, 2*x+1, lx, m)+get_sum(l, r, 2*x+2, m, rx);
}
int get_sum(int l, int r) {
if(r<=l) return 0;
return get_sum(l, r, 0, 0, 2*n+1);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
long long ans2=0;
for(int t=0; t<n; t++){
vector<int> white, black;
for(int i=n; i<2*n; i++){
if(s[i]=='W') white.push_back(i+1);
else black.push_back(i+1);
}
int j=0, k=0;
for(int i=0; i<n; i++){
if(s[i]=='B') {
l[white[j]]=i+1, r[i+1]=white[j];
j++;
}
else{
l[black[k]]=i+1, r[i+1]=black[k];
k++;
}
}
// for(int i=0; i<n; i++){
// cout << "r[" << i+1 << "] = " << r[i+1] << "\n";
// }
long long ans=0;
for(int i=1; i<=2*n; i++){
if(r[i]) continue;
ans+=get_sum(l[i]+1, i);
upd(l[i]);
}
ans2=max(ans2, (long long)n*(n-1)/2-ans);
s+=s[0];
s.erase(s.begin());
// cout << s << "\n";
for(int i=0; i<8*n; i++) sum[i]=0;
for(int i=1; i<=2*n; i++){
l[i]=r[i]=0;
}
}
cout << ans2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
15 ms |
332 KB |
Output is correct |
15 |
Correct |
15 ms |
360 KB |
Output is correct |
16 |
Correct |
17 ms |
332 KB |
Output is correct |
17 |
Correct |
21 ms |
332 KB |
Output is correct |
18 |
Correct |
11 ms |
352 KB |
Output is correct |
19 |
Correct |
11 ms |
352 KB |
Output is correct |
20 |
Correct |
12 ms |
332 KB |
Output is correct |
21 |
Correct |
11 ms |
356 KB |
Output is correct |
22 |
Correct |
11 ms |
332 KB |
Output is correct |
23 |
Correct |
6 ms |
332 KB |
Output is correct |
24 |
Correct |
11 ms |
320 KB |
Output is correct |
25 |
Correct |
9 ms |
332 KB |
Output is correct |
26 |
Correct |
11 ms |
352 KB |
Output is correct |
27 |
Correct |
11 ms |
332 KB |
Output is correct |
28 |
Correct |
16 ms |
360 KB |
Output is correct |
29 |
Correct |
10 ms |
332 KB |
Output is correct |
30 |
Correct |
9 ms |
360 KB |
Output is correct |
31 |
Correct |
12 ms |
332 KB |
Output is correct |
32 |
Correct |
12 ms |
352 KB |
Output is correct |
33 |
Correct |
12 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
15 ms |
332 KB |
Output is correct |
15 |
Correct |
15 ms |
360 KB |
Output is correct |
16 |
Correct |
17 ms |
332 KB |
Output is correct |
17 |
Correct |
21 ms |
332 KB |
Output is correct |
18 |
Correct |
11 ms |
352 KB |
Output is correct |
19 |
Correct |
11 ms |
352 KB |
Output is correct |
20 |
Correct |
12 ms |
332 KB |
Output is correct |
21 |
Correct |
11 ms |
356 KB |
Output is correct |
22 |
Correct |
11 ms |
332 KB |
Output is correct |
23 |
Correct |
6 ms |
332 KB |
Output is correct |
24 |
Correct |
11 ms |
320 KB |
Output is correct |
25 |
Correct |
9 ms |
332 KB |
Output is correct |
26 |
Correct |
11 ms |
352 KB |
Output is correct |
27 |
Correct |
11 ms |
332 KB |
Output is correct |
28 |
Correct |
16 ms |
360 KB |
Output is correct |
29 |
Correct |
10 ms |
332 KB |
Output is correct |
30 |
Correct |
9 ms |
360 KB |
Output is correct |
31 |
Correct |
12 ms |
332 KB |
Output is correct |
32 |
Correct |
12 ms |
352 KB |
Output is correct |
33 |
Correct |
12 ms |
332 KB |
Output is correct |
34 |
Correct |
813 ms |
448 KB |
Output is correct |
35 |
Correct |
840 ms |
460 KB |
Output is correct |
36 |
Correct |
786 ms |
464 KB |
Output is correct |
37 |
Correct |
742 ms |
440 KB |
Output is correct |
38 |
Correct |
616 ms |
332 KB |
Output is correct |
39 |
Correct |
731 ms |
448 KB |
Output is correct |
40 |
Correct |
640 ms |
432 KB |
Output is correct |
41 |
Correct |
593 ms |
388 KB |
Output is correct |
42 |
Correct |
807 ms |
452 KB |
Output is correct |
43 |
Correct |
754 ms |
440 KB |
Output is correct |
44 |
Correct |
683 ms |
432 KB |
Output is correct |
45 |
Correct |
673 ms |
448 KB |
Output is correct |
46 |
Correct |
745 ms |
448 KB |
Output is correct |
47 |
Correct |
715 ms |
452 KB |
Output is correct |
48 |
Correct |
792 ms |
444 KB |
Output is correct |
49 |
Correct |
653 ms |
448 KB |
Output is correct |
50 |
Correct |
704 ms |
444 KB |
Output is correct |
51 |
Correct |
728 ms |
444 KB |
Output is correct |
52 |
Correct |
708 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
15 ms |
332 KB |
Output is correct |
15 |
Correct |
15 ms |
360 KB |
Output is correct |
16 |
Correct |
17 ms |
332 KB |
Output is correct |
17 |
Correct |
21 ms |
332 KB |
Output is correct |
18 |
Correct |
11 ms |
352 KB |
Output is correct |
19 |
Correct |
11 ms |
352 KB |
Output is correct |
20 |
Correct |
12 ms |
332 KB |
Output is correct |
21 |
Correct |
11 ms |
356 KB |
Output is correct |
22 |
Correct |
11 ms |
332 KB |
Output is correct |
23 |
Correct |
6 ms |
332 KB |
Output is correct |
24 |
Correct |
11 ms |
320 KB |
Output is correct |
25 |
Correct |
9 ms |
332 KB |
Output is correct |
26 |
Correct |
11 ms |
352 KB |
Output is correct |
27 |
Correct |
11 ms |
332 KB |
Output is correct |
28 |
Correct |
16 ms |
360 KB |
Output is correct |
29 |
Correct |
10 ms |
332 KB |
Output is correct |
30 |
Correct |
9 ms |
360 KB |
Output is correct |
31 |
Correct |
12 ms |
332 KB |
Output is correct |
32 |
Correct |
12 ms |
352 KB |
Output is correct |
33 |
Correct |
12 ms |
332 KB |
Output is correct |
34 |
Correct |
813 ms |
448 KB |
Output is correct |
35 |
Correct |
840 ms |
460 KB |
Output is correct |
36 |
Correct |
786 ms |
464 KB |
Output is correct |
37 |
Correct |
742 ms |
440 KB |
Output is correct |
38 |
Correct |
616 ms |
332 KB |
Output is correct |
39 |
Correct |
731 ms |
448 KB |
Output is correct |
40 |
Correct |
640 ms |
432 KB |
Output is correct |
41 |
Correct |
593 ms |
388 KB |
Output is correct |
42 |
Correct |
807 ms |
452 KB |
Output is correct |
43 |
Correct |
754 ms |
440 KB |
Output is correct |
44 |
Correct |
683 ms |
432 KB |
Output is correct |
45 |
Correct |
673 ms |
448 KB |
Output is correct |
46 |
Correct |
745 ms |
448 KB |
Output is correct |
47 |
Correct |
715 ms |
452 KB |
Output is correct |
48 |
Correct |
792 ms |
444 KB |
Output is correct |
49 |
Correct |
653 ms |
448 KB |
Output is correct |
50 |
Correct |
704 ms |
444 KB |
Output is correct |
51 |
Correct |
728 ms |
444 KB |
Output is correct |
52 |
Correct |
708 ms |
436 KB |
Output is correct |
53 |
Execution timed out |
2068 ms |
12220 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |