//: Potyczki Algorytmiczne 2022 // FOT #include <algorithm> #include <cstdio> #include <vector> #include <deque> #include <cstddef> #include <utility> int main() { int n{}; std::scanf("%d", &n); std::vector<int> h(n); for (int i = 0; i < n; ++i) { std::scanf("%d", &h[i]); } std::vector<int> sorted(h); std::sort(sorted.begin(), sorted.end()); std::vector<int> used(n); int round{ 1 }; bool changed{}; std::vector<std::deque<int>> swapsL{}, swapsR{}; do { changed = false; swapsL.push_back({}); swapsR.push_back({}); for (int i = 0; i < n; ++i) { if (h[i] != sorted[i]) { auto it{ std::lower_bound(sorted.begin(), sorted.end(), h[i]) }; std::size_t index{ it - sorted.begin() }; if (h[index] == sorted[i]) { swapsL.back().push_back(i + 1); swapsR.back().push_front(index + 1); std::swap(h[i], h[index]); used[i] = used[index] = round; changed = true; } } } for (int i = 0; i < n; ++i) { if (h[i] != sorted[i]) { auto it{ std::lower_bound(sorted.begin(), sorted.end(), h[i]) }; std::size_t index{ it - sorted.begin() }; if ((used[i] < round) && (used[index] < round)) { swapsL.back().push_back(i + 1); swapsR.back().push_front(index + 1); used[i] = used[index] = round; std::swap(h[i], h[index]); changed = true; } } } ++round; } while (changed); swapsL.pop_back(); swapsR.pop_back(); const std::size_t rounds{ swapsL.size() }; std::printf("%lu\n", rounds); for (std::size_t i = 0; i < rounds; ++i) { std::printf("%lu\n", swapsL[i].size() * 2); for (int swap : swapsL[i]) { std::printf("%d ", swap); } for (int swap : swapsR[i]) { std::printf("%d ", swap); } std::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 | //: Potyczki Algorytmiczne 2022 // FOT #include <algorithm> #include <cstdio> #include <vector> #include <deque> #include <cstddef> #include <utility> int main() { int n{}; std::scanf("%d", &n); std::vector<int> h(n); for (int i = 0; i < n; ++i) { std::scanf("%d", &h[i]); } std::vector<int> sorted(h); std::sort(sorted.begin(), sorted.end()); std::vector<int> used(n); int round{ 1 }; bool changed{}; std::vector<std::deque<int>> swapsL{}, swapsR{}; do { changed = false; swapsL.push_back({}); swapsR.push_back({}); for (int i = 0; i < n; ++i) { if (h[i] != sorted[i]) { auto it{ std::lower_bound(sorted.begin(), sorted.end(), h[i]) }; std::size_t index{ it - sorted.begin() }; if (h[index] == sorted[i]) { swapsL.back().push_back(i + 1); swapsR.back().push_front(index + 1); std::swap(h[i], h[index]); used[i] = used[index] = round; changed = true; } } } for (int i = 0; i < n; ++i) { if (h[i] != sorted[i]) { auto it{ std::lower_bound(sorted.begin(), sorted.end(), h[i]) }; std::size_t index{ it - sorted.begin() }; if ((used[i] < round) && (used[index] < round)) { swapsL.back().push_back(i + 1); swapsR.back().push_front(index + 1); used[i] = used[index] = round; std::swap(h[i], h[index]); changed = true; } } } ++round; } while (changed); swapsL.pop_back(); swapsR.pop_back(); const std::size_t rounds{ swapsL.size() }; std::printf("%lu\n", rounds); for (std::size_t i = 0; i < rounds; ++i) { std::printf("%lu\n", swapsL[i].size() * 2); for (int swap : swapsL[i]) { std::printf("%d ", swap); } for (int swap : swapsR[i]) { std::printf("%d ", swap); } std::printf("\n"); } } |