#include <iostream> #include <vector> #include <algorithm> #include <list> struct Person { int height; int index; int round = 0; bool operator<(const Person& rhs) { return height < rhs.height; } friend std::ostream& operator<<(std::ostream& stream, const Person& person) { stream << person.height << " " << person.index + 1; return stream; } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<Person> pepole; pepole.resize(n); for (int i = 0; i < n; ++i) { std::cin >> pepole[i].height; pepole[i].index = i; } std::vector<Person> sorted = pepole; std::sort(sorted.begin(), sorted.end()); for (int i = 0; i < sorted.size(); ++i) { pepole[sorted[i].index].index = i; } // for (auto& person : pepole) // { // std::cout << person << std::endl; // } // std::cout << std::endl << "-----" << std::endl; std::list<std::vector<std::pair<int,int>>> rounds; bool finished = false; int current_round = 0; while (!finished) { std::vector<std::pair<int,int>> round; round.reserve(pepole.size()); finished = true; ++current_round; int i = 0; for (auto& person : pepole) { if (person.index == i || person.round == current_round || pepole[person.index].round == current_round) { ++i; continue; } person.round = current_round; pepole[person.index].round = current_round; //std::cout << "swap " << current_round << " " << i + 1 << " " << person.index + 1 << std::endl; round.emplace_back(std::make_pair(person.index + 1, i + 1)); int temp = person.index; person.index = pepole[temp].index; pepole[temp].index = temp; if (person.index != i) { //std::cout << "mis " << person.index + 1 << " " << i + 1 << std::endl; finished = false; } ++i; } if (round.size() == 0) { std::cout << "0" << std::endl; return 0; } rounds.emplace_back(std::move(round)); //break; } std::cout << rounds.size() << std::endl; for (auto round : rounds) { std::cout << round.size() * 2 << std::endl; for (int i = 0; i < round.size(); ++i) { std::cout << round[i].first << " "; } for (int i = round.size() - 1; i >= 0; --i) { std::cout << round[i].second << " "; } std::cout << std::endl; } return 0; } /* 1670 4 2011 5 1560 3 1232 1 1447 2 */
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <iostream> #include <vector> #include <algorithm> #include <list> struct Person { int height; int index; int round = 0; bool operator<(const Person& rhs) { return height < rhs.height; } friend std::ostream& operator<<(std::ostream& stream, const Person& person) { stream << person.height << " " << person.index + 1; return stream; } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<Person> pepole; pepole.resize(n); for (int i = 0; i < n; ++i) { std::cin >> pepole[i].height; pepole[i].index = i; } std::vector<Person> sorted = pepole; std::sort(sorted.begin(), sorted.end()); for (int i = 0; i < sorted.size(); ++i) { pepole[sorted[i].index].index = i; } // for (auto& person : pepole) // { // std::cout << person << std::endl; // } // std::cout << std::endl << "-----" << std::endl; std::list<std::vector<std::pair<int,int>>> rounds; bool finished = false; int current_round = 0; while (!finished) { std::vector<std::pair<int,int>> round; round.reserve(pepole.size()); finished = true; ++current_round; int i = 0; for (auto& person : pepole) { if (person.index == i || person.round == current_round || pepole[person.index].round == current_round) { ++i; continue; } person.round = current_round; pepole[person.index].round = current_round; //std::cout << "swap " << current_round << " " << i + 1 << " " << person.index + 1 << std::endl; round.emplace_back(std::make_pair(person.index + 1, i + 1)); int temp = person.index; person.index = pepole[temp].index; pepole[temp].index = temp; if (person.index != i) { //std::cout << "mis " << person.index + 1 << " " << i + 1 << std::endl; finished = false; } ++i; } if (round.size() == 0) { std::cout << "0" << std::endl; return 0; } rounds.emplace_back(std::move(round)); //break; } std::cout << rounds.size() << std::endl; for (auto round : rounds) { std::cout << round.size() * 2 << std::endl; for (int i = 0; i < round.size(); ++i) { std::cout << round[i].first << " "; } for (int i = round.size() - 1; i >= 0; --i) { std::cout << round[i].second << " "; } std::cout << std::endl; } return 0; } /* 1670 4 2011 5 1560 3 1232 1 1447 2 */ |