# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
388603 | cpp219 | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "Ofast"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 6;
const ll lim = 3e5 + 6;
const ll inf = 1e9 + 7;
typedef pair<ll,ll> LL;
vector<ll> pos[N],neg[N];
ll bit[N];
bool was[N];
void upd(ll p,ll val){
for (ll i = p;i < N;i += i & -i) bit[i] += val;
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
ll Query(ll L,ll R){
return Get(R) - Get(L - 1);
}
ll Find(vector<ll> &v,ll x){
return *lower_bound(v.begin(),v.end(),x);
}
ll count_swaps(vector<ll> v){
ll n = v.size(),ans = 0;
for (ll i = 0;i < n;i++){
ll now = v[i];
if (now > 0) pos[now].push_back(i);
else neg[-now].push_back(i);
}
for (ll i = 0;i < n;i++){
if (was[i]) continue;
ll now = v[i],nxt;
if (now < 0){
nxt = Find(pos[-now],i); ans += nxt - i - Query(i,nxt) - 1;
}
else{
nxt = Find(neg[now],i); ans += nxt - i - Query(i,nxt);
}
was[nxt] = 1; upd(nxt,1);
}
return ans;
}
/*
vector<ll> v;
ll n,x;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
}
cin>>n;
for (ll i = 1;i <= n;i++) cin>>x,v.push_back(x);
cout<<count_swaps(v);
}
*/