#include <cstdio> #include <algorithm> #include <vector> using namespace std; struct Item { int h, i, dest; bool v, moved; }; bool compareByH(const Item& a, const Item& b) { return a.h < b.h; } bool compareByI(const Item& a, const Item& b) { return a.i < b.i; } int main() { int n; scanf("%d", &n); Item* tab = new Item[3000]; for (int i = 0; i < n; i++) { tab[i].i = i; scanf("%d", &tab[i].h); } sort(tab, tab + n, compareByH); for (int i = 0; i < n; i++) { tab[i].dest = i; } sort(tab, tab + n, compareByI); vector<int> V[3000]; int r = 0; while (true) { bool stop = true; V[r].reserve(n); for (int i = 0; i < n; i++) { tab[i].moved = false; } for (int i = 0; i < n; i++) { int cur = i; int curDest = tab[cur].dest; while (!tab[cur].moved && !tab[curDest].moved && curDest != cur) { stop = false; V[r].push_back((curDest + 1) * 10000 + cur + 1); Item temp = tab[cur]; tab[cur] = tab[curDest]; tab[curDest] = temp; tab[cur].moved = true; tab[curDest].moved = true; cur = tab[curDest].dest; curDest = tab[cur].dest; } } if (stop) { break; } r++; } printf("%d\n", r); for (int i = 0; i < r; i++) { printf("%d\n", 2 * V[i].size()); for (int j = 0; j < V[i].size(); j++) { printf("%d ", V[i][j] % 10000); } for (int j = V[i].size() - 1; j >= 0; j--) { printf("%d ", V[i][j] / 10000); } 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 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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct Item { int h, i, dest; bool v, moved; }; bool compareByH(const Item& a, const Item& b) { return a.h < b.h; } bool compareByI(const Item& a, const Item& b) { return a.i < b.i; } int main() { int n; scanf("%d", &n); Item* tab = new Item[3000]; for (int i = 0; i < n; i++) { tab[i].i = i; scanf("%d", &tab[i].h); } sort(tab, tab + n, compareByH); for (int i = 0; i < n; i++) { tab[i].dest = i; } sort(tab, tab + n, compareByI); vector<int> V[3000]; int r = 0; while (true) { bool stop = true; V[r].reserve(n); for (int i = 0; i < n; i++) { tab[i].moved = false; } for (int i = 0; i < n; i++) { int cur = i; int curDest = tab[cur].dest; while (!tab[cur].moved && !tab[curDest].moved && curDest != cur) { stop = false; V[r].push_back((curDest + 1) * 10000 + cur + 1); Item temp = tab[cur]; tab[cur] = tab[curDest]; tab[curDest] = temp; tab[cur].moved = true; tab[curDest].moved = true; cur = tab[curDest].dest; curDest = tab[cur].dest; } } if (stop) { break; } r++; } printf("%d\n", r); for (int i = 0; i < r; i++) { printf("%d\n", 2 * V[i].size()); for (int j = 0; j < V[i].size(); j++) { printf("%d ", V[i][j] % 10000); } for (int j = V[i].size() - 1; j >= 0; j--) { printf("%d ", V[i][j] / 10000); } printf("\n"); } return 0; } |