/* Jakub Żojdzik 15-12-2022 */ #include <bits/stdc++.h> #ifdef LOCAL #include "debug.hpp" #else #define debug(...) #define dt #endif using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int, int>> vpii; typedef vector<pair<long long, long long>> vpll; #define fi first #define se second #define pb push_back #define rep(i, x, y) for(ll i = (ll)x; i <= (ll)y; i++) #define all(x) x.begin(), x.end() #define sz(x) (ll)(x).size() #define nl cout << '\n' vpii input; int h[3007]; bitset<3007> seen; deque<int> res[3007]; int itr; int main() { cout.tie(0); cin.tie(0)->sync_with_stdio(0); int n, tmp; cin >> n; for(int i = 0; i < n; i++) { cin >> tmp; input.pb({tmp, i}); } sort(all(input)); int rozne = 0; rep(i, 0, n-1) { h[input[i].se] = i; rozne += (i != h[i]); } while(rozne) { seen.reset(); for(int i = 0; i < n; i++) { vi cykl; if(seen[i] || i == h[i]) continue; int akt = i; while(!seen[akt]) { cykl.pb(akt); seen[akt] = 1; akt = h[akt]; } if(sz(cykl) == 2) { res[itr].push_back(cykl[0]); res[itr].push_front(cykl[1]); swap(h[cykl[0]], h[cykl[1]]); } else { for(int j = 1; j <= (sz(cykl)-1)/2; j++) { res[itr].push_back(cykl[j]); res[itr].push_front(cykl[sz(cykl)-j]); swap(h[cykl[j]], h[cykl[sz(cykl)-j]]); } } rozne = 0; for(int i = 0; i < n; i++) { rozne += (h[i] != i); } } itr++; } cout << itr << '\n'; for(int i = 0; i < itr; i++) { cout << sz(res[i]) << '\n'; for(int x : res[i]) { cout << x+1 << ' '; } nl; } } /* */
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 | /* Jakub Żojdzik 15-12-2022 */ #include <bits/stdc++.h> #ifdef LOCAL #include "debug.hpp" #else #define debug(...) #define dt #endif using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int, int>> vpii; typedef vector<pair<long long, long long>> vpll; #define fi first #define se second #define pb push_back #define rep(i, x, y) for(ll i = (ll)x; i <= (ll)y; i++) #define all(x) x.begin(), x.end() #define sz(x) (ll)(x).size() #define nl cout << '\n' vpii input; int h[3007]; bitset<3007> seen; deque<int> res[3007]; int itr; int main() { cout.tie(0); cin.tie(0)->sync_with_stdio(0); int n, tmp; cin >> n; for(int i = 0; i < n; i++) { cin >> tmp; input.pb({tmp, i}); } sort(all(input)); int rozne = 0; rep(i, 0, n-1) { h[input[i].se] = i; rozne += (i != h[i]); } while(rozne) { seen.reset(); for(int i = 0; i < n; i++) { vi cykl; if(seen[i] || i == h[i]) continue; int akt = i; while(!seen[akt]) { cykl.pb(akt); seen[akt] = 1; akt = h[akt]; } if(sz(cykl) == 2) { res[itr].push_back(cykl[0]); res[itr].push_front(cykl[1]); swap(h[cykl[0]], h[cykl[1]]); } else { for(int j = 1; j <= (sz(cykl)-1)/2; j++) { res[itr].push_back(cykl[j]); res[itr].push_front(cykl[sz(cykl)-j]); swap(h[cykl[j]], h[cykl[sz(cykl)-j]]); } } rozne = 0; for(int i = 0; i < n; i++) { rozne += (h[i] != i); } } itr++; } cout << itr << '\n'; for(int i = 0; i < itr; i++) { cout << sz(res[i]) << '\n'; for(int x : res[i]) { cout << x+1 << ' '; } nl; } } /* */ |