# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
295352 | tzero | Arranging Shoes (IOI19_shoes) | C++14 | 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 <iostream>
#include <vector>
#include <math.h>
using namespace std;
using ll = long long;
ll count_swaps(vector<int> s) {
int n = (int)s.size();
bool same_size = true;
for(int i = 0; i < n; i++) {
if(i > 0 && abs(s[i]) != abs(s[i - 1])) same_size = false;
}
if(n == 2) {
if(s[0] < 0) {
printf("0\n");
} else {
printf("1\n");
}
} else if(same_size) {
ll move_left = 0, move_right = 0;
int found_already = 0;
for(int i = 0; i < n; i++) if(s[i] > 0) {
int pos = found_already++ * 2;
move_left += abs(i - pos);
}
found_already = 0;
for(int i = 0; i < n; i++) if(s[i] > 0) {
int pos = found_already++ * 2 + 1;
move_right += abs(i - pos);
}
return min(move_left, move_right);
} else {
return -1;
}
}
int main() {
int n;
scanf("%d", &n);
bool same_size = true;
vector<int> s(n);
for(int i = 0; i < 2 * n; i++) {
scanf("%d", &s[i]);
if(i > 0 && abs(s[i]) != abs(s[i - 1])) same_size = false;
}
if(n == 1) {
if(s[0] < 0) {
printf("0\n");
} else {
printf("1\n");
}
} else if(same_size) {
ll move_left = 0, move_right = 0;
int found_already = 0;
for(int i = 0; i < 2 * n; i++) if(s[i] > 0) {
int pos = found_already++ * 2;
move_left += abs(i - pos);
}
found_already = 0;
for(int i = 0; i < 2 * n; i++) if(s[i] > 0) {
int pos = found_already++ * 2 + 1;
move_right += abs(i - pos);
}
printf("%lld\n", min(move_left, move_right));
} else {
printf("%d\n", -1);
}
return 0;
}