# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
414650 | hibye1217 | Arranging Shoes (IOI19_shoes) | C++17 | 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 <vector>
#include <cstdio>
#include <cassert>
using namespace std;
long long count_swaps(std::vector<int> s);
int main() {
int n;
assert(1 == scanf("%d", &n));
vector<int> S(2 * n);
for (int i = 0; i < 2 * n; i++)
assert(1 == scanf("%d", &S[i]));
fclose(stdin);
long long result = count_swaps(S);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
// #include "shoes.h"
int psh(std::vector<int>& s, int st, int ed){
int res = 0;
for (int j = st; j > ed; j--){
swap(s[j], s[j-1]);
res += 1;
}
return res;
}
long long count_swaps(std::vector<int> s) {
int n = s.size();
int ans = 0;
for (int i = 0; i < n; i += 2){
int p = 0;
for (int j = i; j < n; j++){
if (s[j] == -s[i]){ p = j; break; }
}
ans += psh(s, p, i+1);
if (s[i] > s[i+1]){ swap(s[i], s[i+1]); ans += 1; }
}
return ans;
}