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 "bits/stdc++.h"
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n), pos(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
pos[a[i]] = i;
}
for (int i = 0; i < n; i++) {
if (n - pos[i] - 1 != pos[n - i - 1]) {
cout << -1 << '\n';
return;
}
}
vector<pair<int, int>> ans;
auto Swap = [&] (int i, int j) {
swap(a[i], a[j]);
pos[a[i]] = i;
pos[a[j]] = j;
};
auto make = [&] (int i, int j) {
ans.push_back({i, j});
Swap(i, j);
Swap(n - i - 1, n - j - 1);
};
for (int l = 0; l < n - 1 - l; l++) {
int r = n - l - 1;
if (a[l] == l && a[r] == r) {
continue;
}
if (a[l] == l || a[r] == r) {
assert(false);
if (r - l + 1 <= 5) {
cout << -1 << '\n';
return;
}
if (a[l] == l) {
int ps = l + 1;
while (a[ps] == r || a[n - ps - 1] == r) ps++;
make(l, ps);
} else {
int ps = l + 1;
while (a[ps] == l || a[n - ps - 1] == l) ps++;
make(r, ps);
}
}
assert(a[l] != l && a[r] != r);
if (pos[l] != n - 1 - pos[r]) {
assert(false);
}
if (a[l] == r) {
assert(a[r] == l);
if (l + 1 == r) {
cout << -1 << '\n';
return;
}
make(l, l + 1);
}
make(l, pos[l]);
assert(a[l] == l && a[r] == r);
}
cout << ans.size() << " " << ans.size() << '\n';
for (auto [i, j] : ans) {
cout << i + 1 << " " << j + 1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |