#include<bits/stdc++.h> using namespace std; pair<int,int>leg[3010]; int tab[3010]; vector<vector<pair<int,int>>>pary; bool zaz[3010]; int main() { int n, i; scanf("%d", &n); for(i=1;i<=n;i++){ scanf("%d", &leg[i].first); leg[i].second = i; } sort(leg+1, leg+n+1); for(i=1;i<=n;i++){ tab[leg[i].second] = i; } while(1){ bool czy = 0; for(i=1;i<=n;i++){ if(tab[i]!=i){ czy = 1; break; } } //printf("#\n"); if(czy==0) break; pary.push_back({}); for(i=1;i<=n;i++)zaz[i] = 0; for(i=1;i<=n;i++){ if(tab[i]==i || zaz[i]==1) continue; vector<int>perm; perm.push_back(i); zaz[i] = 1; int j = i; while(tab[j]!=perm[0]){ j = tab[j]; zaz[j] = 1; perm.push_back(j); } if(perm.size()==2){ pary.back().push_back({perm[0], perm[1]}); continue; } for(j=1;j<(int)perm.size();j++){ if(j>=(int)perm.size()-j) break; pary.back().push_back({perm[j], perm[perm.size()-j]}); } } for(auto j: pary.back()){ swap(tab[j.first], tab[j.second]); } } printf("%d\n", (int)pary.size()); for(auto j: pary){ printf("%d\n", 2*j.size()); for(i=0;i<(int)j.size();i++){ printf("%d ", j[i].first); } for(i=j.size()-1;i>=0;i--){ printf("%d ", j[i].second); } printf("\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 | #include<bits/stdc++.h> using namespace std; pair<int,int>leg[3010]; int tab[3010]; vector<vector<pair<int,int>>>pary; bool zaz[3010]; int main() { int n, i; scanf("%d", &n); for(i=1;i<=n;i++){ scanf("%d", &leg[i].first); leg[i].second = i; } sort(leg+1, leg+n+1); for(i=1;i<=n;i++){ tab[leg[i].second] = i; } while(1){ bool czy = 0; for(i=1;i<=n;i++){ if(tab[i]!=i){ czy = 1; break; } } //printf("#\n"); if(czy==0) break; pary.push_back({}); for(i=1;i<=n;i++)zaz[i] = 0; for(i=1;i<=n;i++){ if(tab[i]==i || zaz[i]==1) continue; vector<int>perm; perm.push_back(i); zaz[i] = 1; int j = i; while(tab[j]!=perm[0]){ j = tab[j]; zaz[j] = 1; perm.push_back(j); } if(perm.size()==2){ pary.back().push_back({perm[0], perm[1]}); continue; } for(j=1;j<(int)perm.size();j++){ if(j>=(int)perm.size()-j) break; pary.back().push_back({perm[j], perm[perm.size()-j]}); } } for(auto j: pary.back()){ swap(tab[j.first], tab[j.second]); } } printf("%d\n", (int)pary.size()); for(auto j: pary){ printf("%d\n", 2*j.size()); for(i=0;i<(int)j.size();i++){ printf("%d ", j[i].first); } for(i=j.size()-1;i>=0;i--){ printf("%d ", j[i].second); } printf("\n"); } } |