#include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) using namespace std; int n, t[3003], ile[3003]; vector<deque<int> > res; int main(){ iamspeed; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> t[i]; ile[t[i]] = 1; } int ind = 0; for(int i = 1 ; i <= 3000 ; i++) if(ile[i]) ile[i] = ++ind; for(int i = 1 ; i <= n ; i++) t[i] = ile[t[i]]; for(int l = 1 ; l <= 2 ; l++){ int vis[3003]; for(int i = 1 ; i <= n ; i++) vis[i] = 0; deque<int> dq; if(!dq.empty()) dq.clear(); for(int i = 1 ; i <= n ; i++){ int x = i; vector<int> V; if(!V.empty()) V.clear(); while(!vis[x]){ V.push_back(x); vis[x] = 1; x = t[x]; } for(int j = 0 ; j < V.size() / 2; j++){ dq.push_back(V[j]); dq.push_front(V[V.size() - j - 1]); } } if(!dq.empty()){ res.push_back(dq); while(!dq.empty()){ swap(t[dq.front()], t[dq.back()]); dq.pop_front(); dq.pop_back(); } } } cout << res.size() << endl; for(int i = 0 ; i < res.size() ; i++){ cout << res[i].size() << endl; while(!res[i].empty()){ cout << res[i].front() << " "; res[i].pop_front(); } cout << endl; } }
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 | #include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) using namespace std; int n, t[3003], ile[3003]; vector<deque<int> > res; int main(){ iamspeed; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> t[i]; ile[t[i]] = 1; } int ind = 0; for(int i = 1 ; i <= 3000 ; i++) if(ile[i]) ile[i] = ++ind; for(int i = 1 ; i <= n ; i++) t[i] = ile[t[i]]; for(int l = 1 ; l <= 2 ; l++){ int vis[3003]; for(int i = 1 ; i <= n ; i++) vis[i] = 0; deque<int> dq; if(!dq.empty()) dq.clear(); for(int i = 1 ; i <= n ; i++){ int x = i; vector<int> V; if(!V.empty()) V.clear(); while(!vis[x]){ V.push_back(x); vis[x] = 1; x = t[x]; } for(int j = 0 ; j < V.size() / 2; j++){ dq.push_back(V[j]); dq.push_front(V[V.size() - j - 1]); } } if(!dq.empty()){ res.push_back(dq); while(!dq.empty()){ swap(t[dq.front()], t[dq.back()]); dq.pop_front(); dq.pop_back(); } } } cout << res.size() << endl; for(int i = 0 ; i < res.size() ; i++){ cout << res[i].size() << endl; while(!res[i].empty()){ cout << res[i].front() << " "; res[i].pop_front(); } cout << endl; } } |