#include<bits/stdc++.h> using namespace std; vector<pair<int,int>>v; int tab[3006],idtab[3006]; deque<int>d; vector<deque<int>>vd; int check(int n) { int a=0; for(int i=1;i<=n;i++) { //cout<<tab[i]<<" "; if(tab[i]!=i) { if(tab[tab[i]]==i&&a!=2)a=1; else a=2; } } return a; } void f2(int n) { for(int i=1;i<=n;i++) { if(tab[i]!=i) { int j=tab[i]; int a=tab[i],b=tab[j]; tab[j]=a;tab[i]=b; d.push_front(i); d.push_back(j); } } vd.push_back(d); } bool vis[3006]; bool cycle(int id,int n) { int len=1; int j=id; while(tab[j]!=id) { len++; j=tab[j]; } j=id; int l; //if(len%2==0)l=len/2; //else l=len/2 -1; l=len/2; int p1=id,p2=id,ll=0; //cout<<len<<" "<<l<<" l<= \n"; while(1) { int j2=tab[j]; for(int i=1;i<=l;i++) { //cout<<j<<" "<<j2<<" "<<id<<" id\n"; if(vis[j]==0&&vis[j2]==0&&i>ll) { //cout<<j<<" "<<j2<<" "<<id<<" id\n"; p1=j; p2=j2; ll=i; } j2=tab[j2]; } j=tab[j]; if(j==id)break; } if(ll==0)return 0; //cout<<p1<<" "<<p2<<" pp "<<" ll\n"; int a=tab[p1],b=tab[p2]; tab[p1]=b;tab[p2]=a; d.push_back(p2);// d.push_front(p1);/// vis[p2]=vis[p1]=1; return 1; } int res(int n) { int c=check(n); if(c==0)return 0; if(c==1) { f2(n); return 1; } c=0; int ans=0; while(1) { for(int i=1;i<=n;i++) { if(tab[i]!=i&&tab[tab[i]]!=i&&vis[i]==0) { if(cycle(i,n)==1)c=1; } } if(c==0) { vd.push_back(d); ans++; if(check(n)<2)break; memset(vis,0,sizeof vis); d.clear(); } c=0; } d.clear(); f2(n); return ans+1; } int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n;cin>>n; for(int i=1;i<=n;i++) { int h;cin>>h; v.push_back({h,i}); } sort(v.begin(),v.end()); int id=1; for(auto it:v) { tab[it.second]=id; idtab[id]=it.second; id++; } int ans=check(n); if(ans==0)cout<<0<<"\n"; else if(ans==1) { f2(n); cout<<1<<"\n"<<d.size()<<"\n"; while(d.size()) { cout<<d.front()<<" "; d.pop_front(); } cout<<"\n"; } else { cout<<res(n)<<"\n"; for(int i=0;i<vd.size();i++) { cout<<vd[i].size()<<"\n"; while(vd[i].size()>0) { cout<<vd[i].front()<<" "; vd[i].pop_front(); } 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 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #include<bits/stdc++.h> using namespace std; vector<pair<int,int>>v; int tab[3006],idtab[3006]; deque<int>d; vector<deque<int>>vd; int check(int n) { int a=0; for(int i=1;i<=n;i++) { //cout<<tab[i]<<" "; if(tab[i]!=i) { if(tab[tab[i]]==i&&a!=2)a=1; else a=2; } } return a; } void f2(int n) { for(int i=1;i<=n;i++) { if(tab[i]!=i) { int j=tab[i]; int a=tab[i],b=tab[j]; tab[j]=a;tab[i]=b; d.push_front(i); d.push_back(j); } } vd.push_back(d); } bool vis[3006]; bool cycle(int id,int n) { int len=1; int j=id; while(tab[j]!=id) { len++; j=tab[j]; } j=id; int l; //if(len%2==0)l=len/2; //else l=len/2 -1; l=len/2; int p1=id,p2=id,ll=0; //cout<<len<<" "<<l<<" l<= \n"; while(1) { int j2=tab[j]; for(int i=1;i<=l;i++) { //cout<<j<<" "<<j2<<" "<<id<<" id\n"; if(vis[j]==0&&vis[j2]==0&&i>ll) { //cout<<j<<" "<<j2<<" "<<id<<" id\n"; p1=j; p2=j2; ll=i; } j2=tab[j2]; } j=tab[j]; if(j==id)break; } if(ll==0)return 0; //cout<<p1<<" "<<p2<<" pp "<<" ll\n"; int a=tab[p1],b=tab[p2]; tab[p1]=b;tab[p2]=a; d.push_back(p2);// d.push_front(p1);/// vis[p2]=vis[p1]=1; return 1; } int res(int n) { int c=check(n); if(c==0)return 0; if(c==1) { f2(n); return 1; } c=0; int ans=0; while(1) { for(int i=1;i<=n;i++) { if(tab[i]!=i&&tab[tab[i]]!=i&&vis[i]==0) { if(cycle(i,n)==1)c=1; } } if(c==0) { vd.push_back(d); ans++; if(check(n)<2)break; memset(vis,0,sizeof vis); d.clear(); } c=0; } d.clear(); f2(n); return ans+1; } int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n;cin>>n; for(int i=1;i<=n;i++) { int h;cin>>h; v.push_back({h,i}); } sort(v.begin(),v.end()); int id=1; for(auto it:v) { tab[it.second]=id; idtab[id]=it.second; id++; } int ans=check(n); if(ans==0)cout<<0<<"\n"; else if(ans==1) { f2(n); cout<<1<<"\n"<<d.size()<<"\n"; while(d.size()) { cout<<d.front()<<" "; d.pop_front(); } cout<<"\n"; } else { cout<<res(n)<<"\n"; for(int i=0;i<vd.size();i++) { cout<<vd[i].size()<<"\n"; while(vd[i].size()>0) { cout<<vd[i].front()<<" "; vd[i].pop_front(); } cout<<"\n"; } } } |