#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; vector<int>org; vector<pair<int, int> > vec; cin >> n; for(int i = 0; i < n;i++) { int x; cin >> x; vec.push_back(make_pair(x, i + 1)); org.push_back(x); } sort(vec.begin(), vec.end()); vector<int> data; for(int i = 0; i < n; i++) data.push_back(vec[i].second); int r = 0; vector<int> res[3009]; int visited[3009]; while(1) { int need_swap = false; for(int i = 0; i <= n; i++) visited[i] = 0; for(int i = 0; i < n; i++) { if(visited[i]) continue; int a = data[i]; visited[i] = 1; if(data[i] == i + 1) continue; //int b = vec[a - 1]; if(visited[a - 1]) continue; visited[a - 1] = 1; res[r].push_back(i + 1); res[r].insert(res[r].begin(), a); need_swap = true; } for(int i = 0; i < res[r].size() / 2; i++) { swap(org[res[r][i] - 1], org[res[r][res[r].size() - i - 1] - 1]); } vec.clear(); data.clear(); for(int i = 0; i < n; i++) { vec.push_back(make_pair(org[i], i + 1)); } sort(vec.begin(), vec.end()); for(int i = 0; i < n; i++) data.push_back(vec[i].second); if(!need_swap) break; r++; } cout << r << endl; for(int i = 0; i < r; i++) { cout << res[i].size() << endl; for(int j = 0; j < res[i].size(); j++) { cout << res[i][j] << " "; } cout << endl; } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; vector<int>org; vector<pair<int, int> > vec; cin >> n; for(int i = 0; i < n;i++) { int x; cin >> x; vec.push_back(make_pair(x, i + 1)); org.push_back(x); } sort(vec.begin(), vec.end()); vector<int> data; for(int i = 0; i < n; i++) data.push_back(vec[i].second); int r = 0; vector<int> res[3009]; int visited[3009]; while(1) { int need_swap = false; for(int i = 0; i <= n; i++) visited[i] = 0; for(int i = 0; i < n; i++) { if(visited[i]) continue; int a = data[i]; visited[i] = 1; if(data[i] == i + 1) continue; //int b = vec[a - 1]; if(visited[a - 1]) continue; visited[a - 1] = 1; res[r].push_back(i + 1); res[r].insert(res[r].begin(), a); need_swap = true; } for(int i = 0; i < res[r].size() / 2; i++) { swap(org[res[r][i] - 1], org[res[r][res[r].size() - i - 1] - 1]); } vec.clear(); data.clear(); for(int i = 0; i < n; i++) { vec.push_back(make_pair(org[i], i + 1)); } sort(vec.begin(), vec.end()); for(int i = 0; i < n; i++) data.push_back(vec[i].second); if(!need_swap) break; r++; } cout << r << endl; for(int i = 0; i < r; i++) { cout << res[i].size() << endl; for(int j = 0; j < res[i].size(); j++) { cout << res[i][j] << " "; } cout << endl; } return 0; } |