#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < n; ++i)
cin >> p[i], --p[i];
for (int i = 0; i < n; ++i) if (p[i] + p[n - i - 1] != n - 1) {
cout << "-1\n";
return;
}
int e1 = 0;
for (int i = 0; i < n / 2; ++i)
e1 += p[i] >= n / 2;
if (e1 % 2) {
cout << "-1\n";
return;
}
vector<int> rp(n);
for (int i = 0; i < n; ++i)
rp[p[i]] = i;
vector<int> pcp(n / 2);
vector<bool> pcp_leap(n / 2);
for (int i = 0; i < n / 2; ++i) {
if (p[i] < n / 2) {
pcp[i] = p[i];
pcp_leap[i] = 0;
} else {
pcp[i] = n - 1 - p[i];
pcp_leap[i] = 1;
}
}
vector<int> singles;
vector<pair<int, int>> ans;
for (int i = 0; i < n / 2; ++i) {
if (p[i] == i)
continue;
if (pcp[i] == i) {
singles.push_back(i);
continue;
}
vector<int> a;
int j = i;
do {
a.push_back(j);
j = pcp[j];
} while (j != i);
bool xr = false;
for (auto t : a)
xr ^= pcp_leap[t];
//for (auto x : a)
//cout << x << ' ';
//cout << " | " << xr << endl;
if (xr) {
a.clear();
j = i;
do {
a.push_back(j);
j = p[j];
} while (j != i);
for (size_t r = 0; r + 1 < a.size(); ++r) if (a[r + 1] + a[0] == n - 1) {
ans.push_back({a[0], a[r]});
swap(p[a[0]], p[a[r]]);
singles.push_back(a[0]);
for (size_t t = 2; t <= r; ++t) {
ans.push_back({a[1], a[t]});
swap(p[a[1]], p[a[t]]);
swap(p[n - 1 - a[1]], p[n - 1 - a[t]]);
}
}
} else {
for (size_t t = 0; t + 1 < a.size(); ++t) {
int i0 = a[t];
if (xr)
i0 = n - 1 - a[t];
int j0 = a[t + 1];
xr ^= pcp_leap[a[t + 1]];
if (xr)
j0 = n - 1 - a[t + 1];
ans.push_back({i0, j0});
swap(p[i0], p[j0]);
swap(p[n - 1 - i0], p[n - 1 - j0]);
}
}
}
while (singles.size()) {
int i = singles.back();
singles.pop_back();
int j = singles.back();
singles.pop_back();
ans.push_back({i, n - 1 - j});
ans.push_back({i, j});
//cout << "{ " << i << ' ' << j << " }" << endl;
}
//if (e1 == 0) {
//vector<int> p0(n / 2);
//for (int i = 0; i < n / 2; ++i)
//p0[i] = p[i];
cout << ans.size() << ' ' << ans.size() << '\n';
for (auto x : ans)
cout << x.first + 1 << ' ' << x.second + 1 << '\n';
//return;
//}
//cout << "1 1\n";
//cout << "1 2\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
//cout << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
468 KB |
Correctly distinguished between possibility and impossibility |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
90 ms |
3184 KB |
Valid reconstruction |
2 |
Partially correct |
77 ms |
3040 KB |
Valid reconstruction |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
468 KB |
Correctly distinguished between possibility and impossibility |
2 |
Partially correct |
90 ms |
3184 KB |
Valid reconstruction |
3 |
Partially correct |
77 ms |
3040 KB |
Valid reconstruction |
4 |
Correct |
60 ms |
2380 KB |
Correctly distinguished between possibility and impossibility |
5 |
Correct |
51 ms |
2772 KB |
Correctly distinguished between possibility and impossibility |
6 |
Correct |
44 ms |
2848 KB |
Correctly distinguished between possibility and impossibility |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
90 ms |
3184 KB |
Valid reconstruction |
2 |
Partially correct |
77 ms |
3040 KB |
Valid reconstruction |
3 |
Runtime error |
539 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
468 KB |
Correctly distinguished between possibility and impossibility |
2 |
Partially correct |
90 ms |
3184 KB |
Valid reconstruction |
3 |
Partially correct |
77 ms |
3040 KB |
Valid reconstruction |
4 |
Correct |
60 ms |
2380 KB |
Correctly distinguished between possibility and impossibility |
5 |
Correct |
51 ms |
2772 KB |
Correctly distinguished between possibility and impossibility |
6 |
Correct |
44 ms |
2848 KB |
Correctly distinguished between possibility and impossibility |
7 |
Runtime error |
539 ms |
262144 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |