#include <iostream> #include <algorithm> using namespace std; const int MAX_N = 3009; pair<int, int> tab[MAX_N]; //int scaled_tab[MAX_N]; vector <int> scaled_tab; int switch_table[MAX_N]; bool this_was_switched[MAX_N]; int n; bool visited[MAX_N]; pair<int, int> output[MAX_N]; int output_iterator = 0; pair<int, int> output2[MAX_N]; int output2_iterator = 0; void calc_swaps(int id){ int cur_id = id; int cur_val = scaled_tab[cur_id]; int paired_id = distance(scaled_tab.begin() , find(scaled_tab.begin(), scaled_tab.end(), cur_id)); this_was_switched[cur_id] = true; this_was_switched[cur_val] = true; if(scaled_tab[scaled_tab[paired_id]] == paired_id){ return; } if(scaled_tab[paired_id] == paired_id) return; scaled_tab[paired_id] = scaled_tab[cur_val]; scaled_tab[cur_val] = cur_id; output[output_iterator] = {paired_id, cur_val}; output_iterator++; if(scaled_tab[paired_id] == paired_id){ this_was_switched[paired_id] = true; } else if(scaled_tab[scaled_tab[paired_id]] == paired_id){ this_was_switched[paired_id] = true; this_was_switched[scaled_tab[paired_id]] = true; } else{ calc_swaps(paired_id); } } void switch_them(int a, int b){ int buf; buf = scaled_tab[a]; scaled_tab[a] = scaled_tab[b]; scaled_tab[b] = buf; } void dbg(){ cout<<"\n"; cout<<"DEBUG DEBUG DEBUG DEBUG\n"; for(int i = 0; i <n; i++){ cout<<scaled_tab[i]<<" "; } cout<<"\nDEBUG DEBUG DEBUG DEBUG\n"; } int main() { //int n; cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin>>n; scaled_tab.resize(n); int a; for(int i = 0; i < n; i++){ cin>>a; tab[i] = {a, i}; } sort(tab, tab+n); for(int i = 0; i < n; i++){ scaled_tab[tab[i].second] = i; } bool single_round = true; for(int i = 0; i < n; i++){ if(scaled_tab[i] == i); else if(scaled_tab[scaled_tab[i]] == i); else{ single_round = false; break; } } for(int i = 0; i <= n; i++){ if(i == n){ cout<<0; return 0; } if(scaled_tab[i] != i){ break; } } if(single_round){ cout<<1<<"\n"; for(int i = 0; i < n; i++){ if(scaled_tab[i] != i and visited[i] == false){ output2[output2_iterator] = {i, scaled_tab[i]}; visited[scaled_tab[i]] = true; output2_iterator++; } } //output2_iterator/=2; cout<<output2_iterator * 2<<"\n"; for(int i = 0; i < output2_iterator; i++){ cout<<output2[i].first + 1<<" "; ///switch_them(output2[i].first, output2[i].second); } for(int i = output2_iterator - 1; i >= 0; i--){ cout<<output2[i].second + 1<<" "; } ///dbg(); } else{ cout<<2<<"\n"; int correct_count = 0; int id_zamiany; for(int i = 0; i < n; i++){ if(scaled_tab[i] == i) correct_count++; } if(((n - correct_count)%2) == 5){ } else{ for(int i = 0; i < n; i++){ if(this_was_switched[i] == false){ calc_swaps(i); } } cout<<output_iterator * 2<<"\n"; for(int i = 0; i < output_iterator; i++){ cout<<output[i].first + 1<<" "; //switch_them(output[i].first, output[i].second); } for(int i = output_iterator - 1; i >= 0; i--){ cout<<output[i].second + 1<<" "; } //dbg(); } cout<<"\n"; for(int i = 0; i < n; i++){ if(scaled_tab[i] != i and visited[i] == false){ output2[output2_iterator] = {i, scaled_tab[i]}; visited[scaled_tab[i]] = true; output2_iterator++; } } //output2_iterator/=2; cout<<output2_iterator * 2<<"\n"; for(int i = 0; i < output2_iterator; i++){ cout<<output2[i].first + 1<<" "; ///switch_them(output2[i].first, output2[i].second); } for(int i = output2_iterator - 1; i >= 0; i--){ cout<<output2[i].second + 1<<" "; } ///dbg(); } 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include <iostream> #include <algorithm> using namespace std; const int MAX_N = 3009; pair<int, int> tab[MAX_N]; //int scaled_tab[MAX_N]; vector <int> scaled_tab; int switch_table[MAX_N]; bool this_was_switched[MAX_N]; int n; bool visited[MAX_N]; pair<int, int> output[MAX_N]; int output_iterator = 0; pair<int, int> output2[MAX_N]; int output2_iterator = 0; void calc_swaps(int id){ int cur_id = id; int cur_val = scaled_tab[cur_id]; int paired_id = distance(scaled_tab.begin() , find(scaled_tab.begin(), scaled_tab.end(), cur_id)); this_was_switched[cur_id] = true; this_was_switched[cur_val] = true; if(scaled_tab[scaled_tab[paired_id]] == paired_id){ return; } if(scaled_tab[paired_id] == paired_id) return; scaled_tab[paired_id] = scaled_tab[cur_val]; scaled_tab[cur_val] = cur_id; output[output_iterator] = {paired_id, cur_val}; output_iterator++; if(scaled_tab[paired_id] == paired_id){ this_was_switched[paired_id] = true; } else if(scaled_tab[scaled_tab[paired_id]] == paired_id){ this_was_switched[paired_id] = true; this_was_switched[scaled_tab[paired_id]] = true; } else{ calc_swaps(paired_id); } } void switch_them(int a, int b){ int buf; buf = scaled_tab[a]; scaled_tab[a] = scaled_tab[b]; scaled_tab[b] = buf; } void dbg(){ cout<<"\n"; cout<<"DEBUG DEBUG DEBUG DEBUG\n"; for(int i = 0; i <n; i++){ cout<<scaled_tab[i]<<" "; } cout<<"\nDEBUG DEBUG DEBUG DEBUG\n"; } int main() { //int n; cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin>>n; scaled_tab.resize(n); int a; for(int i = 0; i < n; i++){ cin>>a; tab[i] = {a, i}; } sort(tab, tab+n); for(int i = 0; i < n; i++){ scaled_tab[tab[i].second] = i; } bool single_round = true; for(int i = 0; i < n; i++){ if(scaled_tab[i] == i); else if(scaled_tab[scaled_tab[i]] == i); else{ single_round = false; break; } } for(int i = 0; i <= n; i++){ if(i == n){ cout<<0; return 0; } if(scaled_tab[i] != i){ break; } } if(single_round){ cout<<1<<"\n"; for(int i = 0; i < n; i++){ if(scaled_tab[i] != i and visited[i] == false){ output2[output2_iterator] = {i, scaled_tab[i]}; visited[scaled_tab[i]] = true; output2_iterator++; } } //output2_iterator/=2; cout<<output2_iterator * 2<<"\n"; for(int i = 0; i < output2_iterator; i++){ cout<<output2[i].first + 1<<" "; ///switch_them(output2[i].first, output2[i].second); } for(int i = output2_iterator - 1; i >= 0; i--){ cout<<output2[i].second + 1<<" "; } ///dbg(); } else{ cout<<2<<"\n"; int correct_count = 0; int id_zamiany; for(int i = 0; i < n; i++){ if(scaled_tab[i] == i) correct_count++; } if(((n - correct_count)%2) == 5){ } else{ for(int i = 0; i < n; i++){ if(this_was_switched[i] == false){ calc_swaps(i); } } cout<<output_iterator * 2<<"\n"; for(int i = 0; i < output_iterator; i++){ cout<<output[i].first + 1<<" "; //switch_them(output[i].first, output[i].second); } for(int i = output_iterator - 1; i >= 0; i--){ cout<<output[i].second + 1<<" "; } //dbg(); } cout<<"\n"; for(int i = 0; i < n; i++){ if(scaled_tab[i] != i and visited[i] == false){ output2[output2_iterator] = {i, scaled_tab[i]}; visited[scaled_tab[i]] = true; output2_iterator++; } } //output2_iterator/=2; cout<<output2_iterator * 2<<"\n"; for(int i = 0; i < output2_iterator; i++){ cout<<output2[i].first + 1<<" "; ///switch_them(output2[i].first, output2[i].second); } for(int i = output2_iterator - 1; i >= 0; i--){ cout<<output2[i].second + 1<<" "; } ///dbg(); } return 0; } |