# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
819808 | Bula | 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.
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ff first
#define sc second
//#define int ll
const ll mod=1e9+7, N = 2e5 + 1;
vector<int> t(4 * N);
int get(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
return get(v*2, tl, tm, l, min(r, tm))+get(v*2+1, tm+1, tr, max(l, tm+1), r);
}
void up(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr) {
t[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
up(v*2, tl, tm, pos, new_val);
else
up(v*2+1, tm+1, tr, pos, new_val);
t[v] = t[v*2]+t[v*2+1];
}
}
ll count_swaps(vector<int> &s){
int n = s.size();
vector< set<int> > v(n + 1);
for(int i = 0; i < n; i++){
v[s[i]].insert(i);
}
int ans = 0;
for(int i = 0; i < n; i++){
if(get(1, 0, n - 1, i, i) != 0) continue;
int id = *v[-s[i]].begin();
v[s[i]].erase(v[s[i]].begin());
v[-s[i]].erase(v[-s[i]].begin());
if(s[i] > 0) ans++;
ans += id - i - 1 - get(1, 0, n - 1, i + 1, id - 1);
up(1, 0, n - 1, id, 1);
up(1, 0, n - 1, i, 1);
}
return ans;
}