#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
typedef long long ll;
typedef pair<ll , ll> pll;
const ll maxn = 4e5 + 17;
ll n;
string s;
vector<ll> a , b;
ll ps[maxn] , c[maxn];
ll cal(ll k){
if(c[k] != -1) return c[k];
ll cur = 0;
for(ll t = 0 ; t < n ; t++){
ll i = a[t] , j = b[(t + k) % n];
if(i < j){
ll h1 = ps[j] - ps[i] , h0 = j - i - 1 - h1 , b0 = n - 1 - h0 , h = max(0ll , h1 - b0);
cur += j - i - 1 - (h << 1);
} else {
ll d = (n << 1) - (i - j + 1) , h1 = n - ps[i] + ps[j] , h0 = d - h1 , b0 = n - 1 - h0 , h = max(0ll , h1 - b0);
cur += d - (h << 1);
}
}
cur >>= 1;
return c[k] = cur;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
memset(c , -1 , sizeof(c));
cin>>n>>s;
for(ll i = 0 ; i < (n << 1) ; i++){
if(s[i] == 'B'){
a.push_back(i);
} else {
b.push_back(i);
}
}
ps[0] = (s[0] == 'B');
for(ll i = 1 ; i < (n << 1) ; i++){
ps[i] = ps[i - 1] + (s[i] == 'B');
}
ll sq = 500;
ll res = 0;
for(ll i = 0 ; i < n ; i += sq){
c[i] = cal(i);
res = max(res , c[i]);
}
bool f = false;
for(ll i = sq ; i < n ; i += sq){
f |= (c[i - sq] < c[i]);
if(c[i - sq] >= c[i] && f){
for(ll j = max(0ll , i - (sq << 1)) ; j <= i ; j++){
res = max(res , cal(j));
}
cout<<res<<'\n';
return 0;
}
}
for(ll i = max(0ll , n - (sq << 1)) ; i < n ; i++){
res = max(res , cal(i));
}
for(ll i = 0 ; i < sq ; i++){
res = max(res , cal(i));
}
cout<<res<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
1 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
1 ms |
3412 KB |
Output is correct |
9 |
Correct |
1 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
1 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
1 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
1 ms |
3412 KB |
Output is correct |
9 |
Correct |
1 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
1 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
3 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3412 KB |
Output is correct |
27 |
Correct |
2 ms |
3412 KB |
Output is correct |
28 |
Correct |
2 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3412 KB |
Output is correct |
30 |
Correct |
2 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
1 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
1 ms |
3412 KB |
Output is correct |
9 |
Correct |
1 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
1 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
3 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3412 KB |
Output is correct |
27 |
Correct |
2 ms |
3412 KB |
Output is correct |
28 |
Correct |
2 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3412 KB |
Output is correct |
30 |
Correct |
2 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
34 |
Correct |
11 ms |
3540 KB |
Output is correct |
35 |
Correct |
11 ms |
3540 KB |
Output is correct |
36 |
Correct |
11 ms |
3556 KB |
Output is correct |
37 |
Correct |
11 ms |
3556 KB |
Output is correct |
38 |
Correct |
10 ms |
3540 KB |
Output is correct |
39 |
Correct |
11 ms |
3540 KB |
Output is correct |
40 |
Correct |
10 ms |
3540 KB |
Output is correct |
41 |
Correct |
9 ms |
3544 KB |
Output is correct |
42 |
Correct |
11 ms |
3540 KB |
Output is correct |
43 |
Correct |
17 ms |
3432 KB |
Output is correct |
44 |
Correct |
10 ms |
3540 KB |
Output is correct |
45 |
Correct |
15 ms |
3544 KB |
Output is correct |
46 |
Correct |
15 ms |
3540 KB |
Output is correct |
47 |
Correct |
15 ms |
3540 KB |
Output is correct |
48 |
Correct |
11 ms |
3540 KB |
Output is correct |
49 |
Correct |
10 ms |
3540 KB |
Output is correct |
50 |
Correct |
16 ms |
3548 KB |
Output is correct |
51 |
Correct |
15 ms |
3540 KB |
Output is correct |
52 |
Correct |
11 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
1 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
1 ms |
3412 KB |
Output is correct |
9 |
Correct |
1 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
1 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
3 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3412 KB |
Output is correct |
27 |
Correct |
2 ms |
3412 KB |
Output is correct |
28 |
Correct |
2 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3412 KB |
Output is correct |
30 |
Correct |
2 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
34 |
Correct |
11 ms |
3540 KB |
Output is correct |
35 |
Correct |
11 ms |
3540 KB |
Output is correct |
36 |
Correct |
11 ms |
3556 KB |
Output is correct |
37 |
Correct |
11 ms |
3556 KB |
Output is correct |
38 |
Correct |
10 ms |
3540 KB |
Output is correct |
39 |
Correct |
11 ms |
3540 KB |
Output is correct |
40 |
Correct |
10 ms |
3540 KB |
Output is correct |
41 |
Correct |
9 ms |
3544 KB |
Output is correct |
42 |
Correct |
11 ms |
3540 KB |
Output is correct |
43 |
Correct |
17 ms |
3432 KB |
Output is correct |
44 |
Correct |
10 ms |
3540 KB |
Output is correct |
45 |
Correct |
15 ms |
3544 KB |
Output is correct |
46 |
Correct |
15 ms |
3540 KB |
Output is correct |
47 |
Correct |
15 ms |
3540 KB |
Output is correct |
48 |
Correct |
11 ms |
3540 KB |
Output is correct |
49 |
Correct |
10 ms |
3540 KB |
Output is correct |
50 |
Correct |
16 ms |
3548 KB |
Output is correct |
51 |
Correct |
15 ms |
3540 KB |
Output is correct |
52 |
Correct |
11 ms |
3540 KB |
Output is correct |
53 |
Correct |
1281 ms |
10608 KB |
Output is correct |
54 |
Correct |
1275 ms |
10884 KB |
Output is correct |
55 |
Correct |
1194 ms |
10580 KB |
Output is correct |
56 |
Correct |
1215 ms |
10620 KB |
Output is correct |
57 |
Correct |
1235 ms |
10332 KB |
Output is correct |
58 |
Correct |
1273 ms |
10632 KB |
Output is correct |
59 |
Correct |
1217 ms |
10588 KB |
Output is correct |
60 |
Correct |
1224 ms |
10392 KB |
Output is correct |
61 |
Correct |
1241 ms |
10448 KB |
Output is correct |
62 |
Correct |
1238 ms |
10464 KB |
Output is correct |
63 |
Correct |
1233 ms |
10580 KB |
Output is correct |
64 |
Correct |
1229 ms |
10344 KB |
Output is correct |
65 |
Correct |
1153 ms |
10052 KB |
Output is correct |
66 |
Correct |
1202 ms |
10268 KB |
Output is correct |
67 |
Correct |
1228 ms |
10284 KB |
Output is correct |
68 |
Correct |
1128 ms |
9960 KB |
Output is correct |
69 |
Correct |
1288 ms |
10584 KB |
Output is correct |
70 |
Correct |
1148 ms |
9992 KB |
Output is correct |
71 |
Correct |
1747 ms |
10708 KB |
Output is correct |
72 |
Correct |
1733 ms |
10636 KB |
Output is correct |
73 |
Correct |
1298 ms |
10892 KB |
Output is correct |