#include <bits/stdc++.h> #define MAX_N 3007 using namespace std; pair<int, int> h[MAX_N]; int hp[MAX_N]; vector<int> v[MAX_N][2]; bool uzyte[MAX_N]; bool good[MAX_N]; int WB(int x, int n){ int p = 0, k = n, sr; while(p < k){ sr = (p+k)/2; if(hp[sr] == x) return sr; else if(hp[sr] < x) p = sr+1; else k = sr-1; } return p; } bool sorted(int n){ bool ok = 1; for(int i = 0; i < n; i++){ if(h[i].second == i) good[i] = 1; else ok = 0; } if(!ok) return 0; return 1; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for(int i = 0; i < n; i++){ cin >> h[i].first; hp[i] = h[i].first; } sort(hp, hp+n); for(int i = 0; i < n; i++){ h[i].second = WB(h[i].first, n); if(h[i].second == i) good[i] = 1; } int teraz = 0; while(!sorted(n)){ for(int i = 0; i < n; i++){ if(!good[i] && !good[h[i].second] && !uzyte[i] && !uzyte[h[i].second] && i != h[i].second){ v[teraz][0].push_back(i); v[teraz][1].push_back(h[i].second); uzyte[i] = 1; uzyte[h[i].second] = 1; good[h[i].second] = 1; if(i == h[h[i].second].second) good[i] = 1; swap(h[i], h[h[i].second]); if(i == h[i].second) good[i] = 1; } } teraz++; for(int i = 0; i < n; i++) uzyte[i] = 0; } cout << teraz << '\n'; int c; for(int i = 0; i < teraz; i++){ c = (int) v[i][0].size(); cout << c*2 << '\n'; for(int j = 0; j < c; j++) cout << v[i][0][j]+1 << " "; for(int j = c-1; j >= 0; j--) cout << v[i][1][j]+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 | #include <bits/stdc++.h> #define MAX_N 3007 using namespace std; pair<int, int> h[MAX_N]; int hp[MAX_N]; vector<int> v[MAX_N][2]; bool uzyte[MAX_N]; bool good[MAX_N]; int WB(int x, int n){ int p = 0, k = n, sr; while(p < k){ sr = (p+k)/2; if(hp[sr] == x) return sr; else if(hp[sr] < x) p = sr+1; else k = sr-1; } return p; } bool sorted(int n){ bool ok = 1; for(int i = 0; i < n; i++){ if(h[i].second == i) good[i] = 1; else ok = 0; } if(!ok) return 0; return 1; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for(int i = 0; i < n; i++){ cin >> h[i].first; hp[i] = h[i].first; } sort(hp, hp+n); for(int i = 0; i < n; i++){ h[i].second = WB(h[i].first, n); if(h[i].second == i) good[i] = 1; } int teraz = 0; while(!sorted(n)){ for(int i = 0; i < n; i++){ if(!good[i] && !good[h[i].second] && !uzyte[i] && !uzyte[h[i].second] && i != h[i].second){ v[teraz][0].push_back(i); v[teraz][1].push_back(h[i].second); uzyte[i] = 1; uzyte[h[i].second] = 1; good[h[i].second] = 1; if(i == h[h[i].second].second) good[i] = 1; swap(h[i], h[h[i].second]); if(i == h[i].second) good[i] = 1; } } teraz++; for(int i = 0; i < n; i++) uzyte[i] = 0; } cout << teraz << '\n'; int c; for(int i = 0; i < teraz; i++){ c = (int) v[i][0].size(); cout << c*2 << '\n'; for(int j = 0; j < c; j++) cout << v[i][0][j]+1 << " "; for(int j = c-1; j >= 0; j--) cout << v[i][1][j]+1 << " "; cout << '\n'; } return 0; } |