#include <algorithm> #include <cstdio> #include <map> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VII; int h[3000], hs[3000], p[3000]; bool u[3000]; int main() { INT(n); REP(i,n) scanf("%d", &h[i]); REP(i,n) hs[i] = h[i]; sort(hs, hs + n); map<int, int> ind; REP(i,n) ind[hs[i]] = i; REP(i,n) p[i] = ind[h[i]]; VVI v; for (;;) { bool asc = 1; REP(i,n-1) if (p[i] > p[i + 1]) { asc = 0; break; } if (asc) break; VII w; REP(i,n) u[i] = 0; REP(i,n) if (!u[i]) { VI x; int j = i; while (!u[j]) { x.PB(j); u[j] = 1; j = p[j]; } int s = SIZE(x); int s2 = s >> 1; REP(i,s2) w.PB(MP(x[i], x[s - i - 1])); } VI vv; int ws = SIZE(w); REP(i,ws) vv.PB(w[i].FT); REPD(i,ws) vv.PB(w[i].SD); v.PB(vv); FORE(it,w) swap(p[it->FT], p[it->SD]); } printf("%d\n", SIZE(v)); FORE(it,v) { printf("%d\n", SIZE(*it)); bool start = 1; FORE(jt,*it) { if (!start) printf(" "); start = 0; printf("%d", *jt + 1); } 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 71 72 73 74 75 76 | #include <algorithm> #include <cstdio> #include <map> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VII; int h[3000], hs[3000], p[3000]; bool u[3000]; int main() { INT(n); REP(i,n) scanf("%d", &h[i]); REP(i,n) hs[i] = h[i]; sort(hs, hs + n); map<int, int> ind; REP(i,n) ind[hs[i]] = i; REP(i,n) p[i] = ind[h[i]]; VVI v; for (;;) { bool asc = 1; REP(i,n-1) if (p[i] > p[i + 1]) { asc = 0; break; } if (asc) break; VII w; REP(i,n) u[i] = 0; REP(i,n) if (!u[i]) { VI x; int j = i; while (!u[j]) { x.PB(j); u[j] = 1; j = p[j]; } int s = SIZE(x); int s2 = s >> 1; REP(i,s2) w.PB(MP(x[i], x[s - i - 1])); } VI vv; int ws = SIZE(w); REP(i,ws) vv.PB(w[i].FT); REPD(i,ws) vv.PB(w[i].SD); v.PB(vv); FORE(it,w) swap(p[it->FT], p[it->SD]); } printf("%d\n", SIZE(v)); FORE(it,v) { printf("%d\n", SIZE(*it)); bool start = 1; FORE(jt,*it) { if (!start) printf(" "); start = 0; printf("%d", *jt + 1); } printf("\n"); } } |