| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1323991 | sh_qaxxorov_571 | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int64 count_swaps(vector<int> S) {
int n = S.size();
int64 res = 0;
// Chap va o'ng poyabzallarni bir xil o'lcham bo'yicha tartiblaymiz
for (int i = 0; i < n; i += 2) {
if (S[i] < 0) { // chap bo'lishi kerak edi, o'ng poyabzal
// topib, chap poyabzal bilan almashtiramiz
int j = i + 1;
while (j < n && !(S[j] > 0 && abs(S[j]) == abs(S[i + 1]))) j++;
while (j > i) {
swap(S[j], S[j - 1]);
res++;
j--;
}
}
if (S[i + 1] > 0) { // o'ng bo'lishi kerak edi, chap poyabzal
int j = i + 2;
while (j < n && !(S[j] < 0 && abs(S[j]) == abs(S[i]))) j++;
while (j > i + 1) {
swap(S[j], S[j - 1]);
res++;
j--;
}
}
}
return res;
}
// Test qilish
int main() {
vector<int> S1 = {2, 1, -1, -2};
cout << count_swaps(S1) << endl; // natija: 4
vector<int> S2 = {-2, 2, 2, -2, -2, 2};
cout << count_swaps(S2) << endl; // natija: 2
return 0;
}
