# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1268835 | FaresSTH | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include"bits/stdc++.h"
using namespace std;
using ll=long long;
ll count_swaps(const vector<int>& a){
int n = (int)a.size();
vector<int> neg;
neg.reserve(n);
for (int i=0;i<n;i++) if (a[i] < 0) neg.push_back(i);
auto cost = [&](int start)->long long { // start = 0 (even) or 1 (odd)
vector<int> tgt;
for (int i=start;i<n;i+=2) tgt.push_back(i);
if (tgt.size() != neg.size()) return (long long)4e18; // impossible for this pattern
long long res = 0;
for (size_t k=0;k<neg.size();++k) res += llabs((long long)neg[k] - tgt[k]);
return res;
};
long long ans = min(cost(0), cost(1));
// If both patterns are impossible, return -1 (if your task wants that),
// otherwise return 'ans'.
if (ans >= (long long)3e18) return -1;
return ans;
}