#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 = 1e5 + 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);
}
};
int d[N][2];
vector<int> lef[N], rig[N];
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][1] = lef[i][j];
d[c][0] = rig[i][j];
}
}
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;
}
// signed main() {
// cout << count_swaps({-2, 2, 2, -2, -2, 2});
// }
| # | 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... |