# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1187079 | eri16 | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include "shoes.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int64_t count_swaps(vector<int> S) {
int64_t swaps = 0;
int n = S.size();
for (int i = 0; i < n; ) {
if (S[i] < 0) {
// Skip right shoes — can't start a pair
++i;
continue;
}
int size = S[i];
// Find matching right shoe
int j = i + 1;
while (j < n && S[j] != -size) {
++j;
}
// Move the right shoe leftward to i + 1
while (j > i + 1) {
swap(S[j], S[j - 1]);
--j;
++swaps;
}
// Now valid pair at i, i+1
i += 2;
}
return swaps;
}