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>
#include "shoes.h"
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const double PI = 2 * acos(0);
queue<int> kanan[121212];
int BIT[121212];
void update(int p) {
for (int i = p; i <= 1e5; i += (i & (-i))) BIT[i]++;
}
int query(int p) {
int cnt = 0;
for (int i = p; i > 0; i -= (i & (-i))) {
cnt += BIT[i];
}
return cnt;
}
long long count_swaps(vector<int> s) {
int n = s.size();
priority_queue<pair<int, int> > pq;
for (int i = 0; i < n; i++) {
int x;
x = s[i];
if (x < 0) {
pq.push(mp(-i, -x));
} else {
kanan[x].push(i);
}
}
LL ans = 0;
int idx = 1;
while (!pq.empty()) {
int pos = -pq.top().fi;
int val = pq.top().se;
pq.pop();
pos += (idx - 1) - query(pos);
ans += (pos - idx);
update(pos);
pos = kanan[val].front();
kanan[val].pop();
pos += (idx - query(pos));
ans += (pos - idx - 1);
update(pos);
idx += 2;
}
return ans;
}
# | 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... |