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());
map<int, int> need;
vector<pair<int, int>> lst;
for (int i = 0; i < n; i ++) {
if (need[S[i]] >= 1) {
need[S[i]] --;
continue;
}
lst.push_back({S[i], i});
need[-S[i]] ++;
}
for (auto [val, i] : lst) {
cout << val << ' ';
}
cout << '\n';
for (auto [val, i] : lst) {
int target = -val;
int j = pos[target + n / 2].back();
pos[target + n / 2].pop_back();
int jj = j;
cout << j << '\n';
j += Sum(1, 0, n - 1, 0, j);
res += max(0, (j - i));
Add(1, 0, n - 1, jj);
if (val > 0) {
res ++;
}
}
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... |