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>
#ifdef local
#define debug(args...) qqbx(#args, args)
template <typename ...T> void qqbx(const char *s, T ...args) {
int cnt = sizeof...(T);
(std::cerr << "(" << s << ") = (" , ... , (std::cerr << args << (--cnt ? ", " : ")\n")));
}
#else
#define debug(...) ((void)0)
#endif // local
#define pb emplace_back
#define all(v) begin(v), end(v)
using namespace std;
using ll = long long;
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-1-i] != n-1) return cout << -1 << '\n', void();
int H = n/2;
vector<int> pos(H);
for(int i = 0; i < n; i++) if(p[i] < H) pos[p[i]] = i;
vector<pair<int,int>> ans;
for(int i = 0; i < H; i++) {
if(p[i] != i) {
int j = pos[i];
if(i+j == n-1)
continue;
/* if(p[i] >= H && j >= H) */
/* j = n-1-j; */
swap(pos[p[i] < H ? p[i] : n-1-p[i]], pos[p[j] < H ? p[j] : n-1-p[j]]);
swap(p[i], p[j]);
swap(p[n-1-i], p[n-1-j]);
ans.pb(i, j);
}
for(int j = 0; j < n; j++) cerr << p[j]+1 << (j+1==n ? '\n' : ' ');
}
vector<int> tmp;
for(int i = 0; i < H; i++) if(p[i] != i) tmp.pb(i);
if(tmp.size() % 2 == 1) return cout << -1 << '\n', void();
debug(tmp.size());
for(int i = 0; i < int(tmp.size()); i += 2) {
{
int a = n-1-tmp[i];
int b = tmp[i+1];
swap(p[a], p[b]);
swap(p[n-1-a], p[n-1-b]);
ans.pb(a, b);
}
{
int a = tmp[i];
int b = tmp[i+1];
swap(p[a], p[b]);
swap(p[n-1-a], p[n-1-b]);
ans.pb(a, b);
}
}
for(int j = 0; j < n; j++) cerr << p[j]+1 << (j+1==n ? '\n' : ' ');
if(!is_sorted(all(p))) return cout << -1 << '\n', void();
debug(ans.size());
cout << ans.size() << ' ' << ans.size() << '\n';
for(auto [a, b]: ans) cout << a+1 << ' ' << b+1 << '\n';
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while(t--) solve();
}
Compilation message (stderr)
cat.cpp: In function 'void solve()':
cat.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for(auto [a, b]: ans) cout << a+1 << ' ' << b+1 << '\n';
| ^
# | 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... |