#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=3010; vector<VI> cyc; int h[N],n,vis[N]; int main() { scanf("%d",&n); VI v; rep(i,1,n+1) scanf("%d",&h[i]),v.pb(h[i]); sort(all(v)); rep(i,1,n+1) { h[i]=lower_bound(all(v),h[i])-v.begin()+1; //printf("? %d %d\n",i,h[i]); } int ms=0; rep(i,1,n+1) if (!vis[i]) { int p=i; VI cc; vis[i]=1; while (1) { cc.pb(p); p=h[p]; if (vis[p]) break; vis[p]=1; } //printf("? %d\n",SZ(cc)); cyc.pb(cc); ms=max(ms,SZ(cc)); } if (ms==1) { puts("0"); return 0; } auto printp=[&](vector<PII> pc) { printf("%d\n",2*SZ(pc)); VI cc; rep(i,0,SZ(pc)) cc.pb(pc[i].fi); per(i,0,SZ(pc)) cc.pb(pc[i].se); for (auto x:cc) printf("%d ",x); puts(""); }; if (ms==2) { puts("1"); vector<PII> pc; for (auto cc:cyc) if (SZ(cc)==2) pc.pb(mp(cc[0],cc[1])); printp(pc); return 0; } puts("2"); vector<PII> pc; for (auto cc:cyc) { int m=SZ(cc)/2*2; for (int i=0;2*i<m;i++) { pc.pb(mp(cc[i],cc[m-1-i])); swap(h[cc[i]],h[cc[m-1-i]]); } } printp(pc); for (int i=1;i<=n;i++) assert(h[h[i]]==i); pc.clear(); for (int i=1;i<=n;i++) if (h[i]!=i) { int j=h[i]; pc.pb(mp(i,j)); swap(h[i],h[j]); } printp(pc); }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=3010; vector<VI> cyc; int h[N],n,vis[N]; int main() { scanf("%d",&n); VI v; rep(i,1,n+1) scanf("%d",&h[i]),v.pb(h[i]); sort(all(v)); rep(i,1,n+1) { h[i]=lower_bound(all(v),h[i])-v.begin()+1; //printf("? %d %d\n",i,h[i]); } int ms=0; rep(i,1,n+1) if (!vis[i]) { int p=i; VI cc; vis[i]=1; while (1) { cc.pb(p); p=h[p]; if (vis[p]) break; vis[p]=1; } //printf("? %d\n",SZ(cc)); cyc.pb(cc); ms=max(ms,SZ(cc)); } if (ms==1) { puts("0"); return 0; } auto printp=[&](vector<PII> pc) { printf("%d\n",2*SZ(pc)); VI cc; rep(i,0,SZ(pc)) cc.pb(pc[i].fi); per(i,0,SZ(pc)) cc.pb(pc[i].se); for (auto x:cc) printf("%d ",x); puts(""); }; if (ms==2) { puts("1"); vector<PII> pc; for (auto cc:cyc) if (SZ(cc)==2) pc.pb(mp(cc[0],cc[1])); printp(pc); return 0; } puts("2"); vector<PII> pc; for (auto cc:cyc) { int m=SZ(cc)/2*2; for (int i=0;2*i<m;i++) { pc.pb(mp(cc[i],cc[m-1-i])); swap(h[cc[i]],h[cc[m-1-i]]); } } printp(pc); for (int i=1;i<=n;i++) assert(h[h[i]]==i); pc.clear(); for (int i=1;i<=n;i++) if (h[i]!=i) { int j=h[i]; pc.pb(mp(i,j)); swap(h[i],h[j]); } printp(pc); } |