#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define ii pair<int, int> #define ll long long int n, a[3007], b[3007], c[3007]; //Autor: Jakub Wielocha // VI LO Radom vector<deque<int> > v; deque<int> q; int taken[3007]; bool spr; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i = 1;i<=n;i++) cin>>a[i]; for(int i = 1;i<=n;i++) b[i] = a[i]; sort(b+1, b+n+1); for(int i = 1;i<=n;i++) c[b[i]] = i; for(int i = 1;i<=n;i++) a[i] = c[a[i]]; bool spr = true; for(int i = 1;i<=n;i++) if(a[i]!=i) spr = false; if(spr) { cout<<0; return 0; } int act = 0; while(1) { act++; q.clear(); for(int i = 1;i<=n;i++) { if(a[i]!=i and taken[i]!=act and taken[a[i]]!=act) { taken[i] = act; taken[a[i]] = act; q.push_front(i); q.push_back(a[i]); swap(a[i], a[a[i]]); } } v.push_back(q); spr = true; for(int i = 1;i<=n;i++) if(a[i]!=i) spr = false; if(spr) break; } cout<<v.size()<<"\n"; for(int i = 0;i<v.size();i++) { cout<<v[i].size()<<"\n"; while(!v[i].empty()) { cout<<v[i].front()<<" "; v[i].pop_front(); } 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 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; #define X first #define Y second #define ii pair<int, int> #define ll long long int n, a[3007], b[3007], c[3007]; //Autor: Jakub Wielocha // VI LO Radom vector<deque<int> > v; deque<int> q; int taken[3007]; bool spr; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i = 1;i<=n;i++) cin>>a[i]; for(int i = 1;i<=n;i++) b[i] = a[i]; sort(b+1, b+n+1); for(int i = 1;i<=n;i++) c[b[i]] = i; for(int i = 1;i<=n;i++) a[i] = c[a[i]]; bool spr = true; for(int i = 1;i<=n;i++) if(a[i]!=i) spr = false; if(spr) { cout<<0; return 0; } int act = 0; while(1) { act++; q.clear(); for(int i = 1;i<=n;i++) { if(a[i]!=i and taken[i]!=act and taken[a[i]]!=act) { taken[i] = act; taken[a[i]] = act; q.push_front(i); q.push_back(a[i]); swap(a[i], a[a[i]]); } } v.push_back(q); spr = true; for(int i = 1;i<=n;i++) if(a[i]!=i) spr = false; if(spr) break; } cout<<v.size()<<"\n"; for(int i = 0;i<v.size();i++) { cout<<v[i].size()<<"\n"; while(!v[i].empty()) { cout<<v[i].front()<<" "; v[i].pop_front(); } cout<<"\n"; } return 0; } |