#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int n;
vector<int> t;
vector<vector<int>> answer;
vector<vector<int>> cycles;
void generateCycles() {
vector<int> a = t;
sort(a.begin(), a.end());
map<int, int> m;
for (int i = 0; i < a.size(); i++) {
m[a[i]] = i;
}
vector<bool> proc(n);
for (int i = 0; i < n; i++) {
if (!proc[i]) {
int start = i;
proc[i] = true;
if (m[t[i]] == i) {
}
else {
vector<int> cycle;
cycle.push_back(i);
int pos = m[t[i]];
while (pos != start) {
proc[pos] = true;
cycle.push_back(pos);
pos = m[t[pos]];
}
cycles.push_back(cycle);
}
}
}
}
void generateRound() {
vector<int> front, back;
for (int i = 0; i < cycles.size(); i++) {
for (int j = 0; j + 1 < cycles[i].size(); j += 2) {
front.push_back(cycles[i][j]);
back.push_back(cycles[i][j + 1]);
}
}
for (int i = 0; i < front.size(); i++){
swap(t[front[i]], t[back[i]]);
}
reverse(back.begin(), back.end());
front.insert(front.end(), back.begin(), back.end());
answer.push_back(front);
}
int main() {
cin >> n;
t.resize(n);
for (int i = 0; i < n; i++) {
cin >> t[i];
}
do {
cycles.clear();
generateCycles();
generateRound();
} while (!cycles.empty());
cout << answer.size() - 1 << "\n";
for (int i = 0; i < answer.size() - 1; i++) {
cout << answer[i].size() << "\n";
for (int j = 0; j < answer[i].size(); j++) {
cout << 1 + answer[i][j] << " ";
}
cout << "\n";
}
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; int n; vector<int> t; vector<vector<int>> answer; vector<vector<int>> cycles; void generateCycles() { vector<int> a = t; sort(a.begin(), a.end()); map<int, int> m; for (int i = 0; i < a.size(); i++) { m[a[i]] = i; } vector<bool> proc(n); for (int i = 0; i < n; i++) { if (!proc[i]) { int start = i; proc[i] = true; if (m[t[i]] == i) { } else { vector<int> cycle; cycle.push_back(i); int pos = m[t[i]]; while (pos != start) { proc[pos] = true; cycle.push_back(pos); pos = m[t[pos]]; } cycles.push_back(cycle); } } } } void generateRound() { vector<int> front, back; for (int i = 0; i < cycles.size(); i++) { for (int j = 0; j + 1 < cycles[i].size(); j += 2) { front.push_back(cycles[i][j]); back.push_back(cycles[i][j + 1]); } } for (int i = 0; i < front.size(); i++){ swap(t[front[i]], t[back[i]]); } reverse(back.begin(), back.end()); front.insert(front.end(), back.begin(), back.end()); answer.push_back(front); } int main() { cin >> n; t.resize(n); for (int i = 0; i < n; i++) { cin >> t[i]; } do { cycles.clear(); generateCycles(); generateRound(); } while (!cycles.empty()); cout << answer.size() - 1 << "\n"; for (int i = 0; i < answer.size() - 1; i++) { cout << answer[i].size() << "\n"; for (int j = 0; j < answer[i].size(); j++) { cout << 1 + answer[i][j] << " "; } cout << "\n"; } } |
English