This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* input
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int nxt[N], bit[N], n; bool solved[N];
map<int, queue<int>> q;
void up(int x, int val) {
while (x <= n) {
bit[x] += val;
x += x & (-x);
}
}
int get(int x) {
int ret = 0;
while (x) {
ret += bit[x];
x -= x & (-x);
}
return ret;
}
ll count_swaps(vector<int> v) {
n = v.size();
v.insert(v.begin(), 0);
for (int i = 1; i <= n; ++i) {
if (q[-v[i]].size()) {
int x = q[-v[i]].front(); q[-v[i]].pop();
nxt[x] = i;
}
else q[v[i]].push(i);
}
ll ret = 0;
for (int i = 1; i <= n; ++i) if (!solved[i]) {
int L = i + get(i), R = nxt[i] + get(nxt[i]);
ret += R - L - 1 + (v[i] > 0);
up(nxt[i], 1); solved[nxt[i]] = 1;
}
return ret;
}
# | 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... |