#include <iostream> #include <algorithm> #include <vector> int main() { int n; std::cin >> n; int A[n]; for (int i = 0; i < n; i++) std::cin >> A[i]; int B[n]; std::copy(A, A + n, B); std::sort(B, B + sizeof(B) / sizeof(B[0])); int C[n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (A[i] == B[j]) C[i] = j; std::vector<std::vector<int>> result; bool work = true; for (int i = 0; work; i++) { if (!work) break; std::vector<int> row; for (int j = 0; j < n; j++) { if (C[j] == j) C[j] = -1; if (C[j] == -1) continue; if (std::find(row.begin(), row.end(), C[j] + 1) != row.end()) continue; row.push_back(C[j] + 1); row.insert(row.begin(), j + 1); C[j] = -1; } result.push_back(row); work = false; for (int j = 0; j < n; j++) { if (C[j] != -1) { work = true; break; } } } std::cout << result.size() << "\n"; for (int i = 0; i < result.size(); i++) { std::cout << result[i].size() << "\n"; for (int j = 0; j < result[i].size(); j++) { std::cout << result[i][j]; } std::cout << "\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 | #include <iostream> #include <algorithm> #include <vector> int main() { int n; std::cin >> n; int A[n]; for (int i = 0; i < n; i++) std::cin >> A[i]; int B[n]; std::copy(A, A + n, B); std::sort(B, B + sizeof(B) / sizeof(B[0])); int C[n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (A[i] == B[j]) C[i] = j; std::vector<std::vector<int>> result; bool work = true; for (int i = 0; work; i++) { if (!work) break; std::vector<int> row; for (int j = 0; j < n; j++) { if (C[j] == j) C[j] = -1; if (C[j] == -1) continue; if (std::find(row.begin(), row.end(), C[j] + 1) != row.end()) continue; row.push_back(C[j] + 1); row.insert(row.begin(), j + 1); C[j] = -1; } result.push_back(row); work = false; for (int j = 0; j < n; j++) { if (C[j] != -1) { work = true; break; } } } std::cout << result.size() << "\n"; for (int i = 0; i < result.size(); i++) { std::cout << result[i].size() << "\n"; for (int j = 0; j < result[i].size(); j++) { std::cout << result[i][j]; } std::cout << "\n"; } return 0; } |