#include "bits/stdc++.h" using namespace std; #define endl "\n" void scale(vector < int > & arr) { vector < int > sorted = arr; sort(sorted.begin(), sorted.end()); transform(arr.begin(), arr.end(), arr.begin(), [ & sorted](int x) { return lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin() + 1; }); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; ; vector < int > Liczby(N); for (int i = 0; i < N; i++) { cin >> Liczby[i]; } scale(Liczby); vector < int > Szpieg(N); for (int i = 0; i < N; i++) { Szpieg[i] = i + 1; } int k = 0; for (int i = 0; i < N; i++) { if (Liczby[i] != Szpieg[i]) { k++; } } if(k == 0){ cout << 0 << endl; return 0; } int rounds = 0; vector < int > R; vector < vector < int >> R1(N); while (k > 0) { vector < int > A; vector < int > B; vector < bool > Used(N, 0); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (Liczby[i] == Szpieg[j] && i != j && Used[i] == 0 && Used[j] == 0) { A.push_back(i + 1); B.push_back(j + 1); k --; if (Szpieg[i] == Liczby[j]) { k--; } Liczby[i] = Liczby[j]; Liczby[j] = j + 1; Used[i] = 1; Used[j] = 1; } } } R.push_back(A.size() + B.size()); rounds++; for (int i = 0; i < A.size(); i++) { R1[rounds - 1].push_back(A[i]); } for (int i = B.size() - 1; i >= 0; i--) { R1[rounds - 1].push_back(B[i]); } } cout << rounds << endl; for (int i = 0; i < R.size(); i++) { cout << R[i] << endl; for (int j = 0; j < R1[i].size(); j++) { cout << R1[i][j] << " "; } 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 79 80 81 82 83 84 85 86 | #include "bits/stdc++.h" using namespace std; #define endl "\n" void scale(vector < int > & arr) { vector < int > sorted = arr; sort(sorted.begin(), sorted.end()); transform(arr.begin(), arr.end(), arr.begin(), [ & sorted](int x) { return lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin() + 1; }); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; ; vector < int > Liczby(N); for (int i = 0; i < N; i++) { cin >> Liczby[i]; } scale(Liczby); vector < int > Szpieg(N); for (int i = 0; i < N; i++) { Szpieg[i] = i + 1; } int k = 0; for (int i = 0; i < N; i++) { if (Liczby[i] != Szpieg[i]) { k++; } } if(k == 0){ cout << 0 << endl; return 0; } int rounds = 0; vector < int > R; vector < vector < int >> R1(N); while (k > 0) { vector < int > A; vector < int > B; vector < bool > Used(N, 0); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (Liczby[i] == Szpieg[j] && i != j && Used[i] == 0 && Used[j] == 0) { A.push_back(i + 1); B.push_back(j + 1); k --; if (Szpieg[i] == Liczby[j]) { k--; } Liczby[i] = Liczby[j]; Liczby[j] = j + 1; Used[i] = 1; Used[j] = 1; } } } R.push_back(A.size() + B.size()); rounds++; for (int i = 0; i < A.size(); i++) { R1[rounds - 1].push_back(A[i]); } for (int i = B.size() - 1; i >= 0; i--) { R1[rounds - 1].push_back(B[i]); } } cout << rounds << endl; for (int i = 0; i < R.size(); i++) { cout << R[i] << endl; for (int j = 0; j < R1[i].size(); j++) { cout << R1[i][j] << " "; } cout << endl; } } |