#include <bits/stdc++.h> using namespace std; #define ff first #define ss second int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector <int> v(n); vector <pair <int, int> > ts(n); for (int i = 0; i < n; i++) { cin >> v[i]; ts[i] = {v[i], i}; } sort(ts.begin(), ts.end()); for (int i = 0; i < n; i++) ts[i].ff = i; vector <vector <int> > c; vector <bool> used(n); for (int i = 0; i < n; i++) { if (ts[i].ff != ts[i].ss && !used[ts[i].ss]) { c.emplace_back(); int j = i; while (c.back().empty() || ts[j].ss != c.back().front()) { c.back().emplace_back(ts[j].ss); used[ts[j].ss] = 1; j = ts[j].ss; } } } // for (auto i : c) { // for (auto j : i) { // cout << j << " "; // } // cout << "\n"; // } // return 0; vector <vector <int> > res(2); for (int i = 0; i < (int)c.size(); i++) { for (int j = 0; j < (int)c[i].size()/2; j++) { res[0].push_back(c[i][j]+1); res[0].push_back(c[i][c[i].size()-1-j]+1); } for (int j = 0; j < (int)(c[i].size()-1)/2; j++) { res[1].push_back(c[i][j]+1); res[1].push_back(c[i][c[i].size()-2-j]+1); } } // vector <deque <int> > res(2); // for (int i = 0; i < (int)c.size(); i++) { // for (int j = 0; j < (int)c[i].size()/2; j++) { // res[0].push_front(c[i][j]+1); // res[0].push_back(c[i][c[i].size()-1-j]+1); // } // for (int j = 0; j < (int)(c[i].size()-1)/2; j++) { // res[1].push_front(c[i][j]+1); // res[1].push_back(c[i][c[i].size()-2-j]+1); // } // } while (!res.empty() && res.back().empty()) res.pop_back(); cout << res.size() << "\n"; for (auto i : res) { cout << i.size() << "\n"; for (int j = 0; j < (int)i.size(); j+=2) cout << i[j] << " "; for (int j = (int)i.size()-1; j >= 0; j -= 2) cout << i[j] << " "; cout << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define ff first #define ss second int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector <int> v(n); vector <pair <int, int> > ts(n); for (int i = 0; i < n; i++) { cin >> v[i]; ts[i] = {v[i], i}; } sort(ts.begin(), ts.end()); for (int i = 0; i < n; i++) ts[i].ff = i; vector <vector <int> > c; vector <bool> used(n); for (int i = 0; i < n; i++) { if (ts[i].ff != ts[i].ss && !used[ts[i].ss]) { c.emplace_back(); int j = i; while (c.back().empty() || ts[j].ss != c.back().front()) { c.back().emplace_back(ts[j].ss); used[ts[j].ss] = 1; j = ts[j].ss; } } } // for (auto i : c) { // for (auto j : i) { // cout << j << " "; // } // cout << "\n"; // } // return 0; vector <vector <int> > res(2); for (int i = 0; i < (int)c.size(); i++) { for (int j = 0; j < (int)c[i].size()/2; j++) { res[0].push_back(c[i][j]+1); res[0].push_back(c[i][c[i].size()-1-j]+1); } for (int j = 0; j < (int)(c[i].size()-1)/2; j++) { res[1].push_back(c[i][j]+1); res[1].push_back(c[i][c[i].size()-2-j]+1); } } // vector <deque <int> > res(2); // for (int i = 0; i < (int)c.size(); i++) { // for (int j = 0; j < (int)c[i].size()/2; j++) { // res[0].push_front(c[i][j]+1); // res[0].push_back(c[i][c[i].size()-1-j]+1); // } // for (int j = 0; j < (int)(c[i].size()-1)/2; j++) { // res[1].push_front(c[i][j]+1); // res[1].push_back(c[i][c[i].size()-2-j]+1); // } // } while (!res.empty() && res.back().empty()) res.pop_back(); cout << res.size() << "\n"; for (auto i : res) { cout << i.size() << "\n"; for (int j = 0; j < (int)i.size(); j+=2) cout << i[j] << " "; for (int j = (int)i.size()-1; j >= 0; j -= 2) cout << i[j] << " "; cout << "\n"; } } |