#include <bits/stdc++.h> using namespace std; int n; vector <int> a; vector <bool> visited; inline bool sorted() { for(int i=0; i<n; ++i) if(a[i]!=i) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n; set <int> s; unordered_map <int, int> nr; a.resize(n); visited.resize(n); for(int i=0; i<n; ++i) { cin>>a[i]; s.insert(a[i]); } int curr=0; for(int x : s) { nr[x]=curr; ++curr; } for(int i=0; i<n; ++i) a[i]=nr[a[i]]; if(sorted()) { cout<<"0\n"; return 0; } bool one_move=1; for(int i=0; i<n; ++i) { if(a[a[i]]!=i) { one_move=0; break; } } if(one_move) { cout<<"1\n"; deque <int> ans; for(int i=0; i<n; ++i) { if(a[i]<i) { ans.push_front(a[i]); ans.push_back(i); } } cout<<ans.size()<<"\n"; while(!ans.empty()) { cout<<ans.front()+1<<" "; ans.pop_front(); } cout<<"\n"; return 0; } cout<<"2\n"; deque <int> ans; for(int i=0; i<n; ++i) { if(visited[i]) continue; visited[i]=1; deque <int> cycle; cycle.push_back(i); int x=i; while(a[x]!=i) { x=a[x]; cycle.push_back(x); visited[x]=1; } if(cycle.size()<2) continue; while(cycle.size()>=2) { ans.push_front(cycle.front()); ans.push_back(cycle.back()); swap(a[cycle.front()], a[cycle.back()]); cycle.pop_front(); cycle.pop_back(); } } cout<<ans.size()<<"\n"; while(!ans.empty()) { cout<<ans.front()+1<<" "; ans.pop_front(); } cout<<"\n"; ans.clear(); for(int i=0; i<n; ++i) { if(a[i]<i) { ans.push_front(a[i]); ans.push_back(i); } } cout<<ans.size()<<"\n"; while(!ans.empty()) { cout<<ans.front()+1<<" "; ans.pop_front(); } cout<<"\n"; return 0; } /* 6 3 4 5 6 2 1 */
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <bits/stdc++.h> using namespace std; int n; vector <int> a; vector <bool> visited; inline bool sorted() { for(int i=0; i<n; ++i) if(a[i]!=i) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n; set <int> s; unordered_map <int, int> nr; a.resize(n); visited.resize(n); for(int i=0; i<n; ++i) { cin>>a[i]; s.insert(a[i]); } int curr=0; for(int x : s) { nr[x]=curr; ++curr; } for(int i=0; i<n; ++i) a[i]=nr[a[i]]; if(sorted()) { cout<<"0\n"; return 0; } bool one_move=1; for(int i=0; i<n; ++i) { if(a[a[i]]!=i) { one_move=0; break; } } if(one_move) { cout<<"1\n"; deque <int> ans; for(int i=0; i<n; ++i) { if(a[i]<i) { ans.push_front(a[i]); ans.push_back(i); } } cout<<ans.size()<<"\n"; while(!ans.empty()) { cout<<ans.front()+1<<" "; ans.pop_front(); } cout<<"\n"; return 0; } cout<<"2\n"; deque <int> ans; for(int i=0; i<n; ++i) { if(visited[i]) continue; visited[i]=1; deque <int> cycle; cycle.push_back(i); int x=i; while(a[x]!=i) { x=a[x]; cycle.push_back(x); visited[x]=1; } if(cycle.size()<2) continue; while(cycle.size()>=2) { ans.push_front(cycle.front()); ans.push_back(cycle.back()); swap(a[cycle.front()], a[cycle.back()]); cycle.pop_front(); cycle.pop_back(); } } cout<<ans.size()<<"\n"; while(!ans.empty()) { cout<<ans.front()+1<<" "; ans.pop_front(); } cout<<"\n"; ans.clear(); for(int i=0; i<n; ++i) { if(a[i]<i) { ans.push_front(a[i]); ans.push_back(i); } } cout<<ans.size()<<"\n"; while(!ans.empty()) { cout<<ans.front()+1<<" "; ans.pop_front(); } cout<<"\n"; return 0; } /* 6 3 4 5 6 2 1 */ |