#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> t(n); for(int& i : t) cin >> i; vector<pair<int,int>> p(n); for(int i=0;i<n;i++) p[i] = {t[i], i}; sort(p.begin(), p.end()); for(int i=0;i<n;i++) t[p[i].second] = i; vector<vector<int>> c; for(int i=0;i<n;i++) if(t[i] != -1) { int x = t[i]; t[i] = -1; c.emplace_back(); while(x != -1) { int y = t[x]; t[x] = -1; c.back().push_back(x); x = y; } } vector<deque<int>> result; deque<int> tmp; for(auto& x : c) { if(x.size() == 2) { tmp.push_back(x[0]); tmp.push_front(x[1]); } else if(x.size() >= 3) for(int i=1;i<(x.size()+1)/2;i++) { tmp.push_back(x[i]); tmp.push_front(x[x.size()-i]); } } if(!tmp.empty()) result.push_back(tmp); tmp.clear(); for(auto& x : c) if(x.size() >= 3) for(int i=0;i<x.size()/2;i++) { tmp.push_back(x[i+1]); tmp.push_front(x[(x.size()-i)%x.size()]); } if(!tmp.empty()) result.push_back(tmp); cout << result.size() << "\n"; for(auto& r : result) { cout << r.size() << "\n"; for(int i : r) cout << i+1 << " "; 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 | #include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> t(n); for(int& i : t) cin >> i; vector<pair<int,int>> p(n); for(int i=0;i<n;i++) p[i] = {t[i], i}; sort(p.begin(), p.end()); for(int i=0;i<n;i++) t[p[i].second] = i; vector<vector<int>> c; for(int i=0;i<n;i++) if(t[i] != -1) { int x = t[i]; t[i] = -1; c.emplace_back(); while(x != -1) { int y = t[x]; t[x] = -1; c.back().push_back(x); x = y; } } vector<deque<int>> result; deque<int> tmp; for(auto& x : c) { if(x.size() == 2) { tmp.push_back(x[0]); tmp.push_front(x[1]); } else if(x.size() >= 3) for(int i=1;i<(x.size()+1)/2;i++) { tmp.push_back(x[i]); tmp.push_front(x[x.size()-i]); } } if(!tmp.empty()) result.push_back(tmp); tmp.clear(); for(auto& x : c) if(x.size() >= 3) for(int i=0;i<x.size()/2;i++) { tmp.push_back(x[i+1]); tmp.push_front(x[(x.size()-i)%x.size()]); } if(!tmp.empty()) result.push_back(tmp); cout << result.size() << "\n"; for(auto& r : result) { cout << r.size() << "\n"; for(int i : r) cout << i+1 << " "; cout << "\n"; } return 0; } |