//Challenge: Accepted
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 400005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
int a[maxn], p[maxn];
int bp[maxn], wp[maxn];
struct BIT{
int bit[maxn];
void init(int n) {
for (int i = 0;i <= n + 1;i++) bit[i] = 0;
}
void modify(int ind, int val) {
ind++;
for (;ind < maxn;ind += ind & (-ind)) bit[ind] += val;
}
int query(int ind) {
ind++;
int ret = 0;
for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
return ret;
}
} bit;
int main() {
io
int n;
cin >> n;
n *= 2;
string s;
cin >> s;
int mi = 0, mpos = 0, cur = 0;
for (int i = 0;i < n;i++) {
if (cur < mi) {
mi = cur;
mpos = i;
}
cur += (s[i] == 'B' ? 1 : -1);
}
int bi = 0, wi = 0;
for (int i = 0;i < n;i++) {
a[i] = (s[(mpos + i) % n] == 'B' ? 1 : -1);
if (a[i] == 1) bp[bi++] = i;
else wp[wi++] = i;
}
auto getval = [&](int x) {
for (int i = 0;i < n/2;i++) {
p[bp[i]] = (i < x ? 1 : -1);
p[wp[i]] = (i < n/2 - x ? 1 : -1);
}
int cur = 0;
bit.init(n);
ll ret = 0;
int bi = 0, wi = 0;
//debug(x);
//pary(p, p + n);
for (int i = 0;i < n;i++) {
cur += p[i];
if (cur < 0) return 0LL;
if (p[i] == 1) {
bit.modify(i, 1);
} else {
int cor = (a[i] > 0 ? wp[wi++] : bp[bi++]);
//debug(i, cor);
ret += bit.query(i) - bit.query(cor);
bit.modify(cor, -1);
}
}
//debug(ret);
return ret;
};
ll ans = 0;
for (int i = 0;i <= n / 2;i++) {
ans = max(ans, getval(i));
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
8 ms |
332 KB |
Output is correct |
15 |
Correct |
7 ms |
400 KB |
Output is correct |
16 |
Correct |
7 ms |
396 KB |
Output is correct |
17 |
Correct |
9 ms |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
340 KB |
Output is correct |
20 |
Correct |
6 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
400 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
328 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
340 KB |
Output is correct |
31 |
Correct |
6 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
340 KB |
Output is correct |
33 |
Correct |
8 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
8 ms |
332 KB |
Output is correct |
15 |
Correct |
7 ms |
400 KB |
Output is correct |
16 |
Correct |
7 ms |
396 KB |
Output is correct |
17 |
Correct |
9 ms |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
340 KB |
Output is correct |
20 |
Correct |
6 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
400 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
328 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
340 KB |
Output is correct |
31 |
Correct |
6 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
340 KB |
Output is correct |
33 |
Correct |
8 ms |
324 KB |
Output is correct |
34 |
Correct |
304 ms |
432 KB |
Output is correct |
35 |
Correct |
310 ms |
444 KB |
Output is correct |
36 |
Correct |
288 ms |
340 KB |
Output is correct |
37 |
Correct |
277 ms |
424 KB |
Output is correct |
38 |
Correct |
197 ms |
428 KB |
Output is correct |
39 |
Correct |
234 ms |
440 KB |
Output is correct |
40 |
Correct |
237 ms |
428 KB |
Output is correct |
41 |
Correct |
200 ms |
340 KB |
Output is correct |
42 |
Correct |
289 ms |
440 KB |
Output is correct |
43 |
Correct |
285 ms |
340 KB |
Output is correct |
44 |
Correct |
236 ms |
428 KB |
Output is correct |
45 |
Correct |
180 ms |
340 KB |
Output is correct |
46 |
Correct |
191 ms |
428 KB |
Output is correct |
47 |
Correct |
191 ms |
460 KB |
Output is correct |
48 |
Correct |
240 ms |
436 KB |
Output is correct |
49 |
Correct |
209 ms |
340 KB |
Output is correct |
50 |
Correct |
186 ms |
432 KB |
Output is correct |
51 |
Correct |
195 ms |
432 KB |
Output is correct |
52 |
Correct |
277 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
8 ms |
332 KB |
Output is correct |
15 |
Correct |
7 ms |
400 KB |
Output is correct |
16 |
Correct |
7 ms |
396 KB |
Output is correct |
17 |
Correct |
9 ms |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
340 KB |
Output is correct |
20 |
Correct |
6 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
400 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
328 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
340 KB |
Output is correct |
31 |
Correct |
6 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
340 KB |
Output is correct |
33 |
Correct |
8 ms |
324 KB |
Output is correct |
34 |
Correct |
304 ms |
432 KB |
Output is correct |
35 |
Correct |
310 ms |
444 KB |
Output is correct |
36 |
Correct |
288 ms |
340 KB |
Output is correct |
37 |
Correct |
277 ms |
424 KB |
Output is correct |
38 |
Correct |
197 ms |
428 KB |
Output is correct |
39 |
Correct |
234 ms |
440 KB |
Output is correct |
40 |
Correct |
237 ms |
428 KB |
Output is correct |
41 |
Correct |
200 ms |
340 KB |
Output is correct |
42 |
Correct |
289 ms |
440 KB |
Output is correct |
43 |
Correct |
285 ms |
340 KB |
Output is correct |
44 |
Correct |
236 ms |
428 KB |
Output is correct |
45 |
Correct |
180 ms |
340 KB |
Output is correct |
46 |
Correct |
191 ms |
428 KB |
Output is correct |
47 |
Correct |
191 ms |
460 KB |
Output is correct |
48 |
Correct |
240 ms |
436 KB |
Output is correct |
49 |
Correct |
209 ms |
340 KB |
Output is correct |
50 |
Correct |
186 ms |
432 KB |
Output is correct |
51 |
Correct |
195 ms |
432 KB |
Output is correct |
52 |
Correct |
277 ms |
452 KB |
Output is correct |
53 |
Execution timed out |
2089 ms |
7392 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |