#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> h;
vector<int> h2;
vector<bool> zmienione;
vector<vector<int>> wynik;
int n;
int najmniejsza(int start) {
int mi = 5000000;
int indeks = 0;
for (int i = start; i < h.size(); i++) {
if (h[i] < mi) {
mi = h[i];
indeks = i;
}
}
return indeks;
}
void czysc() {
for (int i = 0; i < n; i++)
zmienione[i] = 0;
}
int zamien(int a, int b) {
//cout << zmienione[a] << zmienione[b] << " ";
if (zmienione[a] == 1 || zmienione[b] == 1)
return 1;
if (zmienione[a] == 0 && zmienione[b] == 0) {
if (a != b) {
int temp = h[a];
h[a] = h[b];
h[b] = temp;
zmienione[a] = 1;
zmienione[b] = 1;
return 0;
}
else
return 1;
}
return -1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cin >> n;
int t;
for (int i = 0; i < n; i++) {
cin >> t;
h.push_back(t);
h2.push_back(t);
zmienione.push_back(0);
}
sort(h2.begin(), h2.end());
bool c = 1;
while(c == 1){
c = 0;
czysc();
wynik.push_back(vector<int>());
for (int i = 0; i < n; i++) {
int l = 0;
while (h[l] != h2[i])
l++;
// if (i != min) {
int r = zamien(l, i);
if (r == 1) {
// wynik.push_back(vector<int>());
// czysc();
// i--;
}
// if(r == -1)
// cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
if (r == 0) {
wynik[wynik.size() - 1].push_back(i);
wynik[wynik.size() - 1].push_back(l);
c = 1;
}
// }
// if(najmniejsza(i))
}
}
cout << wynik.size() - 1;
for (int i = 0; i < wynik.size()-1; i++) {
cout << "\n" << wynik[i].size() << "\n";
for (int j = 0; j < wynik[i].size() / 2; j++)
cout << wynik[i][j * 2] + 1 << " ";
for (int j = wynik[i].size() / 2; j > 0; j--)
cout << wynik[i][j * 2 - 1] + 1 << " ";
}
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> h; vector<int> h2; vector<bool> zmienione; vector<vector<int>> wynik; int n; int najmniejsza(int start) { int mi = 5000000; int indeks = 0; for (int i = start; i < h.size(); i++) { if (h[i] < mi) { mi = h[i]; indeks = i; } } return indeks; } void czysc() { for (int i = 0; i < n; i++) zmienione[i] = 0; } int zamien(int a, int b) { //cout << zmienione[a] << zmienione[b] << " "; if (zmienione[a] == 1 || zmienione[b] == 1) return 1; if (zmienione[a] == 0 && zmienione[b] == 0) { if (a != b) { int temp = h[a]; h[a] = h[b]; h[b] = temp; zmienione[a] = 1; zmienione[b] = 1; return 0; } else return 1; } return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(); cin >> n; int t; for (int i = 0; i < n; i++) { cin >> t; h.push_back(t); h2.push_back(t); zmienione.push_back(0); } sort(h2.begin(), h2.end()); bool c = 1; while(c == 1){ c = 0; czysc(); wynik.push_back(vector<int>()); for (int i = 0; i < n; i++) { int l = 0; while (h[l] != h2[i]) l++; // if (i != min) { int r = zamien(l, i); if (r == 1) { // wynik.push_back(vector<int>()); // czysc(); // i--; } // if(r == -1) // cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"; if (r == 0) { wynik[wynik.size() - 1].push_back(i); wynik[wynik.size() - 1].push_back(l); c = 1; } // } // if(najmniejsza(i)) } } cout << wynik.size() - 1; for (int i = 0; i < wynik.size()-1; i++) { cout << "\n" << wynik[i].size() << "\n"; for (int j = 0; j < wynik[i].size() / 2; j++) cout << wynik[i][j * 2] + 1 << " "; for (int j = wynik[i].size() / 2; j > 0; j--) cout << wynik[i][j * 2 - 1] + 1 << " "; } } |
English