#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
int N;
char S[MAXN*2+10];
int A[MAXN+10], B[MAXN+10], L[MAXN*2+10], R[MAXN*2+10];
ll P[MAXN+10], ans, off;
ll tree[MAXN+10];
void init() { memset(tree, 0, sizeof(tree)); }
void update(int i, int k) { for(; i<=N; i+=(i&-i)) tree[i]+=k; }
ll query(int i) { ll ret=0; for(i=R[i]+1; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
void update2(int l, int r, int k)
{
//printf("%d %d -> %d %d\n", l, r, R[l]+1, L[r]+1);
l=R[l]+1; r=L[r]+1;
if(l<=r) update(l, k), update(r+1, -k);
else update(l, k), update(r+1, -k), update(1, k);
}
int maxcnt;
struct dequeue
{
int pt=0;
vector<pii> V;
void push_back(pii x) { V.push_back(x); }
void pop_back() { V.pop_back(); }
void pop_front() { pt++; }
pii front() { return V[pt]; }
bool empty() { return pt==V.size(); }
};
ll f(int j)
{
j=(j+off)%N;
if(P[j]!=-1) return P[j];
int p=0, q=0;
dequeue DQ;
ll now=0;
init();
for(int i=0; i<N; i++)
{
int u=A[i], v=B[(i+j)%N];
while(!DQ.empty() && DQ.front().second<u)
{
update2(DQ.front().second, DQ.front().first, -1);
update2(DQ.front().first, DQ.front().second, 1);
DQ.pop_front();
}
now+=query(v);
update2(v, u, 1);
if(u<v) DQ.push_back({u, v});
}
ans=max(ans, now);
return P[j]=now;
}
int main()
{
memset(P, -1, sizeof(P));
scanf("%d", &N);
scanf(" %s", S+1);
memset(L, -1, sizeof(L));
memset(R, -1, sizeof(R));
int ca=0, cb=0;
for(int i=1; i<=2*N; i++)
{
if(S[i]=='B') A[ca++]=i;
else B[cb]=i, L[i]=R[i]=cb++;
}
R[2*N+1]=0;
for(int i=2*N; i>=1; i--) if(R[i]==-1) R[i]=R[i+1];
L[0]=N-1;
for(int i=1; i<=2*N; i++) if(L[i]==-1) L[i]=L[i-1];
//for(int i=1; i<=2*N; i++) printf("%d ", L[i]); printf("\n");
//for(int i=1; i<=2*N; i++) printf("%d ", R[i]); printf("\n");
off=N-1;
int lo=0, hi=N-1;
while(lo+2<hi)
{
int m1=(lo*2+hi)/3, m2=(lo+hi*2)/3;
if(f(m1)>f(m2)) hi=m2;
else lo=m1;
}
for(int i=lo; i<=hi; i++) f(i);
printf("%lld\n", ans);
}
Compilation message
monochrome.cpp: In member function 'bool dequeue::empty()':
monochrome.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | bool empty() { return pt==V.size(); }
| ~~^~~~~~~~~~
monochrome.cpp: In function 'll f(int)':
monochrome.cpp:46:6: warning: unused variable 'p' [-Wunused-variable]
46 | int p=0, q=0;
| ^
monochrome.cpp:46:11: warning: unused variable 'q' [-Wunused-variable]
46 | int p=0, q=0;
| ^
monochrome.cpp: In function 'int main()':
monochrome.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
monochrome.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
72 | scanf(" %s", S+1);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6528 KB |
Output is correct |
2 |
Correct |
5 ms |
6528 KB |
Output is correct |
3 |
Correct |
4 ms |
6528 KB |
Output is correct |
4 |
Correct |
4 ms |
6528 KB |
Output is correct |
5 |
Correct |
4 ms |
6528 KB |
Output is correct |
6 |
Correct |
5 ms |
6528 KB |
Output is correct |
7 |
Correct |
4 ms |
6528 KB |
Output is correct |
8 |
Correct |
4 ms |
6528 KB |
Output is correct |
9 |
Correct |
5 ms |
6528 KB |
Output is correct |
10 |
Correct |
5 ms |
6528 KB |
Output is correct |
11 |
Correct |
5 ms |
6528 KB |
Output is correct |
12 |
Correct |
5 ms |
6528 KB |
Output is correct |
13 |
Correct |
5 ms |
6528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6528 KB |
Output is correct |
2 |
Correct |
5 ms |
6528 KB |
Output is correct |
3 |
Correct |
4 ms |
6528 KB |
Output is correct |
4 |
Correct |
4 ms |
6528 KB |
Output is correct |
5 |
Correct |
4 ms |
6528 KB |
Output is correct |
6 |
Correct |
5 ms |
6528 KB |
Output is correct |
7 |
Correct |
4 ms |
6528 KB |
Output is correct |
8 |
Correct |
4 ms |
6528 KB |
Output is correct |
9 |
Correct |
5 ms |
6528 KB |
Output is correct |
10 |
Correct |
5 ms |
6528 KB |
Output is correct |
11 |
Correct |
5 ms |
6528 KB |
Output is correct |
12 |
Correct |
5 ms |
6528 KB |
Output is correct |
13 |
Correct |
5 ms |
6528 KB |
Output is correct |
14 |
Correct |
6 ms |
6656 KB |
Output is correct |
15 |
Correct |
6 ms |
6656 KB |
Output is correct |
16 |
Correct |
6 ms |
6628 KB |
Output is correct |
17 |
Correct |
6 ms |
6656 KB |
Output is correct |
18 |
Correct |
5 ms |
6528 KB |
Output is correct |
19 |
Correct |
6 ms |
6528 KB |
Output is correct |
20 |
Correct |
5 ms |
6656 KB |
Output is correct |
21 |
Correct |
5 ms |
6528 KB |
Output is correct |
22 |
Correct |
5 ms |
6656 KB |
Output is correct |
23 |
Correct |
6 ms |
6528 KB |
Output is correct |
24 |
Correct |
5 ms |
6528 KB |
Output is correct |
25 |
Correct |
7 ms |
6628 KB |
Output is correct |
26 |
Correct |
6 ms |
6528 KB |
Output is correct |
27 |
Correct |
6 ms |
6632 KB |
Output is correct |
28 |
Correct |
7 ms |
6656 KB |
Output is correct |
29 |
Correct |
5 ms |
6528 KB |
Output is correct |
30 |
Correct |
6 ms |
6528 KB |
Output is correct |
31 |
Correct |
6 ms |
6656 KB |
Output is correct |
32 |
Correct |
7 ms |
6656 KB |
Output is correct |
33 |
Correct |
7 ms |
6528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6528 KB |
Output is correct |
2 |
Correct |
5 ms |
6528 KB |
Output is correct |
3 |
Correct |
4 ms |
6528 KB |
Output is correct |
4 |
Correct |
4 ms |
6528 KB |
Output is correct |
5 |
Correct |
4 ms |
6528 KB |
Output is correct |
6 |
Correct |
5 ms |
6528 KB |
Output is correct |
7 |
Correct |
4 ms |
6528 KB |
Output is correct |
8 |
Correct |
4 ms |
6528 KB |
Output is correct |
9 |
Correct |
5 ms |
6528 KB |
Output is correct |
10 |
Correct |
5 ms |
6528 KB |
Output is correct |
11 |
Correct |
5 ms |
6528 KB |
Output is correct |
12 |
Correct |
5 ms |
6528 KB |
Output is correct |
13 |
Correct |
5 ms |
6528 KB |
Output is correct |
14 |
Correct |
6 ms |
6656 KB |
Output is correct |
15 |
Correct |
6 ms |
6656 KB |
Output is correct |
16 |
Correct |
6 ms |
6628 KB |
Output is correct |
17 |
Correct |
6 ms |
6656 KB |
Output is correct |
18 |
Correct |
5 ms |
6528 KB |
Output is correct |
19 |
Correct |
6 ms |
6528 KB |
Output is correct |
20 |
Correct |
5 ms |
6656 KB |
Output is correct |
21 |
Correct |
5 ms |
6528 KB |
Output is correct |
22 |
Correct |
5 ms |
6656 KB |
Output is correct |
23 |
Correct |
6 ms |
6528 KB |
Output is correct |
24 |
Correct |
5 ms |
6528 KB |
Output is correct |
25 |
Correct |
7 ms |
6628 KB |
Output is correct |
26 |
Correct |
6 ms |
6528 KB |
Output is correct |
27 |
Correct |
6 ms |
6632 KB |
Output is correct |
28 |
Correct |
7 ms |
6656 KB |
Output is correct |
29 |
Correct |
5 ms |
6528 KB |
Output is correct |
30 |
Correct |
6 ms |
6528 KB |
Output is correct |
31 |
Correct |
6 ms |
6656 KB |
Output is correct |
32 |
Correct |
7 ms |
6656 KB |
Output is correct |
33 |
Correct |
7 ms |
6528 KB |
Output is correct |
34 |
Correct |
11 ms |
6656 KB |
Output is correct |
35 |
Correct |
11 ms |
6656 KB |
Output is correct |
36 |
Correct |
11 ms |
6656 KB |
Output is correct |
37 |
Correct |
11 ms |
6656 KB |
Output is correct |
38 |
Correct |
11 ms |
6584 KB |
Output is correct |
39 |
Correct |
13 ms |
6656 KB |
Output is correct |
40 |
Correct |
14 ms |
6656 KB |
Output is correct |
41 |
Correct |
10 ms |
6656 KB |
Output is correct |
42 |
Correct |
14 ms |
6656 KB |
Output is correct |
43 |
Correct |
10 ms |
6684 KB |
Output is correct |
44 |
Correct |
11 ms |
6692 KB |
Output is correct |
45 |
Correct |
15 ms |
6656 KB |
Output is correct |
46 |
Correct |
14 ms |
6656 KB |
Output is correct |
47 |
Correct |
8 ms |
6656 KB |
Output is correct |
48 |
Correct |
13 ms |
6656 KB |
Output is correct |
49 |
Correct |
8 ms |
6656 KB |
Output is correct |
50 |
Correct |
8 ms |
6656 KB |
Output is correct |
51 |
Correct |
8 ms |
6656 KB |
Output is correct |
52 |
Correct |
11 ms |
6656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6528 KB |
Output is correct |
2 |
Correct |
5 ms |
6528 KB |
Output is correct |
3 |
Correct |
4 ms |
6528 KB |
Output is correct |
4 |
Correct |
4 ms |
6528 KB |
Output is correct |
5 |
Correct |
4 ms |
6528 KB |
Output is correct |
6 |
Correct |
5 ms |
6528 KB |
Output is correct |
7 |
Correct |
4 ms |
6528 KB |
Output is correct |
8 |
Correct |
4 ms |
6528 KB |
Output is correct |
9 |
Correct |
5 ms |
6528 KB |
Output is correct |
10 |
Correct |
5 ms |
6528 KB |
Output is correct |
11 |
Correct |
5 ms |
6528 KB |
Output is correct |
12 |
Correct |
5 ms |
6528 KB |
Output is correct |
13 |
Correct |
5 ms |
6528 KB |
Output is correct |
14 |
Correct |
6 ms |
6656 KB |
Output is correct |
15 |
Correct |
6 ms |
6656 KB |
Output is correct |
16 |
Correct |
6 ms |
6628 KB |
Output is correct |
17 |
Correct |
6 ms |
6656 KB |
Output is correct |
18 |
Correct |
5 ms |
6528 KB |
Output is correct |
19 |
Correct |
6 ms |
6528 KB |
Output is correct |
20 |
Correct |
5 ms |
6656 KB |
Output is correct |
21 |
Correct |
5 ms |
6528 KB |
Output is correct |
22 |
Correct |
5 ms |
6656 KB |
Output is correct |
23 |
Correct |
6 ms |
6528 KB |
Output is correct |
24 |
Correct |
5 ms |
6528 KB |
Output is correct |
25 |
Correct |
7 ms |
6628 KB |
Output is correct |
26 |
Correct |
6 ms |
6528 KB |
Output is correct |
27 |
Correct |
6 ms |
6632 KB |
Output is correct |
28 |
Correct |
7 ms |
6656 KB |
Output is correct |
29 |
Correct |
5 ms |
6528 KB |
Output is correct |
30 |
Correct |
6 ms |
6528 KB |
Output is correct |
31 |
Correct |
6 ms |
6656 KB |
Output is correct |
32 |
Correct |
7 ms |
6656 KB |
Output is correct |
33 |
Correct |
7 ms |
6528 KB |
Output is correct |
34 |
Correct |
11 ms |
6656 KB |
Output is correct |
35 |
Correct |
11 ms |
6656 KB |
Output is correct |
36 |
Correct |
11 ms |
6656 KB |
Output is correct |
37 |
Correct |
11 ms |
6656 KB |
Output is correct |
38 |
Correct |
11 ms |
6584 KB |
Output is correct |
39 |
Correct |
13 ms |
6656 KB |
Output is correct |
40 |
Correct |
14 ms |
6656 KB |
Output is correct |
41 |
Correct |
10 ms |
6656 KB |
Output is correct |
42 |
Correct |
14 ms |
6656 KB |
Output is correct |
43 |
Correct |
10 ms |
6684 KB |
Output is correct |
44 |
Correct |
11 ms |
6692 KB |
Output is correct |
45 |
Correct |
15 ms |
6656 KB |
Output is correct |
46 |
Correct |
14 ms |
6656 KB |
Output is correct |
47 |
Correct |
8 ms |
6656 KB |
Output is correct |
48 |
Correct |
13 ms |
6656 KB |
Output is correct |
49 |
Correct |
8 ms |
6656 KB |
Output is correct |
50 |
Correct |
8 ms |
6656 KB |
Output is correct |
51 |
Correct |
8 ms |
6656 KB |
Output is correct |
52 |
Correct |
11 ms |
6656 KB |
Output is correct |
53 |
Correct |
1763 ms |
11912 KB |
Output is correct |
54 |
Correct |
1735 ms |
11780 KB |
Output is correct |
55 |
Correct |
1536 ms |
10384 KB |
Output is correct |
56 |
Correct |
1570 ms |
11504 KB |
Output is correct |
57 |
Correct |
1197 ms |
10396 KB |
Output is correct |
58 |
Correct |
1859 ms |
12160 KB |
Output is correct |
59 |
Correct |
1363 ms |
10452 KB |
Output is correct |
60 |
Correct |
1452 ms |
11632 KB |
Output is correct |
61 |
Correct |
1461 ms |
10408 KB |
Output is correct |
62 |
Correct |
1430 ms |
11620 KB |
Output is correct |
63 |
Correct |
1450 ms |
10436 KB |
Output is correct |
64 |
Correct |
1167 ms |
12120 KB |
Output is correct |
65 |
Correct |
1391 ms |
11424 KB |
Output is correct |
66 |
Execution timed out |
2079 ms |
12144 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |