#include<bits/stdc++.h>
using namespace std;
vector<long long> pr(200010);
long long n;
void dude(long long l, long long r, long long x, long long c, bool yeah) {
if(r < l) {
return;
}
if(yeah) {
l-=x;
r-=x;
}
else {
l = x-l;
r = x-r;
swap(l,r);
}
if(l < 0) {
l+=n;
}
if(r < 0) {
r+=n;
}
if(l <= r) {
pr[l]+=c;
pr[r+1]-=c;
}
else {
pr[l]+=c;
pr[n]-=c;
pr[0]+=c;
pr[r+1]-=c;
}
}
void calc(bool yeah, vector<long long> p, vector<long long> q) {
for(long long i = 1; i <= n; i++) {
long long c = p[i];
long long l = 0,r = n,p1,p2;
while(l < r) {
long long mid = (l+r+1)/2;
if(q[mid] > c) {
r = mid-1;
}
else {
l = mid;
}
}
p1 = l;
if(c <= n) {
l = 0;
r = n;
while(l < r) {
long long mid = (l+r+1)/2;
if(q[mid] > c+n) {
r = mid-1;
}
else {
l = mid;
}
}
p2 = l;
dude(1,p1,i,c,yeah);
dude(p1+1,p2,i,-c,yeah);
if(yeah) {
dude(p2+1,n,i,2*n+c,yeah);
}
else {
dude(p2+1,n,i,c,yeah);
}
}
else {
l = 0;
r = n;
while(l < r) {
long long mid = (l+r+1)/2;
if(q[mid] < c-n) {
l = mid;
}
else {
r = mid-1;
}
}
p2 = l;
if(yeah) {
dude(1,p2,i,2*n-c,yeah);
}
else {
dude(1,p2,i,-c,yeah);
}
dude(p2+1,p1,i,c,yeah);
dude(p1+1,n,i,-c,yeah);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
string s;
cin >> s;
vector<long long> p(1);
vector<long long> q(1);
for(long long i = 0; i < 2*n; i++) {
if(s[i] == 'B') {
p.push_back(i+1);
}
else {
q.push_back(i+1);
}
}
p.push_back(LLONG_MAX);
q.push_back(LLONG_MAX);
calc(1,p,q);
calc(0,q,p);
long long c = 0,ans = 0;
for(long long i = 0; i < n; i++) {
c+=pr[i];
ans = max(ans,(c-n)/2);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1900 KB |
Output is correct |
24 |
Correct |
1 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1900 KB |
Output is correct |
24 |
Correct |
1 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
1 ms |
1884 KB |
Output is correct |
34 |
Correct |
2 ms |
1884 KB |
Output is correct |
35 |
Correct |
1 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1884 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
1884 KB |
Output is correct |
39 |
Correct |
1 ms |
2008 KB |
Output is correct |
40 |
Correct |
1 ms |
1884 KB |
Output is correct |
41 |
Correct |
1 ms |
1944 KB |
Output is correct |
42 |
Correct |
1 ms |
1884 KB |
Output is correct |
43 |
Correct |
1 ms |
1884 KB |
Output is correct |
44 |
Correct |
1 ms |
1884 KB |
Output is correct |
45 |
Correct |
1 ms |
1884 KB |
Output is correct |
46 |
Correct |
1 ms |
1884 KB |
Output is correct |
47 |
Correct |
1 ms |
1884 KB |
Output is correct |
48 |
Correct |
1 ms |
1884 KB |
Output is correct |
49 |
Correct |
1 ms |
1884 KB |
Output is correct |
50 |
Correct |
1 ms |
1884 KB |
Output is correct |
51 |
Correct |
1 ms |
1884 KB |
Output is correct |
52 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1900 KB |
Output is correct |
24 |
Correct |
1 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
1 ms |
1884 KB |
Output is correct |
34 |
Correct |
2 ms |
1884 KB |
Output is correct |
35 |
Correct |
1 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1884 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
1884 KB |
Output is correct |
39 |
Correct |
1 ms |
2008 KB |
Output is correct |
40 |
Correct |
1 ms |
1884 KB |
Output is correct |
41 |
Correct |
1 ms |
1944 KB |
Output is correct |
42 |
Correct |
1 ms |
1884 KB |
Output is correct |
43 |
Correct |
1 ms |
1884 KB |
Output is correct |
44 |
Correct |
1 ms |
1884 KB |
Output is correct |
45 |
Correct |
1 ms |
1884 KB |
Output is correct |
46 |
Correct |
1 ms |
1884 KB |
Output is correct |
47 |
Correct |
1 ms |
1884 KB |
Output is correct |
48 |
Correct |
1 ms |
1884 KB |
Output is correct |
49 |
Correct |
1 ms |
1884 KB |
Output is correct |
50 |
Correct |
1 ms |
1884 KB |
Output is correct |
51 |
Correct |
1 ms |
1884 KB |
Output is correct |
52 |
Correct |
1 ms |
1884 KB |
Output is correct |
53 |
Correct |
36 ms |
9648 KB |
Output is correct |
54 |
Correct |
36 ms |
9436 KB |
Output is correct |
55 |
Correct |
36 ms |
8920 KB |
Output is correct |
56 |
Correct |
34 ms |
9176 KB |
Output is correct |
57 |
Correct |
26 ms |
8832 KB |
Output is correct |
58 |
Correct |
22 ms |
9060 KB |
Output is correct |
59 |
Correct |
19 ms |
8924 KB |
Output is correct |
60 |
Correct |
27 ms |
9120 KB |
Output is correct |
61 |
Correct |
29 ms |
8896 KB |
Output is correct |
62 |
Correct |
26 ms |
9144 KB |
Output is correct |
63 |
Correct |
33 ms |
9176 KB |
Output is correct |
64 |
Correct |
27 ms |
8808 KB |
Output is correct |
65 |
Correct |
34 ms |
8612 KB |
Output is correct |
66 |
Correct |
27 ms |
8796 KB |
Output is correct |
67 |
Correct |
28 ms |
8808 KB |
Output is correct |
68 |
Correct |
27 ms |
8296 KB |
Output is correct |
69 |
Correct |
22 ms |
9056 KB |
Output is correct |
70 |
Correct |
22 ms |
8580 KB |
Output is correct |
71 |
Correct |
26 ms |
9060 KB |
Output is correct |
72 |
Correct |
33 ms |
9064 KB |
Output is correct |
73 |
Correct |
38 ms |
9436 KB |
Output is correct |