This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Ignut
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1e5 + 123;
int t[MAXN];
void Add(int v, int l, int r, int pos) {
t[v] ++;
if (l == r)
return;
int mid = l + (r - l) / 2;
if (pos <= mid)
Add(v * 2, l, mid, pos);
else
Add(v * 2 + 1, mid + 1, r, pos);
}
int Sum(int v, int l, int r, int ql, int qr) {
if (l > qr || ql > r)
return 0;
if (l >= ql && r <= qr)
return t[v];
int mid = l + (r - l) / 2;
return Sum(v * 2, l, mid, ql, qr) + Sum(v * 2 + 1, mid + 1, r, ql, qr);
}
ll count_swaps(vector<int> S) {
ll res = 0;
int n = S.size();
vector<int> pos[n + 1] = {};
for (int i = 0; i < n; i ++)
pos[S[i] + n / 2].push_back(i);
for (int i = 0; i <= n; i ++)
reverse(pos[i].begin(), pos[i].end());
vector<pair<int, int>> vec;
for (int i = 0; i < n; i ++) {
if (pos[S[i] + n / 2].back() != i)
continue;
vec.push_back({i, pos[-S[i] + n / 2].back()});
pos[S[i] + n / 2].pop_back();
pos[-S[i] + n / 2].pop_back();
if (S[i] > 0) res ++;
}
for (auto [i, j] : vec) {
int diff = j - i - 1;
diff -= Sum(1, 0, n - 1, i + 1, j);
res += diff;
Add(1, 0, n - 1, j);
}
return res;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |