| Task: | Vaihdot |
| Sender: | hltk |
| Submission time: | 2020-10-16 20:20:42 +0300 |
| Language: | C++ (C++17) |
| Status: | READY |
| Result: | 0 |
| group | verdict | score |
|---|---|---|
| #1 | WRONG ANSWER | 0 |
| #2 | WRONG ANSWER | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | WRONG ANSWER | 0.01 s | 1, 2 | details |
| #2 | WRONG ANSWER | 0.03 s | 2 | details |
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n), pos(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
v[i]--;
pos[v[i]] = i;
}
bool ok = true;
vector<pair<int, int>> swaps;
auto do_swap = [&](int a, int b) {
swap(v[a], v[b]);
swap(pos[v[a]], pos[v[b]]);
swaps.emplace_back(a, b);
};
for (int i = 0; i < n; ++i) {
if (pos[i] == i)
continue;
if (abs(pos[i] - i) > 1) {
do_swap(pos[i], i);
} else {
bool f = false;
for (int j = 0; j < n; ++j) {
if (abs(i - j) > 1 && abs(pos[i] - j) > 1) {
f = true;
do_swap(pos[i], j);
do_swap(i, j);
do_swap(pos[i], j);
break;
}
}
if (!f)
ok = false;
}
}
if (!ok)
cout << -1 << '\n';
else {
cout << swaps.size() << '\n';
for (auto& [l, r] : swaps)
cout << l+1 << ' ' << r+1 << '\n';
}
}
}
Test details
Test 1
Group: 1, 2
Verdict: WRONG ANSWER
| input |
|---|
| 1000 1 1 2 1 2 ... |
| correct output |
|---|
| 0 0 -1 0 -1 ... |
| user output |
|---|
| 0 0 -1 0 -1 ... Truncated |
Test 2
Group: 2
Verdict: WRONG ANSWER
| input |
|---|
| 1000 79 49 42 77 41 37 61 46 55 7 72 4... |
| correct output |
|---|
| 81 67 79 70 78 3 77 60 76 ... |
| user output |
|---|
| 83 58 1 45 2 16 3 29 4 ... Truncated |
