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 ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define pb push_back
using namespace std;
int fw[200020];
vector<pii> pos[200020];
vector<pii> swp;
void update(int i, int val) {
for (; i <= 200000; i += i&-i) {
fw[i] += val;
}
}
int query(int i) {
int cnt = 0;
for (; i > 0; i -= i&-i) {
cnt += fw[i];
}
return cnt;
}
ll count_swaps(vector<int> s) {
int n = s.size()/2;
s.insert(s.begin(), 0);
ll ans = 0;
for (int i = 1; i <= 2*n; ++i) {
update(i, 1);
pos[abs(s[i])].pb({s[i], i});
}
for (int i = 1; i <= n; ++i) {
sort(pos[i].begin(), pos[i].end());
int m = pos[i].size();
for (int j = 0; j < m/2; ++j) {
int l = pos[i][j].nd;
int r = pos[i][j + m/2].nd;
if (l > r) {
++ans;
swap(l, r);
}
swp.pb({l, r});
}
}
sort(swp.begin(), swp.end());
for (auto [l, r] : swp) {
ans += query(r - 1) - query(l);
update(r, -1);
update(l, -1);
}
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... |