#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector <int> kol(n); vector <int> sor(n); for (int i = 0; i < n; i++) { cin >> kol[i]; sor[i] = kol[i]; } sort (sor.begin(), sor.end()); for (int i = 0; i < n; i++) { int j = 0; while (kol[i] != sor[j]) { j++; } kol[i] = j; } vector <vector<int>> odp; for (int i = 0; i < n; i++) { sor[i] = i; } while (kol != sor) { vector <bool> odw(n, 0); vector <int> wyn1; vector <int> wyn2; for(int i = 0; i<n; i++) { int j = i; while (odw [j] == 0 && odw[kol[j]] == 0 && kol[j] != sor[j]) { wyn1.push_back(j); wyn2.push_back(kol[j]); odw [j] = 1; odw [kol[j]] = 1; int a = kol[j]; kol[j] = kol[a]; kol[a] = a; j = kol[j]; } } for(int i = 0; i < wyn2.size(); i++) { wyn1.push_back(wyn2[wyn2.size() - i -1]); } odp.push_back(wyn1); } cout << odp.size() << endl; for (int i = 0; i < odp.size(); i++) { cout << odp[i].size() << endl; for (int j = 0; j < odp[i].size(); j++) { cout << odp[i][j] + 1<< " "; } cout << endl; } }
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 | #include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector <int> kol(n); vector <int> sor(n); for (int i = 0; i < n; i++) { cin >> kol[i]; sor[i] = kol[i]; } sort (sor.begin(), sor.end()); for (int i = 0; i < n; i++) { int j = 0; while (kol[i] != sor[j]) { j++; } kol[i] = j; } vector <vector<int>> odp; for (int i = 0; i < n; i++) { sor[i] = i; } while (kol != sor) { vector <bool> odw(n, 0); vector <int> wyn1; vector <int> wyn2; for(int i = 0; i<n; i++) { int j = i; while (odw [j] == 0 && odw[kol[j]] == 0 && kol[j] != sor[j]) { wyn1.push_back(j); wyn2.push_back(kol[j]); odw [j] = 1; odw [kol[j]] = 1; int a = kol[j]; kol[j] = kol[a]; kol[a] = a; j = kol[j]; } } for(int i = 0; i < wyn2.size(); i++) { wyn1.push_back(wyn2[wyn2.size() - i -1]); } odp.push_back(wyn1); } cout << odp.size() << endl; for (int i = 0; i < odp.size(); i++) { cout << odp[i].size() << endl; for (int j = 0; j < odp[i].size(); j++) { cout << odp[i][j] + 1<< " "; } cout << endl; } } |