This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
queue <int> d[1 + 2 * N + 5];
typedef long long ll;
int n;
int aib[1 + N * 2];
int used[1 + N * 2];
int lsb (int x) {
return x & -x;
}
void upd (int x, int y) {
while (x <= n) {
aib[x] += y;
x += lsb (x);
}
}
int qry (int x) {
int ans = 0;
while (x) {
ans += aib[x];
x -= lsb (x);
}
return ans;
}
int count_swaps (vector <int> v) {
n = v.size ();
for (int i = 0; i < n; i++) {
d[v[i] + N].push (i);
}
for (int i = 0; i < n; i++) {
upd (i + 1, 1);
}
ll ans = 0;
for (int i = 0; i < n; i++) {
if (used[i]) continue;
d[v[i] + N].pop ();
int j = d[N - v[i]].front();
d[N - v[i]].pop();
used[i] = 1;
used[j] = 1;
ans += qry (j) - qry (i) - 1;
upd (i + 1, 1);
upd (j + 1, -1);
if (v[i] > v[j])
ans++;
}
return ans;
}
/*int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n;
vector <int> v (n);
for (auto &x : v)
cin >> x;
cout << count_swaps (v) << "\n";
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |