#include <algorithm> #include <cstdio> #include <vector> int main() { int n; scanf("%d", &n); std::vector<std::pair<int, int>> perm(n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); perm[i] = {a, i}; } std::sort(perm.begin(), perm.end()); std::vector<std::vector<int>> cycles; bool one_turn = true; for (int i = 0; i < n; ++i) { if (perm[i].second != -1) { cycles.emplace_back(); int current = i, next = perm[current].second; while (next != -1) { cycles.back().push_back(current); perm[current].second = -1; current = next; next = perm[current].second; } std::reverse(cycles.back().begin(), cycles.back().end()); if (cycles.back().size() == 1) { cycles.pop_back(); } else { one_turn &= cycles.back().size() % 2 == 0; } } } if (cycles.size() == 0) { printf("0\n"); return 0; } if (one_turn) { printf("1\n"); int total_size = 0; for (unsigned i = 0; i < cycles.size(); ++i) { total_size += cycles[i].size(); } printf("%d\n", total_size); for (unsigned i = 0; i < cycles.size(); ++i) { for (unsigned j = 0; j < cycles[i].size() / 2; ++j) { printf("%d ", cycles[i][j] + 1); } } for (int i = cycles.size() - 1; i >= 0; --i) { for (unsigned j = cycles[i].size() / 2; j < cycles[i].size(); ++j) { printf("%d ", cycles[i][j] + 1); } } printf("\n"); } else { printf("2\n"); int total_size_1 = 0, total_size_2 = 0; for (unsigned i = 0; i < cycles.size(); ++i) { if (cycles[i].size() % 2 == 1) { total_size_1 += 2; total_size_2--; } total_size_2 += cycles[i].size(); } printf("%d\n", total_size_1); for (unsigned i = 0; i < cycles.size(); ++i) { if (cycles[i].size() % 2 == 1) { printf("%d ", cycles[i][0] + 1); } } for (int i = cycles.size() - 1; i >= 0; --i) { if (cycles[i].size() % 2 == 1) { printf("%d ", cycles[i][1] + 1); auto it = cycles[i].begin(); it++; cycles[i].erase(it); } } printf("\n%d\n", total_size_2); for (int i = cycles.size() - 1; i >= 0; --i) { for (unsigned j = 0; j < cycles[i].size() / 2; ++j) { printf("%d ", cycles[i][j] + 1); } } for (unsigned i = 0; i < cycles.size(); ++i) { for (unsigned j = cycles[i].size() / 2; j < cycles[i].size(); ++j) { printf("%d ", cycles[i][j] + 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <algorithm> #include <cstdio> #include <vector> int main() { int n; scanf("%d", &n); std::vector<std::pair<int, int>> perm(n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); perm[i] = {a, i}; } std::sort(perm.begin(), perm.end()); std::vector<std::vector<int>> cycles; bool one_turn = true; for (int i = 0; i < n; ++i) { if (perm[i].second != -1) { cycles.emplace_back(); int current = i, next = perm[current].second; while (next != -1) { cycles.back().push_back(current); perm[current].second = -1; current = next; next = perm[current].second; } std::reverse(cycles.back().begin(), cycles.back().end()); if (cycles.back().size() == 1) { cycles.pop_back(); } else { one_turn &= cycles.back().size() % 2 == 0; } } } if (cycles.size() == 0) { printf("0\n"); return 0; } if (one_turn) { printf("1\n"); int total_size = 0; for (unsigned i = 0; i < cycles.size(); ++i) { total_size += cycles[i].size(); } printf("%d\n", total_size); for (unsigned i = 0; i < cycles.size(); ++i) { for (unsigned j = 0; j < cycles[i].size() / 2; ++j) { printf("%d ", cycles[i][j] + 1); } } for (int i = cycles.size() - 1; i >= 0; --i) { for (unsigned j = cycles[i].size() / 2; j < cycles[i].size(); ++j) { printf("%d ", cycles[i][j] + 1); } } printf("\n"); } else { printf("2\n"); int total_size_1 = 0, total_size_2 = 0; for (unsigned i = 0; i < cycles.size(); ++i) { if (cycles[i].size() % 2 == 1) { total_size_1 += 2; total_size_2--; } total_size_2 += cycles[i].size(); } printf("%d\n", total_size_1); for (unsigned i = 0; i < cycles.size(); ++i) { if (cycles[i].size() % 2 == 1) { printf("%d ", cycles[i][0] + 1); } } for (int i = cycles.size() - 1; i >= 0; --i) { if (cycles[i].size() % 2 == 1) { printf("%d ", cycles[i][1] + 1); auto it = cycles[i].begin(); it++; cycles[i].erase(it); } } printf("\n%d\n", total_size_2); for (int i = cycles.size() - 1; i >= 0; --i) { for (unsigned j = 0; j < cycles[i].size() / 2; ++j) { printf("%d ", cycles[i][j] + 1); } } for (unsigned i = 0; i < cycles.size(); ++i) { for (unsigned j = cycles[i].size() / 2; j < cycles[i].size(); ++j) { printf("%d ", cycles[i][j] + 1); } } printf("\n"); } } |