This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// sorry didnt tryin to prove:(
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n;
int ft[2 * N];
string s;
vector<int> W, B;
void upd(int i, int x) {
for (; i <= 2 * n; i += i & -i) {
ft[i] += x;
}
}
int qry(int i) {
int res = 0;
for (; i; i -= i & -i) {
res += ft[i];
}
return res;
}
int qry(int l, int r) {
return qry(r) - qry(l - 1);
}
long long get(int k) {
long long res = 0;
vector<array<int, 2>> cands;
for (int i = 0; i < n; ++i) {
int a = W[i], b = B[(i + k) % n];
cands.push_back({min(a, b), max(a, b)});
}
sort(cands.begin(), cands.end());
fill(ft + 1, ft + 2 * n + 1, 0);
for (auto [l, r] : cands) {
res += qry(l, r);
upd(r, 1);
}
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= 2 * n; ++i) {
char c; cin >> c;
(c == 'W' ? W : B).push_back(i);
}
int l = 1, r = n, p = 0;
while (l <= r) {
int md = (l + r) / 2;
if (get(md - 1) <= get(md)) {
p = md;
l = md + 1;
} else {
r = md - 1;
}
}
cout << get(p);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |