#include <iostream> #include <algorithm> #include <vector> #include <deque> using namespace std; int binsearch(int tab[], int n, int x) { int l = 1, p = n; while (l < p) { int s = (l+p)/2; if (x <= tab[s]) p = s; else l = s+1; } return l; } void print(deque<int> contener) { for (auto i : contener) cout << i << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int graduates[n+1]; int sorted_graduates[n+1]; int order[n+1]; int permutation[n+1]; int wektor[n+1]; for (int i = 1; i <= n; i++) { cin >> graduates[i]; sorted_graduates[i] = graduates[i]; wektor[i] = 0; } sort(sorted_graduates+1, sorted_graduates+n+1); for (int i = 1; i <= n; i++) order[i] = binsearch(sorted_graduates, n, graduates[i]); for (int i = 1; i <= n; i++) permutation[order[i]] = i; vector<vector<int>> cycles; for (int i = 1; i <= n; i++) { if (wektor[i] == 0) { wektor[i] = 1; vector<int> cycle; cycle.push_back(i); int begin = i; int next = permutation[begin]; while (next != begin) { wektor[next] = 1; cycle.push_back(next); next = permutation[next]; } cycles.push_back(cycle); } } bool one_round = true; deque<int> round; vector<vector<int>> rests; for (auto c : cycles) { if (c.size() > 2) { one_round = false; for (int i = 0; i < c.size(); i += 2) { if (i+1 < c.size()) { round.push_front(c.at(i)); round.push_back(c.at(i+1)); if (i+2 < c.size()) { vector<int> rest; rest.push_back(c.at(i+1)); rest.push_back(c.at(i+2)); rests.push_back(rest); } } } } else if (c.size() == 2) { round.push_front(c.front()); round.push_back(c.back()); } } if (one_round) { cout << 1 << "\n" << round.size() << "\n"; print(round); } else { cout << 2 << "\n" << round.size() << "\n"; print(round); round.clear(); for (auto r : rests) { round.push_front(r.front()); round.push_back(r.back()); } cout << round.size() << "\n"; print(round); } 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 109 110 111 112 113 114 115 | #include <iostream> #include <algorithm> #include <vector> #include <deque> using namespace std; int binsearch(int tab[], int n, int x) { int l = 1, p = n; while (l < p) { int s = (l+p)/2; if (x <= tab[s]) p = s; else l = s+1; } return l; } void print(deque<int> contener) { for (auto i : contener) cout << i << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int graduates[n+1]; int sorted_graduates[n+1]; int order[n+1]; int permutation[n+1]; int wektor[n+1]; for (int i = 1; i <= n; i++) { cin >> graduates[i]; sorted_graduates[i] = graduates[i]; wektor[i] = 0; } sort(sorted_graduates+1, sorted_graduates+n+1); for (int i = 1; i <= n; i++) order[i] = binsearch(sorted_graduates, n, graduates[i]); for (int i = 1; i <= n; i++) permutation[order[i]] = i; vector<vector<int>> cycles; for (int i = 1; i <= n; i++) { if (wektor[i] == 0) { wektor[i] = 1; vector<int> cycle; cycle.push_back(i); int begin = i; int next = permutation[begin]; while (next != begin) { wektor[next] = 1; cycle.push_back(next); next = permutation[next]; } cycles.push_back(cycle); } } bool one_round = true; deque<int> round; vector<vector<int>> rests; for (auto c : cycles) { if (c.size() > 2) { one_round = false; for (int i = 0; i < c.size(); i += 2) { if (i+1 < c.size()) { round.push_front(c.at(i)); round.push_back(c.at(i+1)); if (i+2 < c.size()) { vector<int> rest; rest.push_back(c.at(i+1)); rest.push_back(c.at(i+2)); rests.push_back(rest); } } } } else if (c.size() == 2) { round.push_front(c.front()); round.push_back(c.back()); } } if (one_round) { cout << 1 << "\n" << round.size() << "\n"; print(round); } else { cout << 2 << "\n" << round.size() << "\n"; print(round); round.clear(); for (auto r : rests) { round.push_front(r.front()); round.push_back(r.back()); } cout << round.size() << "\n"; print(round); } return 0; } |