#include <bits/stdc++.h> #define pb push_back using namespace std; const int N = 3015; const int logN = 15; vector <int> wywolanie[15]; bool vis[N]; void odcykl(int x, vector <int> input) { vector <int> cykl = {x}; vis[x] = true; int ptr = x; while (input[ptr] != x) { ptr = input[ptr]; cykl.pb(ptr); vis[ptr] = true; } int numer_wywolania = 0; int size = cykl.size(); if (size % 2 == 1) { for (int i = 1; i <= cykl.size() / 2; i++) { wywolanie[0].pb(cykl[i]); wywolanie[0].pb(cykl[size - i]); } for (int i = size / 2; i >= 1; i--) { wywolanie[1].pb(cykl[i]); wywolanie[1].pb(cykl[(size + 1 - i) % size]); } } else { if (size == 2) { wywolanie[0].pb(cykl[0]); wywolanie[0].pb(cykl[1]); return; } for (int i = 1; i < cykl.size() / 2; i++) { wywolanie[0].pb(cykl[i]); wywolanie[0].pb(cykl[size - i]); } for (int i = size / 2; i >= 1; i--) { wywolanie[1].pb(cykl[i]); wywolanie[1].pb(cykl[(size + 1 - i) % size]); } } return; } int main() { int n; cin >> n; vector <int> noscl(n); map <int, int> scl; for (int i = 0; i < n; i++) cin >> noscl[i]; vector <int> input = noscl; sort(noscl.begin(), noscl.end()); for (int i = 0; i < n; i++) scl[noscl[i]] = i; for (int i = 0; i < n; i++) input[i] = scl[input[i]]; for (int i = 0; i < n; i++) if (!vis[i]) odcykl(i, input); if (wywolanie[1].size() == 0 and wywolanie[0].size() == 0) cout << 0 << "\n"; else if (wywolanie[1].size() == 0) cout << 1 << "\n"; else cout << 2 << "\n"; for (int i = 0; i < 15; i++) { if (wywolanie[i].size() == 0) break; cout << wywolanie[i].size() << "\n"; deque <int> dq; for (int j = 0; j < wywolanie[i].size(); j+= 2) { dq.push_front(wywolanie[i][j] + 1); dq.push_back(wywolanie[i][j + 1] + 1); } while (!dq.empty()) { int v = dq.front(); dq.pop_front(); cout << v << " "; } cout << "\n"; } 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 | #include <bits/stdc++.h> #define pb push_back using namespace std; const int N = 3015; const int logN = 15; vector <int> wywolanie[15]; bool vis[N]; void odcykl(int x, vector <int> input) { vector <int> cykl = {x}; vis[x] = true; int ptr = x; while (input[ptr] != x) { ptr = input[ptr]; cykl.pb(ptr); vis[ptr] = true; } int numer_wywolania = 0; int size = cykl.size(); if (size % 2 == 1) { for (int i = 1; i <= cykl.size() / 2; i++) { wywolanie[0].pb(cykl[i]); wywolanie[0].pb(cykl[size - i]); } for (int i = size / 2; i >= 1; i--) { wywolanie[1].pb(cykl[i]); wywolanie[1].pb(cykl[(size + 1 - i) % size]); } } else { if (size == 2) { wywolanie[0].pb(cykl[0]); wywolanie[0].pb(cykl[1]); return; } for (int i = 1; i < cykl.size() / 2; i++) { wywolanie[0].pb(cykl[i]); wywolanie[0].pb(cykl[size - i]); } for (int i = size / 2; i >= 1; i--) { wywolanie[1].pb(cykl[i]); wywolanie[1].pb(cykl[(size + 1 - i) % size]); } } return; } int main() { int n; cin >> n; vector <int> noscl(n); map <int, int> scl; for (int i = 0; i < n; i++) cin >> noscl[i]; vector <int> input = noscl; sort(noscl.begin(), noscl.end()); for (int i = 0; i < n; i++) scl[noscl[i]] = i; for (int i = 0; i < n; i++) input[i] = scl[input[i]]; for (int i = 0; i < n; i++) if (!vis[i]) odcykl(i, input); if (wywolanie[1].size() == 0 and wywolanie[0].size() == 0) cout << 0 << "\n"; else if (wywolanie[1].size() == 0) cout << 1 << "\n"; else cout << 2 << "\n"; for (int i = 0; i < 15; i++) { if (wywolanie[i].size() == 0) break; cout << wywolanie[i].size() << "\n"; deque <int> dq; for (int j = 0; j < wywolanie[i].size(); j+= 2) { dq.push_front(wywolanie[i][j] + 1); dq.push_back(wywolanie[i][j + 1] + 1); } while (!dq.empty()) { int v = dq.front(); dq.pop_front(); cout << v << " "; } cout << "\n"; } return 0; } |