#include<bits/stdc++.h> using namespace std; int ein[3100]; int eout[3100]; int vis[3100]; vector<int> p1; vector<int> p2; void dfs(int a, int fat, int cnt, int d) { // cout<<a<<" "; if(vis[a] == true) cnt++; vis[a] = true; if(d==2) { if(a==fat) { if(cnt > 1) return; a = eout[fat]; int c = eout[fat]; eout[fat] = eout[a]; eout[a] = c; p1.push_back(fat); p2.push_back(a); return; } if(cnt > 1) return; int c = eout[fat]; eout[fat] = eout[a]; eout[a] = c; ein[eout[fat]] = -1; p1.push_back(fat); p2.push_back(a); if(ein[fat] == -1) return; dfs(ein[fat], ein[fat], 0, 0); return; } dfs(eout[a], fat, cnt, d+1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int> t; vector<pair<int, int> > st; for(int i=1;i<=n;i++) { int a; cin>>a; t.push_back(a); st.push_back({a, i}); } sort(st.begin(), st.end()); for(int i=0;i<st.size();i++) { ein[i+1] = st[i].second; eout[st[i].second] = i+1; } vector<vector<int> > res; while(true) { p1.clear(); p2.clear(); fill(vis, vis + n + 1, 0); for(int i=1;i<=n;i++) { if(vis[i] == true || eout[i] == i) continue; dfs(i, i, 0, 0); // cout<<endl; } if(p1.size() == 0) break; reverse(p2.begin(), p2.end()); res.push_back({}); for(auto v : p1) res.back().push_back(v); for(auto v : p2) res.back().push_back(v); // cout<<"\n\n\n\n\n\n"; } cout<<res.size()<<"\n"; for(auto v : res) { cout<<v.size()<<"\n"; for(auto p : v) cout<<p<<" "; 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 79 80 81 82 83 84 85 86 87 88 89 90 | #include<bits/stdc++.h> using namespace std; int ein[3100]; int eout[3100]; int vis[3100]; vector<int> p1; vector<int> p2; void dfs(int a, int fat, int cnt, int d) { // cout<<a<<" "; if(vis[a] == true) cnt++; vis[a] = true; if(d==2) { if(a==fat) { if(cnt > 1) return; a = eout[fat]; int c = eout[fat]; eout[fat] = eout[a]; eout[a] = c; p1.push_back(fat); p2.push_back(a); return; } if(cnt > 1) return; int c = eout[fat]; eout[fat] = eout[a]; eout[a] = c; ein[eout[fat]] = -1; p1.push_back(fat); p2.push_back(a); if(ein[fat] == -1) return; dfs(ein[fat], ein[fat], 0, 0); return; } dfs(eout[a], fat, cnt, d+1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int> t; vector<pair<int, int> > st; for(int i=1;i<=n;i++) { int a; cin>>a; t.push_back(a); st.push_back({a, i}); } sort(st.begin(), st.end()); for(int i=0;i<st.size();i++) { ein[i+1] = st[i].second; eout[st[i].second] = i+1; } vector<vector<int> > res; while(true) { p1.clear(); p2.clear(); fill(vis, vis + n + 1, 0); for(int i=1;i<=n;i++) { if(vis[i] == true || eout[i] == i) continue; dfs(i, i, 0, 0); // cout<<endl; } if(p1.size() == 0) break; reverse(p2.begin(), p2.end()); res.push_back({}); for(auto v : p1) res.back().push_back(v); for(auto v : p2) res.back().push_back(v); // cout<<"\n\n\n\n\n\n"; } cout<<res.size()<<"\n"; for(auto v : res) { cout<<v.size()<<"\n"; for(auto p : v) cout<<p<<" "; cout<<"\n"; } } |