#include<bits/stdc++.h> #include <unistd.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 gen(getpid()); int n; const int MN = 3009; int tab[MN]; int sorted[MN]; int pier[MN]; int where[MN]; map<int,int> conv; bool justgood =1; void prepare(){ cin >> n; for(int i = 1; i <= n; i++){ cin >> tab[i]; sorted[i] = tab[i]; } sort(sorted + 1, sorted + n + 1); for(int i = 1; i <= n; i++) conv[sorted[i]] = i; for(int i = 1; i <= n ; i++) pier[i] = conv[tab[i]]; for(int i = 1; i <= n; i++) where[pier[i]] = i; /*for(int i =1 ;i <=n ;i++) cout << pier[i] << ' ' ; cout << "\n"; */ for(int i = 1; i < n; i++){ if(tab[i] > tab[i+1]) justgood = 0; } } bitset <MN> visited; pii put[MN]; bool checkifswapable(){ bool good =1 ; for(int i = 1; i <= n; i++){ if(pier[i] == i){ continue; } if(pier[pier[i]] != i) good = 0; } return good; } void solve_swapable(){ int ind = 0; visited.reset(); for(int i = 1; i <= n; i++){ if(pier[i] == i){ visited[i] = 1; continue; } else { if(pier[pier[i]] == i && !visited[pier[i]] && !visited[i]){ visited[i] = 1; visited[pier[i]] = 1; put[ind++] = {i, pier[i]}; } } } cout << 2 * ind << "\n"; for(int i = 0; i < ind; i++) cout << put[i].st << ' '; for(int i = ind -1; i > -1; i--) cout << put[i].nd << ' '; } void repair(){ int ind = 0; visited.reset(); int todo = n; for(int i = 1; i <= n; i++){ if(pier[i] == i){ todo--; visited[i] = 1; continue; } if(pier[pier[i]] == i && !visited[pier[i]] && !visited[i]){ todo -= 2; visited[pier[i]] =1 ; visited[i] = 1; } } int preffree = 1; int pocz = -1, value; for(int i = 1; i <=n; i++){ if(!visited[i]){ pocz = i; value = pier[i]; break; } } int doppoz, buf; //for(int i =1; i <=n ;i++) //cout << visited[i] << ' '; //cout << "\n"; bool done; int rund = 0; int zost = n - visited.count(); while( preffree < n){ //cout << "START: " << pocz << ' ' << value << "\n"; visited[pocz] = 1; doppoz = where[pocz]; rund++; zost--; done = 0; if(value != pocz){ // nie zamieniamy dobrego jedynaka if(pier[value] != pocz && !visited[value] && !visited[where[pocz]]){ //gdy dobra para od razu put[ind++] = {where[pocz], value}; zost--; //cout <<"\nrow " << where[pocz] << ' ' << value << "\n"; visited[value] =1; visited[where[pocz]] = 1; todo -= 1; if(pier[value] != where[pocz] ){ buf = pocz; pocz = where[buf]; //cout << "WARFIN " << value << ' ' << pier[value] << "\n"; value = pier[value]; done =1 ; } } } if(!done){ for(int i = preffree; i <= n; i++){ if(visited[i]) preffree++; else{ pocz = i; value = pier[i]; break; } } preffree++; } } cout << 2 * ind << "\n"; for(int i = 0; i < ind; i++) cout << put[i].st << ' '; for(int i = ind -1 ; i > - 1; i--) cout << put[i].nd << ' '; for(int i =0; i < ind; i++){ swap(pier[put[i].st], pier[put[i].nd]); } // tutaj musi byc najmadrzejsza petla na swiecie , ktora jest efektywnie rozwiazaniem calego zadania } void run(){ if(justgood){ cout << 0; return; } if(!checkifswapable()){ cout << 2 << "\n"; repair(); cout << "\n"; solve_swapable(); } else { cout << 1 << "\n"; solve_swapable(); } } int main(){ prepare(); run() ; }
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<bits/stdc++.h> #include <unistd.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 gen(getpid()); int n; const int MN = 3009; int tab[MN]; int sorted[MN]; int pier[MN]; int where[MN]; map<int,int> conv; bool justgood =1; void prepare(){ cin >> n; for(int i = 1; i <= n; i++){ cin >> tab[i]; sorted[i] = tab[i]; } sort(sorted + 1, sorted + n + 1); for(int i = 1; i <= n; i++) conv[sorted[i]] = i; for(int i = 1; i <= n ; i++) pier[i] = conv[tab[i]]; for(int i = 1; i <= n; i++) where[pier[i]] = i; /*for(int i =1 ;i <=n ;i++) cout << pier[i] << ' ' ; cout << "\n"; */ for(int i = 1; i < n; i++){ if(tab[i] > tab[i+1]) justgood = 0; } } bitset <MN> visited; pii put[MN]; bool checkifswapable(){ bool good =1 ; for(int i = 1; i <= n; i++){ if(pier[i] == i){ continue; } if(pier[pier[i]] != i) good = 0; } return good; } void solve_swapable(){ int ind = 0; visited.reset(); for(int i = 1; i <= n; i++){ if(pier[i] == i){ visited[i] = 1; continue; } else { if(pier[pier[i]] == i && !visited[pier[i]] && !visited[i]){ visited[i] = 1; visited[pier[i]] = 1; put[ind++] = {i, pier[i]}; } } } cout << 2 * ind << "\n"; for(int i = 0; i < ind; i++) cout << put[i].st << ' '; for(int i = ind -1; i > -1; i--) cout << put[i].nd << ' '; } void repair(){ int ind = 0; visited.reset(); int todo = n; for(int i = 1; i <= n; i++){ if(pier[i] == i){ todo--; visited[i] = 1; continue; } if(pier[pier[i]] == i && !visited[pier[i]] && !visited[i]){ todo -= 2; visited[pier[i]] =1 ; visited[i] = 1; } } int preffree = 1; int pocz = -1, value; for(int i = 1; i <=n; i++){ if(!visited[i]){ pocz = i; value = pier[i]; break; } } int doppoz, buf; //for(int i =1; i <=n ;i++) //cout << visited[i] << ' '; //cout << "\n"; bool done; int rund = 0; int zost = n - visited.count(); while( preffree < n){ //cout << "START: " << pocz << ' ' << value << "\n"; visited[pocz] = 1; doppoz = where[pocz]; rund++; zost--; done = 0; if(value != pocz){ // nie zamieniamy dobrego jedynaka if(pier[value] != pocz && !visited[value] && !visited[where[pocz]]){ //gdy dobra para od razu put[ind++] = {where[pocz], value}; zost--; //cout <<"\nrow " << where[pocz] << ' ' << value << "\n"; visited[value] =1; visited[where[pocz]] = 1; todo -= 1; if(pier[value] != where[pocz] ){ buf = pocz; pocz = where[buf]; //cout << "WARFIN " << value << ' ' << pier[value] << "\n"; value = pier[value]; done =1 ; } } } if(!done){ for(int i = preffree; i <= n; i++){ if(visited[i]) preffree++; else{ pocz = i; value = pier[i]; break; } } preffree++; } } cout << 2 * ind << "\n"; for(int i = 0; i < ind; i++) cout << put[i].st << ' '; for(int i = ind -1 ; i > - 1; i--) cout << put[i].nd << ' '; for(int i =0; i < ind; i++){ swap(pier[put[i].st], pier[put[i].nd]); } // tutaj musi byc najmadrzejsza petla na swiecie , ktora jest efektywnie rozwiazaniem calego zadania } void run(){ if(justgood){ cout << 0; return; } if(!checkifswapable()){ cout << 2 << "\n"; repair(); cout << "\n"; solve_swapable(); } else { cout << 1 << "\n"; solve_swapable(); } } int main(){ prepare(); run() ; } |