#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 12, MOD = (int)1e9 + 7;
int n, t[N * 2];
string s;
vector<int> x, y;
void add(int i, int val) {
while(i <= n + n) {
t[i] += val;
i += i & -i;
}
}
ll get(int i) {
ll ret = 0;
while(i) {
ret += t[i];
i -= i & -i;
}
return ret;
}
ll get(int l, int r) {
return get(r) - get(l - 1);
}
ll calc(int k) {
for(int i = 1; i <= n + n; i++) {
t[i] = 0;
}
ll ret = 0;
vector<pair<int,int>> a;
for(int i = 0; i < n; i++) {
int j = (i + k) % n;
int l = x[i], r = y[j];
if(l > r) {
swap(l, r);
}
a.emplace_back(l, -1);
a.emplace_back(r, l);
}
// return 0;
sort(a.begin(), a.end());
for(auto [x, y]:a) {
if(y == -1) {
add(x, 1);
} else {
ret += get(y + 1, x);
add(y, -1);
}
}
return ret;
}
void test() {
cin >> n >> s;
for(int i = 0; i < n + n; i++) {
if(s[i] == 'W') {
x.push_back(i + 1);
} else {
y.push_back(i + 1);
}
}
ll res = 0;
for(int i = 0; i < n; i++) {
// cout << i << ' ' << calc(i) << '\n';
res = max(res, calc(i));
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
7 ms |
488 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
11 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
488 KB |
Output is correct |
22 |
Correct |
5 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
484 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
8 ms |
484 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
6 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
7 ms |
488 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
11 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
488 KB |
Output is correct |
22 |
Correct |
5 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
484 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
8 ms |
484 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
6 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
34 |
Correct |
365 ms |
344 KB |
Output is correct |
35 |
Correct |
362 ms |
548 KB |
Output is correct |
36 |
Correct |
325 ms |
344 KB |
Output is correct |
37 |
Correct |
321 ms |
344 KB |
Output is correct |
38 |
Correct |
256 ms |
360 KB |
Output is correct |
39 |
Correct |
292 ms |
348 KB |
Output is correct |
40 |
Correct |
224 ms |
540 KB |
Output is correct |
41 |
Correct |
233 ms |
596 KB |
Output is correct |
42 |
Correct |
335 ms |
548 KB |
Output is correct |
43 |
Correct |
329 ms |
344 KB |
Output is correct |
44 |
Correct |
271 ms |
344 KB |
Output is correct |
45 |
Correct |
303 ms |
344 KB |
Output is correct |
46 |
Correct |
324 ms |
344 KB |
Output is correct |
47 |
Correct |
326 ms |
544 KB |
Output is correct |
48 |
Correct |
337 ms |
348 KB |
Output is correct |
49 |
Correct |
250 ms |
596 KB |
Output is correct |
50 |
Correct |
293 ms |
544 KB |
Output is correct |
51 |
Correct |
288 ms |
540 KB |
Output is correct |
52 |
Correct |
268 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
7 ms |
488 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
11 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
488 KB |
Output is correct |
22 |
Correct |
5 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
484 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
8 ms |
484 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
6 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
34 |
Correct |
365 ms |
344 KB |
Output is correct |
35 |
Correct |
362 ms |
548 KB |
Output is correct |
36 |
Correct |
325 ms |
344 KB |
Output is correct |
37 |
Correct |
321 ms |
344 KB |
Output is correct |
38 |
Correct |
256 ms |
360 KB |
Output is correct |
39 |
Correct |
292 ms |
348 KB |
Output is correct |
40 |
Correct |
224 ms |
540 KB |
Output is correct |
41 |
Correct |
233 ms |
596 KB |
Output is correct |
42 |
Correct |
335 ms |
548 KB |
Output is correct |
43 |
Correct |
329 ms |
344 KB |
Output is correct |
44 |
Correct |
271 ms |
344 KB |
Output is correct |
45 |
Correct |
303 ms |
344 KB |
Output is correct |
46 |
Correct |
324 ms |
344 KB |
Output is correct |
47 |
Correct |
326 ms |
544 KB |
Output is correct |
48 |
Correct |
337 ms |
348 KB |
Output is correct |
49 |
Correct |
250 ms |
596 KB |
Output is correct |
50 |
Correct |
293 ms |
544 KB |
Output is correct |
51 |
Correct |
288 ms |
540 KB |
Output is correct |
52 |
Correct |
268 ms |
348 KB |
Output is correct |
53 |
Execution timed out |
2041 ms |
11852 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |