#include <bits/stdc++.h> using namespace std; int n , l , w2 , x; int tab[3005]; int tab2[3005]; int num[3005]; bool odw[3005]; vector<int> w[3005]; void czysc() { for(int i = 1 ; i <= 3000 ; i ++) odw[i] = 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1 ; i <= n ; i ++) { cin >> tab[i]; tab2[i] = tab[i]; num[tab[i]] = i; } sort(tab2 + 1 , tab2 + n + 1); while(1) { l = 0; for(int i = n ; i >= 1 ; i --) { if(tab[i] != tab2[i]) { if(odw[i] == 0 && odw[num[tab2[i]]] == 0) { //cout << i << " " << num[tab2[i]] << "\n"; x = num[tab2[i]]; odw[i] = 1; odw[x] = 1; w[w2].push_back(i); w[w2].push_back(x); swap(num[tab[i]] , num[tab[x]]); swap(tab[i] , tab[x]); } } if(tab[i] == tab2[i]) l ++; } czysc(); if(l == n) break; w2 ++; } if(w[0].size() != 0) cout << w2 + 1 << "\n"; for(int i = 0 ; i <= w2 ; i ++) { cout << w[i].size() << "\n"; for(int j = 0 ; j < w[i].size() ; j +=2) cout << w[i][j] << " "; for(int j = w[i].size() - 1 ; j >= 1 ; j -=2) cout << w[i][j] << " "; 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 | #include <bits/stdc++.h> using namespace std; int n , l , w2 , x; int tab[3005]; int tab2[3005]; int num[3005]; bool odw[3005]; vector<int> w[3005]; void czysc() { for(int i = 1 ; i <= 3000 ; i ++) odw[i] = 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1 ; i <= n ; i ++) { cin >> tab[i]; tab2[i] = tab[i]; num[tab[i]] = i; } sort(tab2 + 1 , tab2 + n + 1); while(1) { l = 0; for(int i = n ; i >= 1 ; i --) { if(tab[i] != tab2[i]) { if(odw[i] == 0 && odw[num[tab2[i]]] == 0) { //cout << i << " " << num[tab2[i]] << "\n"; x = num[tab2[i]]; odw[i] = 1; odw[x] = 1; w[w2].push_back(i); w[w2].push_back(x); swap(num[tab[i]] , num[tab[x]]); swap(tab[i] , tab[x]); } } if(tab[i] == tab2[i]) l ++; } czysc(); if(l == n) break; w2 ++; } if(w[0].size() != 0) cout << w2 + 1 << "\n"; for(int i = 0 ; i <= w2 ; i ++) { cout << w[i].size() << "\n"; for(int j = 0 ; j < w[i].size() ; j +=2) cout << w[i][j] << " "; for(int j = w[i].size() - 1 ; j >= 1 ; j -=2) cout << w[i][j] << " "; cout << "\n"; } return 0; } |