#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 mem[N];
ll calc(int k) {
if(mem[k] != -1) {
return mem[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);
}
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 mem[k] = ret;
}
void test() {
memset(mem, -1, sizeof(mem));
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);
}
}
if(n == 1) {
cout << 0 << '\n';
return;
}
if(calc(0) < calc(1)) {
int l = 1, r = n;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(calc(mid) > calc(mid - 1)) {
l = mid;
} else {
r = mid;
}
}
cout << calc(l) << '\n';
} else {
int l = 0, r = n - 1;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(calc(mid) > calc(mid + 1)) {
r = mid;
} else {
l = mid;
}
}
cout << calc(r) << '\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 |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2816 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2816 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2816 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2816 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |