#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = 3005; int a[nax]; bool vis[nax]; int n; void solve(){ cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; vector<int> uni; for(int i=1;i<=n;i++) uni.pb(a[i]); sort(uni.begin(), uni.end()); for(int i=1;i<=n;i++){ a[i] = (int)(lower_bound(uni.begin(), uni.end(), a[i]) - uni.begin()) + 1; } vector<vector<int> > cycles; int mx = 0; for(int i=1;i<=n;i++){ if(!vis[i]){ vector<int> cur; vis[i] = true; int tmp = a[i]; cur.pb(i); while(!vis[tmp]){ vis[tmp] = true; cur.pb(tmp); tmp = a[tmp]; } cycles.pb(cur); mx = max(mx, (int)cur.size()); } } vector<vector<int> > ans; if(mx > 2){ deque<int> L, R; for(auto cur : cycles){ if((int)cur.size() <= 2) continue; int l = 0; int r = (int)cur.size() - 1; int sz = cur.size(); while(l < r){ L.pb(cur[l + 1]); R.push_front(cur[(r + 1) % sz]); int p1 = cur[l + 1]; int p2 = cur[(r + 1) % sz]; swap(a[p1], a[p2]); l++; r--; } } vector<int> cur; for(int x : L) cur.pb(x); for(int x : R) cur.pb(x); if(cur.size()) ans.pb(cur); } deque<int> L, R; for(int i=1;i<=n;i++) vis[i] = false; for(int i=1;i<=n;i++){ if(!vis[i] && a[i] != i){ vis[i] = true; vis[a[i]] = true; int p1 = i; int p2 = a[i]; L.pb(p1); R.push_front(p2); swap(a[p1], a[p2]); } } vector<int> cur; for(int x : L) cur.pb(x); for(int x : R) cur.pb(x); if(cur.size()) ans.pb(cur); cout << ans.size() << "\n"; for(auto x : ans){ cout << x.size() << "\n"; for(auto y : x) cout << y << " "; cout << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); 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 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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = 3005; int a[nax]; bool vis[nax]; int n; void solve(){ cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; vector<int> uni; for(int i=1;i<=n;i++) uni.pb(a[i]); sort(uni.begin(), uni.end()); for(int i=1;i<=n;i++){ a[i] = (int)(lower_bound(uni.begin(), uni.end(), a[i]) - uni.begin()) + 1; } vector<vector<int> > cycles; int mx = 0; for(int i=1;i<=n;i++){ if(!vis[i]){ vector<int> cur; vis[i] = true; int tmp = a[i]; cur.pb(i); while(!vis[tmp]){ vis[tmp] = true; cur.pb(tmp); tmp = a[tmp]; } cycles.pb(cur); mx = max(mx, (int)cur.size()); } } vector<vector<int> > ans; if(mx > 2){ deque<int> L, R; for(auto cur : cycles){ if((int)cur.size() <= 2) continue; int l = 0; int r = (int)cur.size() - 1; int sz = cur.size(); while(l < r){ L.pb(cur[l + 1]); R.push_front(cur[(r + 1) % sz]); int p1 = cur[l + 1]; int p2 = cur[(r + 1) % sz]; swap(a[p1], a[p2]); l++; r--; } } vector<int> cur; for(int x : L) cur.pb(x); for(int x : R) cur.pb(x); if(cur.size()) ans.pb(cur); } deque<int> L, R; for(int i=1;i<=n;i++) vis[i] = false; for(int i=1;i<=n;i++){ if(!vis[i] && a[i] != i){ vis[i] = true; vis[a[i]] = true; int p1 = i; int p2 = a[i]; L.pb(p1); R.push_front(p2); swap(a[p1], a[p2]); } } vector<int> cur; for(int x : L) cur.pb(x); for(int x : R) cur.pb(x); if(cur.size()) ans.pb(cur); cout << ans.size() << "\n"; for(auto x : ans){ cout << x.size() << "\n"; for(auto y : x) cout << y << " "; cout << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } |