#include "shoes.h"
#include <vector>
#include <cmath>
long long count_swaps(std::vector<int> s) {
long long swaps = 0;
int n = s.size();
for (int i = 0; i < n; i += 2) {
// s[i] must be a left shoe
if (s[i] < 0) {
// this should never happen if the input is guaranteed to be solvable
continue;
}
int left_size = s[i];
// Find the closest matching right shoe
int j = i + 1;
while (j < n && s[j] != -left_size) {
++j;
}
// Bring the right shoe to position i + 1 by adjacent swaps
while (j > i + 1) {
std::swap(s[j], s[j - 1]);
--j;
++swaps;
}
}
return swaps;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |