#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 3e3+7; int a[mxN],b[mxN], c[mxN], mx = 0; // b = {index, wartosc}, c = {wartosc, index} bool vis[mxN]; vector<int> cycle; vector<pair<int,int>> pierw; vector<pair<int,int>> drug; void dfs(int v){ cycle.pb(v); vis[v] = 1; if(!vis[a[v]]) dfs(a[v]); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i,1,n) cin >> a[i]; rep(i,1,n) b[i] = a[i]; sort(b+1,b+n+1); rep(i,1,n) c[b[i]] = i; rep(i,1,n) a[i] = c[a[i]]; rep(i,1,n) if(!vis[i]){ dfs(i); mx = max(mx, sz(cycle)); int x = 0, y = sz(cycle)-1; while(sz(cycle) > 1 && x < y){ pierw.pb({cycle[x],cycle[y]}); x++, y--; } cycle.clear(); } if(mx == 1) cout << "0\n"; else if(mx == 2){ cout << "1\n"; rep(i,1,n) if(a[i] < i) drug.pb({a[i] , i}); cout << 2*sz(drug) << "\n"; for(auto [u,v] : drug) cout << u << " "; reverse(all(drug)); for(auto [u,v] : drug) cout << v << " "; cout << "\n"; }else{ cout << "2\n"; cout << 2*sz(pierw) << "\n"; for(auto [u,v] : pierw) cout << u << " "; reverse(all(pierw)); for(auto [u,v] : pierw) cout << v << " "; cout << "\n"; for(auto [u,v] : pierw) swap(a[u], a[v]); rep(i,1,n) if(a[i] < i) drug.pb({a[i] , i}); cout << 2*sz(drug) << "\n"; for(auto [u,v] : drug) cout << u << " "; reverse(all(drug)); for(auto [u,v] : drug) cout << v << " "; cout << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 3e3+7; int a[mxN],b[mxN], c[mxN], mx = 0; // b = {index, wartosc}, c = {wartosc, index} bool vis[mxN]; vector<int> cycle; vector<pair<int,int>> pierw; vector<pair<int,int>> drug; void dfs(int v){ cycle.pb(v); vis[v] = 1; if(!vis[a[v]]) dfs(a[v]); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i,1,n) cin >> a[i]; rep(i,1,n) b[i] = a[i]; sort(b+1,b+n+1); rep(i,1,n) c[b[i]] = i; rep(i,1,n) a[i] = c[a[i]]; rep(i,1,n) if(!vis[i]){ dfs(i); mx = max(mx, sz(cycle)); int x = 0, y = sz(cycle)-1; while(sz(cycle) > 1 && x < y){ pierw.pb({cycle[x],cycle[y]}); x++, y--; } cycle.clear(); } if(mx == 1) cout << "0\n"; else if(mx == 2){ cout << "1\n"; rep(i,1,n) if(a[i] < i) drug.pb({a[i] , i}); cout << 2*sz(drug) << "\n"; for(auto [u,v] : drug) cout << u << " "; reverse(all(drug)); for(auto [u,v] : drug) cout << v << " "; cout << "\n"; }else{ cout << "2\n"; cout << 2*sz(pierw) << "\n"; for(auto [u,v] : pierw) cout << u << " "; reverse(all(pierw)); for(auto [u,v] : pierw) cout << v << " "; cout << "\n"; for(auto [u,v] : pierw) swap(a[u], a[v]); rep(i,1,n) if(a[i] < i) drug.pb({a[i] , i}); cout << 2*sz(drug) << "\n"; for(auto [u,v] : drug) cout << u << " "; reverse(all(drug)); for(auto [u,v] : drug) cout << v << " "; cout << "\n"; } } |