# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1047352 | vjudge1 | Arranging Shoes (IOI19_shoes) | C++17 | 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;
const int mxN = 1e5 + 7;
int tree[mxN];
int n;
void upd(int l,int r)
{
l++;
for( ;n > l;l += l & -l) tree[l]++;
for( ;n > r;r += r & -r) tree[r]--;
}
int que(int pos)
{
pos--;
int res = 0;
for( ;pos > 0;pos -= pos & -pos) res += tree[pos];
return res;
}
int count_swaps(vector<int> a)
{
n = a.size();
int mark[n];
memset(mark,0,sizeof(mark));
vector<int> sz[2 * n];
int ans = 0;
for(int i = 0;n > i;i++)
{
sz[n + a[i]].push_back(i);
}
for(int i = 0;n > i;i++)
{
if(mark[i]) continue;
int kayd = -a[i] + n;
auto l = upper_bound(sz[kayd].begin(), sz[kayd].end(), i);
if(l == sz[kayd].end()) continue;
ans += que(i) + (*l - i);
if(a[*l] > 0) ans--;
upd(i,*l);
mark[*l]++;
}
return ans;
}
int main()
{
vector<int> a = {-2, 2, 2, -2, -2, 2};
cout << count_swaps(a) << endl;
}