#include <bits/stdc++.h>
#include "shoes.h"
// #include "grader.cpp"
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define nl '\n'
struct Fenw {
int n;
vector<int> t;
Fenw(int n) : n(n), t(n + 1) {}
void update(int i, int x) {
i++;
for(; i <= n; i += i & -i) t[i] += x;
}
int get(int i) {
int res = 0;
for(; i >= 1; i -= i & -i) res += t[i];
return res;
}
int get(int l, int r) {
return get(r + 1) - get(l);
}
};
long long count_swaps(vector<int> s) {
int n = s.size();
long long ans = 0;
Fenw fn(n);
bool flag = 0;
map<int, set<int>> st;
for(int i = 0; i < n; i++) {
st[s[i]].emplace(i);
}
for(int i = 0; i < n; i++) {
if(fn.get(i, i)) continue;
int x = s[i];
if(flag != (x > 0)) {
int y = -x;
int j = *st[y].begin();
st[y].erase(st[y].begin());
ans += j - i - fn.get(i, j);
fn.update(j, 1);
} else flag = !flag;
st[x].erase(i);
}
return ans;
}
/**
4
2 -2 2 2 -2 -2 -2 2
4
2 -2 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... |