# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1257451 | huydeptraiso1vutruvn | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <vector>
using namespace std;
int minSwapsToPairShoes(int n, vector<int>& shoes) {
int totalSwaps = 0;
int len = 2 * n;
for (int i = 0; i < len; i += 2) {
// Nếu vị trí i là giày phải, ta cần tìm giày trái phù hợp
if (shoes[i] > 0) {
for (int j = i + 1; j < len; ++j) {
if (shoes[j] == -shoes[i]) {
// Đưa shoes[j] về vị trí i
for (int k = j; k > i; --k) {
swap(shoes[k], shoes[k - 1]);
totalSwaps++;
}
break;
}
}
}
// Giờ vị trí i là giày trái, kiểm tra i+1 là giày phải tương ứng
if (shoes[i + 1] != -shoes[i]) {
for (int j = i + 2; j < len; ++j) {
if (shoes[j] == -shoes[i]) {
// Đưa shoes[j] về vị trí i+1
for (int k = j; k > i + 1; --k) {
swap(shoes[k], shoes[k - 1]);
totalSwaps++;
}
break;
}
}
}
}
return totalSwaps;
}
int main() {
int n;
cin >> n;
vector<int> shoes(2 * n);
for (int i = 0; i < 2 * n; ++i) {
cin >> shoes[i];
}
int result = minSwapsToPairShoes(n, shoes);
cout << result << endl;
return 0;
}