#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
#ifdef DEBUG
template<class T> int size(T &&x) {
return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for(auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#endif
#ifdef DEBUG
struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
typedef vector<pii> vpi;
vi skal(vi vec) {
int n = sz(vec);
vpi skal(n);
vi ret(n);
rep(i, 0, n) skal[i] = {vec[i], i};
sort(all(skal));
rep(i, 0, n) ret[skal[i].e2] = i;
return ret;
}
int main() {
boost;
int n;
cin >> n;
vi vec(n);
rep(i, 0, n) cin >> vec[i];
vec = skal(vec);
debug(vec);
vector <vpi> odp;
int tryhard = 0;
while (true) {
debug(vec);
bool ok = true;
rep(i, 0, n) if (vec[i] != i) ok = false;
if (ok) {
assert(sz(odp) <= 2);
cout << sz(odp) << "\n";
trav(ciag, odp) {
cout << 2 * sz(ciag) << "\n";
for (auto [a, b] : ciag) cout << a + 1 << ' ';
for (int i = sz(ciag) - 1; i >= 0; --i) cout << ciag[i].second + 1 << ' ';
cout << "\n";
}
exit(0);
}
vector <bool> odw(n, false);
vpi swaps;
rep(i, 0, n) {
if (vec[i] == i || odw[i]) continue;
vi vis;
int j = i;
while (!odw[j]) {
odw[j] = true;
vis.pb(j);
j = vec[j];
}
if (tryhard == 0 && sz(vis) != 2) { //magically swap the cycle differently
int viss = sz(vis);
for (int iter = 1; iter < viss - iter; ++iter) {
int a = vis[iter], b = vis[viss - iter];
swaps.push_back({a, b});
}
}
else {
assert(sz(vis) == 2);
for (int iter = 1; iter < sz(vis); iter += 2) {
int a = vis[iter - 1], b = vis[iter];
swaps.push_back({a, b});
}
}
}
tryhard += 1;
trav(zam, swaps) swap(vec[zam.e1], vec[zam.e2]);
odp.push_back(swaps);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif typedef vector<pii> vpi; vi skal(vi vec) { int n = sz(vec); vpi skal(n); vi ret(n); rep(i, 0, n) skal[i] = {vec[i], i}; sort(all(skal)); rep(i, 0, n) ret[skal[i].e2] = i; return ret; } int main() { boost; int n; cin >> n; vi vec(n); rep(i, 0, n) cin >> vec[i]; vec = skal(vec); debug(vec); vector <vpi> odp; int tryhard = 0; while (true) { debug(vec); bool ok = true; rep(i, 0, n) if (vec[i] != i) ok = false; if (ok) { assert(sz(odp) <= 2); cout << sz(odp) << "\n"; trav(ciag, odp) { cout << 2 * sz(ciag) << "\n"; for (auto [a, b] : ciag) cout << a + 1 << ' '; for (int i = sz(ciag) - 1; i >= 0; --i) cout << ciag[i].second + 1 << ' '; cout << "\n"; } exit(0); } vector <bool> odw(n, false); vpi swaps; rep(i, 0, n) { if (vec[i] == i || odw[i]) continue; vi vis; int j = i; while (!odw[j]) { odw[j] = true; vis.pb(j); j = vec[j]; } if (tryhard == 0 && sz(vis) != 2) { //magically swap the cycle differently int viss = sz(vis); for (int iter = 1; iter < viss - iter; ++iter) { int a = vis[iter], b = vis[viss - iter]; swaps.push_back({a, b}); } } else { assert(sz(vis) == 2); for (int iter = 1; iter < sz(vis); iter += 2) { int a = vis[iter - 1], b = vis[iter]; swaps.push_back({a, b}); } } } tryhard += 1; trav(zam, swaps) swap(vec[zam.e1], vec[zam.e2]); odp.push_back(swaps); } } |
English