#include<bits/stdc++.h> using namespace std; #define MAX_N 3007 int n; int h; vector<int> height; vector<vector<int>> swaps; int scaled_height[MAX_N]; bool is_present[MAX_N] = { 0 }; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { cin >> h; height.push_back(h); is_present[h] = true; } int next_height = 0; for (int i = 0; i < MAX_N; i++) { if (is_present[i]) { scaled_height[i] = next_height; next_height++; } } for (int i = 0; i < n; i++) { height[i] = scaled_height[height[i]]; } while (!is_sorted(height.begin(), height.end())) { vector<vector<int>> cycles; bool visited[MAX_N] = { 0 }; for (int i = 0; i < n; i++) { if (visited[i] || height[i] == i) { continue; } visited[i] = true; int next_position = height[i]; vector<int> cycle; cycle.push_back(i); while (next_position != cycle.front()) { visited[next_position] = true; cycle.push_back(next_position); next_position = height[next_position]; } cycles.push_back(cycle); } vector<int> next_swaps; for (auto cycle : cycles) { for (size_t i = 0; i < cycle.size() / 2; i++) { next_swaps.push_back(cycle[i]); } } reverse(cycles.begin(), cycles.end()); for (auto cycle : cycles) { int start = (cycle.size() / 2) + (cycle.size() % 2); for (size_t i = start; i < cycle.size(); i++) { next_swaps.push_back(cycle[i]); } } swaps.push_back(next_swaps); for (size_t i = 0, j = next_swaps.size() - 1; i < next_swaps.size() / 2; i++, j--) { swap(height[next_swaps[i]], height[next_swaps[j]]); } } cout << swaps.size() << "\n"; for (auto swaps_round : swaps) { cout << swaps_round.size() << "\n"; for (auto swap : swaps_round) { cout << swap + 1 << " "; } cout << "\n"; } 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 | #include<bits/stdc++.h> using namespace std; #define MAX_N 3007 int n; int h; vector<int> height; vector<vector<int>> swaps; int scaled_height[MAX_N]; bool is_present[MAX_N] = { 0 }; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { cin >> h; height.push_back(h); is_present[h] = true; } int next_height = 0; for (int i = 0; i < MAX_N; i++) { if (is_present[i]) { scaled_height[i] = next_height; next_height++; } } for (int i = 0; i < n; i++) { height[i] = scaled_height[height[i]]; } while (!is_sorted(height.begin(), height.end())) { vector<vector<int>> cycles; bool visited[MAX_N] = { 0 }; for (int i = 0; i < n; i++) { if (visited[i] || height[i] == i) { continue; } visited[i] = true; int next_position = height[i]; vector<int> cycle; cycle.push_back(i); while (next_position != cycle.front()) { visited[next_position] = true; cycle.push_back(next_position); next_position = height[next_position]; } cycles.push_back(cycle); } vector<int> next_swaps; for (auto cycle : cycles) { for (size_t i = 0; i < cycle.size() / 2; i++) { next_swaps.push_back(cycle[i]); } } reverse(cycles.begin(), cycles.end()); for (auto cycle : cycles) { int start = (cycle.size() / 2) + (cycle.size() % 2); for (size_t i = start; i < cycle.size(); i++) { next_swaps.push_back(cycle[i]); } } swaps.push_back(next_swaps); for (size_t i = 0, j = next_swaps.size() - 1; i < next_swaps.size() / 2; i++, j--) { swap(height[next_swaps[i]], height[next_swaps[j]]); } } cout << swaps.size() << "\n"; for (auto swaps_round : swaps) { cout << swaps_round.size() << "\n"; for (auto swap : swaps_round) { cout << swap + 1 << " "; } cout << "\n"; } return 0; } |