#include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; vector<vector<int>> get_cycles(vector<int> const & vec) { vector<vector<int>> result; vector<bool> visited(vec.size(), false); for(int i = 0; i < vec.size(); i++) { if(!visited[i]) { vector<int> cur_res; int cur_ind = i; while(!visited[cur_ind]) { cur_res.push_back(cur_ind); visited[cur_ind] = true; cur_ind = vec[cur_ind]; } result.push_back(cur_res); } } return result; } bool is_sorted(vector<int> const & vec) { for(int i = 1; i < vec.size(); i++) { if(vec[i] < vec[i-1]) return false; } return true; } void print_part_res(vector<int> const & left_res, vector<int> const & right_res) { cout << left_res.size() + right_res.size() << "\n"; for(int i = 0; i < left_res.size(); i++) { cout << left_res[i] + 1 << " "; } for(int i = right_res.size() - 1; i >= 0; i--) { cout << right_res[i] + 1 << " "; } cout << "\n"; } void solve() { int n; cin >> n; vector<pair<int,int>> pre_vec(n); for(int i = 0; i < n; i++) { int a; cin >> a; pre_vec[i] = {a, i}; } sort(pre_vec.begin(), pre_vec.end()); vector<int> vec(n); for(int i = 0; i < n; i++) { vec[pre_vec[i].second] = i; } if(is_sorted(vec)) { cout << "0\n"; return; } vector<vector<int>> cycles = get_cycles(vec); vector<int> left_res; vector<int> right_res; for(auto const & cycle : cycles) { for(int i = 0; i < cycle.size() / 2; i++) { swap(vec[cycle[i]], vec[cycle[cycle.size()-1-i]]); left_res.push_back(cycle[i]); right_res.push_back(cycle[cycle.size()-1-i]); } } if(is_sorted(vec)) { cout << "1\n"; print_part_res(left_res, right_res); return; } else { cout << "2\n"; print_part_res(left_res, right_res); } cycles = get_cycles(vec); left_res.clear(); right_res.clear(); for(auto const & cycle : cycles) { for(int i = 0; i < cycle.size() / 2; i++) { swap(vec[cycle[i]], vec[cycle[cycle.size()-1-i]]); left_res.push_back(cycle[i]); right_res.push_back(cycle[cycle.size()-1-i]); } } print_part_res(left_res, right_res); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; vector<vector<int>> get_cycles(vector<int> const & vec) { vector<vector<int>> result; vector<bool> visited(vec.size(), false); for(int i = 0; i < vec.size(); i++) { if(!visited[i]) { vector<int> cur_res; int cur_ind = i; while(!visited[cur_ind]) { cur_res.push_back(cur_ind); visited[cur_ind] = true; cur_ind = vec[cur_ind]; } result.push_back(cur_res); } } return result; } bool is_sorted(vector<int> const & vec) { for(int i = 1; i < vec.size(); i++) { if(vec[i] < vec[i-1]) return false; } return true; } void print_part_res(vector<int> const & left_res, vector<int> const & right_res) { cout << left_res.size() + right_res.size() << "\n"; for(int i = 0; i < left_res.size(); i++) { cout << left_res[i] + 1 << " "; } for(int i = right_res.size() - 1; i >= 0; i--) { cout << right_res[i] + 1 << " "; } cout << "\n"; } void solve() { int n; cin >> n; vector<pair<int,int>> pre_vec(n); for(int i = 0; i < n; i++) { int a; cin >> a; pre_vec[i] = {a, i}; } sort(pre_vec.begin(), pre_vec.end()); vector<int> vec(n); for(int i = 0; i < n; i++) { vec[pre_vec[i].second] = i; } if(is_sorted(vec)) { cout << "0\n"; return; } vector<vector<int>> cycles = get_cycles(vec); vector<int> left_res; vector<int> right_res; for(auto const & cycle : cycles) { for(int i = 0; i < cycle.size() / 2; i++) { swap(vec[cycle[i]], vec[cycle[cycle.size()-1-i]]); left_res.push_back(cycle[i]); right_res.push_back(cycle[cycle.size()-1-i]); } } if(is_sorted(vec)) { cout << "1\n"; print_part_res(left_res, right_res); return; } else { cout << "2\n"; print_part_res(left_res, right_res); } cycles = get_cycles(vec); left_res.clear(); right_res.clear(); for(auto const & cycle : cycles) { for(int i = 0; i < cycle.size() / 2; i++) { swap(vec[cycle[i]], vec[cycle[cycle.size()-1-i]]); left_res.push_back(cycle[i]); right_res.push_back(cycle[cycle.size()-1-i]); } } print_part_res(left_res, right_res); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |