#include <bits/stdc++.h>
#define int long long
using namespace std;
struct SegTree {
vector<int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
}
void cons() {
for(int i = MAX/2-1;i>=1;i--) {
seg[i] = seg[2*i] + seg[2*i+1];
}
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = seg[2*n] + seg[2*n+1];
n /= 2;
}
}
void Edit(int n, int a) {
seg[n+MAX/2] += a;
update(n);
}
int sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return 0;
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return sum(s, e, 2*n, ns, mid) + sum(s, e, 2*n+1, mid, ne);
}
int sum(int s, int e) {
return sum(s, e, 1, 0, MAX/2);
}
};
int simulate(vector<int> V) {
SegTree tree(V.size()+5);
vector<int> used;
used.resize(V.size()/2);
fill(used.begin(),used.end(),-1);
int ans = 0;
int i;
for(i=0;i<V.size();i++) {
if(used[V[i]] != -1) {
ans += tree.sum(used[V[i]]+1, i);
tree.Edit(used[V[i]],-1);
}
else {
tree.Edit(i, 1);
used[V[i]] = i;
}
}
return ans;
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
string s2;
cin >> N >> s2;
int i, j, k;
vector<int> A, B;
for(i=0;i<2*N;i++) {
if(s2[i]=='B') A.push_back(i);
else B.push_back(i);
}
vector<int> V;
V.resize(s2.length());
for(i=0;i<A.size();i++) V[A[i]] = i;
int ans = -1;
int s = 0, e = N - 1;
while(e-s>=3) {
int l = (2*s+e)/3;
int r = (s+2*e)/3;
for(int j = l; j < l + N; j++) {
V[B[j%N]] = j-l;
}
int v1 = simulate(V);
for(int j = r; j < r + N; j++) {
V[B[j%N]] = j-r;
}
int v2 = simulate(V);
if(v1<v2) s = l;
else e = r;
}
for(i=s;i<=e;i++) {
for(int j = i; j < i + N; j++) {
V[B[j%N]] = j-i;
}
ans = max(ans, simulate(V));
}
cout << ans;
}
Compilation message
monochrome.cpp: In member function 'long long int SegTree::sum(long long int, long long int, long long int, long long int, long long int)':
monochrome.cpp:33:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid = ns + ne >> 1;
| ~~~^~~~
monochrome.cpp: In function 'long long int simulate(std::vector<long long int>)':
monochrome.cpp:47:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(i=0;i<V.size();i++) {
| ~^~~~~~~~~
monochrome.cpp: In function 'int main()':
monochrome.cpp:74:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(i=0;i<A.size();i++) V[A[i]] = i;
| ~^~~~~~~~~
monochrome.cpp:66:12: warning: unused variable 'j' [-Wunused-variable]
66 | int i, j, k;
| ^
monochrome.cpp:66:15: warning: unused variable 'k' [-Wunused-variable]
66 | int i, j, k;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
312 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
352 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
312 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
352 KB |
Output is correct |
31 |
Correct |
3 ms |
304 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
312 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
352 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
312 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
352 KB |
Output is correct |
31 |
Correct |
3 ms |
304 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
332 KB |
Output is correct |
34 |
Correct |
17 ms |
440 KB |
Output is correct |
35 |
Correct |
17 ms |
444 KB |
Output is correct |
36 |
Correct |
16 ms |
460 KB |
Output is correct |
37 |
Correct |
16 ms |
436 KB |
Output is correct |
38 |
Correct |
14 ms |
460 KB |
Output is correct |
39 |
Correct |
16 ms |
452 KB |
Output is correct |
40 |
Correct |
15 ms |
460 KB |
Output is correct |
41 |
Correct |
17 ms |
440 KB |
Output is correct |
42 |
Correct |
16 ms |
460 KB |
Output is correct |
43 |
Correct |
16 ms |
460 KB |
Output is correct |
44 |
Correct |
17 ms |
452 KB |
Output is correct |
45 |
Correct |
17 ms |
460 KB |
Output is correct |
46 |
Correct |
17 ms |
460 KB |
Output is correct |
47 |
Correct |
16 ms |
440 KB |
Output is correct |
48 |
Correct |
14 ms |
444 KB |
Output is correct |
49 |
Correct |
14 ms |
460 KB |
Output is correct |
50 |
Correct |
15 ms |
460 KB |
Output is correct |
51 |
Correct |
15 ms |
460 KB |
Output is correct |
52 |
Correct |
16 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
312 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
352 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
312 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
352 KB |
Output is correct |
31 |
Correct |
3 ms |
304 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
332 KB |
Output is correct |
34 |
Correct |
17 ms |
440 KB |
Output is correct |
35 |
Correct |
17 ms |
444 KB |
Output is correct |
36 |
Correct |
16 ms |
460 KB |
Output is correct |
37 |
Correct |
16 ms |
436 KB |
Output is correct |
38 |
Correct |
14 ms |
460 KB |
Output is correct |
39 |
Correct |
16 ms |
452 KB |
Output is correct |
40 |
Correct |
15 ms |
460 KB |
Output is correct |
41 |
Correct |
17 ms |
440 KB |
Output is correct |
42 |
Correct |
16 ms |
460 KB |
Output is correct |
43 |
Correct |
16 ms |
460 KB |
Output is correct |
44 |
Correct |
17 ms |
452 KB |
Output is correct |
45 |
Correct |
17 ms |
460 KB |
Output is correct |
46 |
Correct |
17 ms |
460 KB |
Output is correct |
47 |
Correct |
16 ms |
440 KB |
Output is correct |
48 |
Correct |
14 ms |
444 KB |
Output is correct |
49 |
Correct |
14 ms |
460 KB |
Output is correct |
50 |
Correct |
15 ms |
460 KB |
Output is correct |
51 |
Correct |
15 ms |
460 KB |
Output is correct |
52 |
Correct |
16 ms |
440 KB |
Output is correct |
53 |
Execution timed out |
2075 ms |
20780 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |