#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 3030; int n; int a[N], b[N], vis[N]; pii t[N]; vi ansL[2], ansR[2]; int main() { scanf("%d", &n); FOR(i,n) scanf("%d", &a[i]); FOR(i,n) t[i] = mp(a[i], i); sort(t,t+n); FOR(i,n) b[t[i].se] = i; bool ok = true; FOR(i,n) if (i!=b[i]) ok = false; if (ok) { printf("0\n"); return 0; } int need = 1; FOR(i,n) if (!vis[i]) { int x = b[i]; vis[i] = 1; vi p; p.pb(i); while (x != i) { vis[x] = 1; p.pb(x); x = b[x]; } int m = SZ(p); //cerr << "m = " << m << "\n"; //FOR(j,m) cerr << p[j] << " "; //cerr << "\n"; if (m > 2) need = 2; for (int j = 0, k = m-1; j < k; j++, k--) { ansL[0].pb(p[j]); ansR[0].pb(p[k]); swap(p[j], p[k]); } for (int j = 0, k = m-2; j < k; j++, k--) { ansL[1].pb(p[j]); ansR[1].pb(p[k]); swap(p[j], p[k]); } } printf("%d\n", need); FOR(i,need) { int m = SZ(ansL[i]); printf("%d\n", m*2); FOR(j,m) printf("%d ", ansL[i][j]+1); FOR(j,m) printf("%d ", ansR[i][m-j-1]+1); printf("\n"); } 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 3030; int n; int a[N], b[N], vis[N]; pii t[N]; vi ansL[2], ansR[2]; int main() { scanf("%d", &n); FOR(i,n) scanf("%d", &a[i]); FOR(i,n) t[i] = mp(a[i], i); sort(t,t+n); FOR(i,n) b[t[i].se] = i; bool ok = true; FOR(i,n) if (i!=b[i]) ok = false; if (ok) { printf("0\n"); return 0; } int need = 1; FOR(i,n) if (!vis[i]) { int x = b[i]; vis[i] = 1; vi p; p.pb(i); while (x != i) { vis[x] = 1; p.pb(x); x = b[x]; } int m = SZ(p); //cerr << "m = " << m << "\n"; //FOR(j,m) cerr << p[j] << " "; //cerr << "\n"; if (m > 2) need = 2; for (int j = 0, k = m-1; j < k; j++, k--) { ansL[0].pb(p[j]); ansR[0].pb(p[k]); swap(p[j], p[k]); } for (int j = 0, k = m-2; j < k; j++, k--) { ansL[1].pb(p[j]); ansR[1].pb(p[k]); swap(p[j], p[k]); } } printf("%d\n", need); FOR(i,need) { int m = SZ(ansL[i]); printf("%d\n", m*2); FOR(j,m) printf("%d ", ansL[i][j]+1); FOR(j,m) printf("%d ", ansR[i][m-j-1]+1); printf("\n"); } return 0; } |