#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, x, ilosc_zmian; pair <int, int> tab[5001]; int ind[5001]; int t[5001]; int o[5001]; vector <int> tyl, przod, tyl2, przod2; void solv(int i){ if(t[i]==i) return; ilosc_zmian++; tyl2.push_back(t[i]); przod2.push_back(i); if(t[t[i]]==i) { o[t[i]]=1; return; } x=ind[i]; tyl.push_back(t[i]); przod.push_back(x); o[t[i]]=1; o[x]=1; swap(t[x],t[t[i]]); solv(x); } void init(){ for (int i=1;i<=n;i++) { if(o[i]==0 && t[i]!=i) { tyl.push_back(i); przod.push_back(t[i]); o[t[i]]=1; o[i]=1; swap(t[t[i]], t[i]); solv(i); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1;i<=n;i++) { cin >> tab[i].first; tab[i].second=i; } sort(tab+1,tab+n+1); for (int i=1;i<=n;i++) { ind[i]=tab[i].second; t[tab[i].second]=i; } int z=0; for (int i=1;i<=n;i++) {if(t[i]==i)z++;} if (z==n) {cout <<0; return 0;} init(); if (ilosc_zmian==0) { cout << 1<<'\n'<<2*tyl.size()<<'\n'; for (int i=0;i<przod.size();i++) cout << przod[i]<< " "; for (int i=tyl.size()-1;i>=0;i--) cout << tyl[i] << " "; } else { cout << 2<<'\n'<<2*tyl.size()<<'\n'; for (int i=0;i<przod.size();i++) cout << przod[i]<< " "; for (int i=tyl.size()-1;i>=0;i--) cout << tyl[i] << " "; cout <<'\n'<<2*tyl2.size()<<'\n'; for (int i=0;i<przod2.size();i++) cout << przod2[i]<< " "; for (int i=tyl2.size()-1;i>=0;i--) cout << tyl2[i] << " "; } /*cout <<endl<<"ilosc_zmian"<<ilosc_zmian; for (int i=0;i<przod.size();i++) cout << przod[i]<< " "; for (int i=tyl.size()-1;i>=0;i--) cout << tyl[i] << " "; cout << endl; for (int i=1;i<=n;i++) cout <<t[i]<<" ";*/ /*cout <<endl<<"ind"<<endl; for (int i=1;i<=n;i++) cout <<ind[i]<<" ";*/ 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 108 109 110 111 112 113 114 115 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int n, x, ilosc_zmian; pair <int, int> tab[5001]; int ind[5001]; int t[5001]; int o[5001]; vector <int> tyl, przod, tyl2, przod2; void solv(int i){ if(t[i]==i) return; ilosc_zmian++; tyl2.push_back(t[i]); przod2.push_back(i); if(t[t[i]]==i) { o[t[i]]=1; return; } x=ind[i]; tyl.push_back(t[i]); przod.push_back(x); o[t[i]]=1; o[x]=1; swap(t[x],t[t[i]]); solv(x); } void init(){ for (int i=1;i<=n;i++) { if(o[i]==0 && t[i]!=i) { tyl.push_back(i); przod.push_back(t[i]); o[t[i]]=1; o[i]=1; swap(t[t[i]], t[i]); solv(i); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1;i<=n;i++) { cin >> tab[i].first; tab[i].second=i; } sort(tab+1,tab+n+1); for (int i=1;i<=n;i++) { ind[i]=tab[i].second; t[tab[i].second]=i; } int z=0; for (int i=1;i<=n;i++) {if(t[i]==i)z++;} if (z==n) {cout <<0; return 0;} init(); if (ilosc_zmian==0) { cout << 1<<'\n'<<2*tyl.size()<<'\n'; for (int i=0;i<przod.size();i++) cout << przod[i]<< " "; for (int i=tyl.size()-1;i>=0;i--) cout << tyl[i] << " "; } else { cout << 2<<'\n'<<2*tyl.size()<<'\n'; for (int i=0;i<przod.size();i++) cout << przod[i]<< " "; for (int i=tyl.size()-1;i>=0;i--) cout << tyl[i] << " "; cout <<'\n'<<2*tyl2.size()<<'\n'; for (int i=0;i<przod2.size();i++) cout << przod2[i]<< " "; for (int i=tyl2.size()-1;i>=0;i--) cout << tyl2[i] << " "; } /*cout <<endl<<"ilosc_zmian"<<ilosc_zmian; for (int i=0;i<przod.size();i++) cout << przod[i]<< " "; for (int i=tyl.size()-1;i>=0;i--) cout << tyl[i] << " "; cout << endl; for (int i=1;i<=n;i++) cout <<t[i]<<" ";*/ /*cout <<endl<<"ind"<<endl; for (int i=1;i<=n;i++) cout <<ind[i]<<" ";*/ return 0; } |