#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int main() { int n; scanf("%d", &n); vector<pair<int, int> > v; vector<int> idx(1); vector<int> target(n + 1); FOR(i,0,n) { int a; scanf("%d", &a); v.push_back(MP(a, i + 1)); idx.push_back(i + 1); } sort(v.begin(), v.end()); FOR(i,0,n) { target[v[i].second] = i + 1; } vector<vector<int> > result; while (true) { vector<bool> was(n + 1, false); vector<int> s1, s2; FORE(i,1,n) { if (!was[i]) { if (i == target[idx[i]]) continue; vector<int> cycle; cycle.push_back(i); int j = target[idx[i]]; while (j != i) { was[j] = true; cycle.push_back(j); j = target[idx[j]]; } FOR(j,0,SZ(cycle) / 2) s1.push_back(cycle[j]); for(int j = SZ(cycle) - 1; j >= (SZ(cycle) + 1)/ 2; --j) s2.push_back(cycle[j]); } } if (!s1.empty()) { result.push_back(s1); for (int i = SZ(s2) - 1; i >= 0; --i) result.back().push_back(s2[i]); FOR(i,0,SZ(s1)) { swap(idx[s1[i]], idx[s2[i]]); } } else { break; } } printf("%d\n", SZ(result)); FOR(i,0,SZ(result)) { printf("%d\n", SZ(result[i])); FOR(j,0,SZ(result[i])) { printf("%d ", result[i][j]); } 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 77 78 79 80 81 82 83 84 85 86 | #include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int main() { int n; scanf("%d", &n); vector<pair<int, int> > v; vector<int> idx(1); vector<int> target(n + 1); FOR(i,0,n) { int a; scanf("%d", &a); v.push_back(MP(a, i + 1)); idx.push_back(i + 1); } sort(v.begin(), v.end()); FOR(i,0,n) { target[v[i].second] = i + 1; } vector<vector<int> > result; while (true) { vector<bool> was(n + 1, false); vector<int> s1, s2; FORE(i,1,n) { if (!was[i]) { if (i == target[idx[i]]) continue; vector<int> cycle; cycle.push_back(i); int j = target[idx[i]]; while (j != i) { was[j] = true; cycle.push_back(j); j = target[idx[j]]; } FOR(j,0,SZ(cycle) / 2) s1.push_back(cycle[j]); for(int j = SZ(cycle) - 1; j >= (SZ(cycle) + 1)/ 2; --j) s2.push_back(cycle[j]); } } if (!s1.empty()) { result.push_back(s1); for (int i = SZ(s2) - 1; i >= 0; --i) result.back().push_back(s2[i]); FOR(i,0,SZ(s1)) { swap(idx[s1[i]], idx[s2[i]]); } } else { break; } } printf("%d\n", SZ(result)); FOR(i,0,SZ(result)) { printf("%d\n", SZ(result[i])); FOR(j,0,SZ(result[i])) { printf("%d ", result[i][j]); } printf("\n"); } } |