#include<iostream> #include<utility> #include<algorithm> #include<vector> bool pred(std::pair<int,int>& a, std::pair<int,int>& b){ return a.first < b.first; } int rn(int n){ int ret = 0; for(; n > 1;){ ret++; n = n/2 + n%2; } return ret; } int main(){ int n; std::cin >> n; std::pair<int,int>* t = new std::pair<int,int>[n]; for(int i = 0; i < n; i++){ std::cin >> t[i].first; t[i].second = i; } std::sort(t, t+n, pred); int* s = new int[n]; bool* vis = new bool[n]; for(int i = 0; i < n; i++){ s[t[i].second] = i; vis[i] = false; } std::vector< std::vector<int> > c, ct; long long unsigned int mxs = 1; for(int i = 0; i < n; i++){ if(!vis[i]){ std::vector<int> tmp = std::vector<int>(); for(int j = i; !vis[j]; j = s[j]){ tmp.push_back(j); vis[j] = true; } if(tmp.size() > 1) c.push_back(tmp); mxs = std::max(mxs, (long long unsigned int)tmp.size()); } } int r = rn(mxs); std::cout << r << "\n"; int dl; for(; c.size() > 0;){ dl = 0; std::vector<int> l, p; ct.clear(); for(auto& it : c){ std::vector<int> tmp; for(int i = 1; i < it.size(); i += 2){ l.push_back(it[i-1]); p.push_back(it[i]); dl += 2; tmp.push_back(it[i-1]); } if(it.size()%2 == 1){ tmp.push_back(it[it.size()-1]); } if(tmp.size() > 1) ct.push_back(tmp); } std::swap(c,ct); std::cout << dl << "\n"; for(int i = 0; i < l.size(); i++) std::cout << l[i]+1 << " "; for(int i = p.size() - 1; i >= 0; i--) std::cout << p[i]+1 << " "; std::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 | #include<iostream> #include<utility> #include<algorithm> #include<vector> bool pred(std::pair<int,int>& a, std::pair<int,int>& b){ return a.first < b.first; } int rn(int n){ int ret = 0; for(; n > 1;){ ret++; n = n/2 + n%2; } return ret; } int main(){ int n; std::cin >> n; std::pair<int,int>* t = new std::pair<int,int>[n]; for(int i = 0; i < n; i++){ std::cin >> t[i].first; t[i].second = i; } std::sort(t, t+n, pred); int* s = new int[n]; bool* vis = new bool[n]; for(int i = 0; i < n; i++){ s[t[i].second] = i; vis[i] = false; } std::vector< std::vector<int> > c, ct; long long unsigned int mxs = 1; for(int i = 0; i < n; i++){ if(!vis[i]){ std::vector<int> tmp = std::vector<int>(); for(int j = i; !vis[j]; j = s[j]){ tmp.push_back(j); vis[j] = true; } if(tmp.size() > 1) c.push_back(tmp); mxs = std::max(mxs, (long long unsigned int)tmp.size()); } } int r = rn(mxs); std::cout << r << "\n"; int dl; for(; c.size() > 0;){ dl = 0; std::vector<int> l, p; ct.clear(); for(auto& it : c){ std::vector<int> tmp; for(int i = 1; i < it.size(); i += 2){ l.push_back(it[i-1]); p.push_back(it[i]); dl += 2; tmp.push_back(it[i-1]); } if(it.size()%2 == 1){ tmp.push_back(it[it.size()-1]); } if(tmp.size() > 1) ct.push_back(tmp); } std::swap(c,ct); std::cout << dl << "\n"; for(int i = 0; i < l.size(); i++) std::cout << l[i]+1 << " "; for(int i = p.size() - 1; i >= 0; i--) std::cout << p[i]+1 << " "; std::cout << "\n"; } return 0; } |