#include <iostream> #include <algorithm> #include <vector> #include <bitset> using namespace std; constexpr int N = 3007; vector <vector <int>> ans; vector <pair <int, int>> h; vector <int> v1, v2; bitset <N> used; int des[N], pos[N]; inline bool check() { for(int i = 1; i <= (int)h.size(); i++) if(des[i] != i) return 0; return 1; } inline int dfs0(int v) { int s = v, wlk = 1; while(des[v] != s) { v = des[v]; wlk++; } return wlk - 1; } inline void dfs(int v) { vector <int> act; int tot = dfs0(v), wlk = 0; if(tot <= 1) return; used[v] = 1; v = des[v]; while(!used[v]) { used[v] = 1; wlk++; if(!((tot & 1) && wlk * 2 == tot + 1)) { if(wlk * 2 <= tot) v1.push_back(v); else act.push_back(v); } v = des[v]; } while(!act.empty()) { v2.push_back(act.back()); act.pop_back(); } } inline void dfs1(int v) { used[v] = used[des[v]] = 1; v1.push_back(v); v = des[v]; v2.push_back(v); } inline void sort() { for(int i = 1; i <= (int)h.size(); i++) if(!used[i] && des[i] != i) dfs(i); while(!v2.empty()) { v1.push_back(v2.back()); v2.pop_back(); } v2.erase(v2.begin(), v2.end()); for(int i = 0; i < (int)v1.size(); i++) v2.push_back(v1[i]); for(int i = 0; i < (int)v1.size() / 2; i++) { swap(pos[v1[i]], pos[v1[v1.size() - 1 - i]]); swap(des[v1[i]], des[v1[v2.size() - 1 - i]]); } if(!v2.empty()) ans.push_back(v2); v1.erase(v1.begin(), v1.end()); v2.erase(v2.begin(), v2.end()); used.reset(); for(int i = 1; i <= (int)h.size(); i++) if(i != pos[i]) { v1.push_back(i); v2.push_back(pos[i]); swap(pos[i], pos[pos[i]]); } while(!v2.empty()) { v1.push_back(v2.back()); v2.pop_back(); } if(!v1.empty()) ans.push_back(v1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) { int a; cin >> a; h.push_back({a, i}); } sort(h.begin(), h.end()); for(int i = 0; i < n; i++) { des[i + 1] = h[i].second; pos[h[i].second] = i + 1; } sort(); cout << ans.size() << "\n"; for(int i = 0; i < (int)ans.size(); i++) { cout << ans[i].size() << "\n"; for(int j = 0; j < (int)ans[i].size(); j++) cout << ans[i][j] << " "; 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 100 101 102 103 104 105 106 107 108 109 110 111 | #include <iostream> #include <algorithm> #include <vector> #include <bitset> using namespace std; constexpr int N = 3007; vector <vector <int>> ans; vector <pair <int, int>> h; vector <int> v1, v2; bitset <N> used; int des[N], pos[N]; inline bool check() { for(int i = 1; i <= (int)h.size(); i++) if(des[i] != i) return 0; return 1; } inline int dfs0(int v) { int s = v, wlk = 1; while(des[v] != s) { v = des[v]; wlk++; } return wlk - 1; } inline void dfs(int v) { vector <int> act; int tot = dfs0(v), wlk = 0; if(tot <= 1) return; used[v] = 1; v = des[v]; while(!used[v]) { used[v] = 1; wlk++; if(!((tot & 1) && wlk * 2 == tot + 1)) { if(wlk * 2 <= tot) v1.push_back(v); else act.push_back(v); } v = des[v]; } while(!act.empty()) { v2.push_back(act.back()); act.pop_back(); } } inline void dfs1(int v) { used[v] = used[des[v]] = 1; v1.push_back(v); v = des[v]; v2.push_back(v); } inline void sort() { for(int i = 1; i <= (int)h.size(); i++) if(!used[i] && des[i] != i) dfs(i); while(!v2.empty()) { v1.push_back(v2.back()); v2.pop_back(); } v2.erase(v2.begin(), v2.end()); for(int i = 0; i < (int)v1.size(); i++) v2.push_back(v1[i]); for(int i = 0; i < (int)v1.size() / 2; i++) { swap(pos[v1[i]], pos[v1[v1.size() - 1 - i]]); swap(des[v1[i]], des[v1[v2.size() - 1 - i]]); } if(!v2.empty()) ans.push_back(v2); v1.erase(v1.begin(), v1.end()); v2.erase(v2.begin(), v2.end()); used.reset(); for(int i = 1; i <= (int)h.size(); i++) if(i != pos[i]) { v1.push_back(i); v2.push_back(pos[i]); swap(pos[i], pos[pos[i]]); } while(!v2.empty()) { v1.push_back(v2.back()); v2.pop_back(); } if(!v1.empty()) ans.push_back(v1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) { int a; cin >> a; h.push_back({a, i}); } sort(h.begin(), h.end()); for(int i = 0; i < n; i++) { des[i + 1] = h[i].second; pos[h[i].second] = i + 1; } sort(); cout << ans.size() << "\n"; for(int i = 0; i < (int)ans.size(); i++) { cout << ans[i].size() << "\n"; for(int j = 0; j < (int)ans[i].size(); j++) cout << ans[i][j] << " "; cout << "\n"; } return 0; } |