| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1323992 | 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;
for (int i = 0; i < n; i += 2) {
int left = abs(S[i]); // Chap poyabzal o'lchami
int right = abs(S[i + 1]); // O'ng poyabzal o'lchami
// 1. Chap poyabzalni topish
if (S[i] < 0) {
int j = i + 1;
while (j < n && !(S[j] > 0 && abs(S[j]) == left)) j++;
while (j > i) {
swap(S[j], S[j - 1]);
res++;
j--;
}
}
// 2. O'ng poyabzalni topish
if (S[i + 1] > 0) {
int j = i + 2;
while (j < n && !(S[j] < 0 && abs(S[j]) == right)) j++;
while (j > i + 1) {
swap(S[j], S[j - 1]);
res++;
j--;
}
}
}
return res;
}
// Test
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;
}
