#include "bits/stdc++.h"
#include "shoes.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
// const int inf = 2e18;
const int N = 2e5 + 7;
struct BIT {
vector<ll> bit;
int n;
BIT() {}
BIT(int n) : n(n) {
bit.resize(n + 1, 0);
}
void update(int u, ll x) {
for(u; u <= n; u += (u & -u)) bit[u] += x;
}
int get(int u) {
ll ans = 0;
for(u; u > 0; u -= (u & -u)) ans += bit[u];
return ans;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
};
vector<int> d[N];
vector<int> lef[N], rig[N];
bool cmp(vector<int> a, vector<int> b) {
return min(a[0], a[1]) < min(b[0], b[1]);
}
long long count_swaps(std::vector<int> s) {
for(int i = 0; i < sz(s); i++) {
if(s[i] < 0) {
lef[-s[i]].pb(i + 1);
} else {
rig[s[i]].pb(i + 1);
}
}
int n = sz(s) / 2;
int c = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < sz(lef[i]); j++) {
d[++c].pb(rig[i][j]);
d[c].pb(lef[i][j]);
}
}
sort(d + 1, d + c + 1, cmp);
BIT bit = BIT(sz(s) + 1);
ll ans = 0;
for(int i = 1; i <= c; i++) {
for(int j = 1; j >= 0; j--) {
ll pos = i * 2 - j;
ll sus = d[i][j] + bit.get(d[i][j] + 1, sz(s));
bit.update(d[i][j], 1);
ans += abs(sus - pos);
}
}
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... |