#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 12, INF = 1 << 30; int n, T[maxN]; void intoPerm() { map <int, int> M; FOR(i, 1, n+1) M[T[i]] = 1; int ctr = 1; for (auto& [k, v] : M) v = ctr++; FOR(i, 1, n+1) T[i] = M[T[i]]; } auto getCycles() { vector <list <int> > C(0); vector <bool> vis(n+1, false); FOR(i, 1, n+1) if (!vis[i]) { list <int> c(0); for (int v = i; !vis[v]; v = T[v]) c.pb(v), vis[v] = true; C.pb(c); } return C; } void procCycle(list <int>& c, vector <vector <pair <int, int> > >& res) { FOR(d, 0, 2) { if (SZ(c) < 2) break; auto it = c.begin(), jt = prev(c.end()); FOR(_, 0, SZ(c) / 2) res[d].pb({*it++, *jt--}); c.erase(c.begin()); } } void print(vector <pair <int, int> >& P) { printf("%d\n", SZ(P) * 2); for (auto [a, _] : P) printf("%d ", a); reverse(ALL(P)); for (auto [_, b] : P) printf("%d ", b); printf("\n"); } void solve() { scanf ("%d", &n); FOR(i, 1, n+1) scanf ("%d", T+i); intoPerm(); auto C = getCycles(); vector <vector <pair <int, int> > > res(2, vector <pair <int, int> > {}); for (auto& c : C) procCycle(c, res); while (!res.empty() and res.back().empty()) res.pop_back(); printf("%d\n", SZ(res)); for (auto& P : res) print(P); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; }
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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 12, INF = 1 << 30; int n, T[maxN]; void intoPerm() { map <int, int> M; FOR(i, 1, n+1) M[T[i]] = 1; int ctr = 1; for (auto& [k, v] : M) v = ctr++; FOR(i, 1, n+1) T[i] = M[T[i]]; } auto getCycles() { vector <list <int> > C(0); vector <bool> vis(n+1, false); FOR(i, 1, n+1) if (!vis[i]) { list <int> c(0); for (int v = i; !vis[v]; v = T[v]) c.pb(v), vis[v] = true; C.pb(c); } return C; } void procCycle(list <int>& c, vector <vector <pair <int, int> > >& res) { FOR(d, 0, 2) { if (SZ(c) < 2) break; auto it = c.begin(), jt = prev(c.end()); FOR(_, 0, SZ(c) / 2) res[d].pb({*it++, *jt--}); c.erase(c.begin()); } } void print(vector <pair <int, int> >& P) { printf("%d\n", SZ(P) * 2); for (auto [a, _] : P) printf("%d ", a); reverse(ALL(P)); for (auto [_, b] : P) printf("%d ", b); printf("\n"); } void solve() { scanf ("%d", &n); FOR(i, 1, n+1) scanf ("%d", T+i); intoPerm(); auto C = getCycles(); vector <vector <pair <int, int> > > res(2, vector <pair <int, int> > {}); for (auto& c : C) procCycle(c, res); while (!res.empty() and res.back().empty()) res.pop_back(); printf("%d\n", SZ(res)); for (auto& P : res) print(P); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |