# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
305987 | kylych03 | 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 "shoes.h"
#include <bits/stdc++.h>
#include "grader.cpp"
using namespace std;
long long res[200010];
int vis[200011], dp[200010];
vector <int> a[100002], b[100002];
long long count_swaps(std::vector<int> s) {
long long sum = 0;
for(int i = 0 ; i < s.size(); i++){
if(s[i]>0)
a[s[i]].push_back(i);
else
b[abs(s[i])].push_back(i);
}
for(int i = 0 ; i <100001;i++){
for(int j = 0 ; j < a[i].size();j++){
dp[a[i][j]]=b[i][j];
dp[b[i][j]]=a[i][j];
}
}
for(int i = 0 ; i < s.size(); i++){
if(vis[i]==1)
continue;
int j=i+1;
while( j!=dp[i] ){
res[j]++;
j++;
}
sum += (j - i - 1 + (res[j] - res[i]));
if(s[i] > 0)
sum++;
vis[dp[i]]=1;
}
return sum;
}