#include "shoes.h"
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
long long count_swaps(std::vector<int> s) {
long long swaps = 0;
int n = s.size();
for (int i = 0; i < n; ++i) {
// only try to pair a left shoe (positive) that isn't already
// immediately preceded by its right mate:
if (s[i] > 0 && !(i > 0 && s[i-1] == -s[i])) {
// find its matching right shoe
int j = i+1;
while (j < n && s[j] != -s[i]) ++j;
// move that right shoe from j to i, one adjacent swap at a time
while (j > i) {
std::swap(s[j], s[j-1]);
--j;
++swaps;
}
// now s[i] is the right shoe, s[i+1] is the left shoe
}
}
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... |