#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 2e5 + 7;
int n;
int aib[2 * N];
vector<int> v[2];
string s;
void update(int x) {
for (int i = x; i < 2 * N; i += i & -i) {
aib[i]++;
}
}
int query(int x) {
int ret = 0;
for (int i = x; i >= 1; i -= i & -i) {
ret += aib[i];
}
return ret;
}
int get(int l, int r) {
return query(r) - query(l - 1);
}
ll solve(int offset) {
for (int i = 1; i <= 2 * n; i++) {
aib[i] = 0;
}
vector<pair<int, int>> seg;
for (int i = 0; i < n; i++) {
int j = (i + offset) % n;
seg.push_back({v[0][i], v[1][j]});
if (seg.back().first > seg.back().second) {
swap(seg.back().first, seg.back().second);
}
}
sort(seg.begin(), seg.end());
ll ret = 0;
for (auto &it : seg) {
ret += get(it.first, it.second);
update(it.second);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen("input", "r", stdin);
cin >> n >> s;
for (int i = 0; i < 2 * n; i++) {
v[(s[i] == 'B')].push_back(i);
}
int low = 0, high = n - 1, sol = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (solve(mid) < solve(mid + 1)) {
sol = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
cout << solve(sol + 1) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |