#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, a; vector<pair<int, int> > v; cin >> n; for (int i=0; i<n; i++) { cin >> a; v.push_back(make_pair(a,i)); } sort(v.begin(),v.end()); int t[n]; int used[n]; for (int i=0; i<n; i++) { t[v[i].second] = i; used[i] = false; } int longest = 0; vector<vector<int>> cycles; for (int i=0; i<n; i++) { if (!used[i]) { used[i] = true; if (t[i] == i) continue; int current = i; vector<int> cycle; cycle.push_back(i); current = t[i]; while (current != i) { used[current] = true; cycle.push_back(current); current = t[current]; } cycles.push_back(cycle); if (cycle.size() > longest) longest = cycle.size(); } } if (longest == 0) cout << 0 << endl; else cout << longest-1 << endl; int howManyLeft = 0; for (int i=0; i<longest-1; i++) { for (int j=0; j<n; j++) used[j] = false; cycles.clear(); for (int ii=0; ii<n; ii++) { if (!used[ii]) { used[ii] = true; if (t[ii] == ii) continue; int current = ii; vector<int> cycle; cycle.push_back(ii); current = t[ii]; while (current != ii) { used[current] = true; cycle.push_back(current); current = t[current]; } cycles.push_back(cycle); } } cout << cycles.size()*2 << endl; for (int j=0; j<cycles.size(); j++) cout << cycles[j][0]+1 << " "; for (int j=cycles.size()-1; j>=0; j--) cout << cycles[j][1]+1 << " "; for (int j=0; j<cycles.size(); j++) { int temp = t[cycles[j][0]]; t[cycles[j][0]] = t[cycles[j][1]]; t[cycles[j][1]] = temp; } cout << endl; } }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, a; vector<pair<int, int> > v; cin >> n; for (int i=0; i<n; i++) { cin >> a; v.push_back(make_pair(a,i)); } sort(v.begin(),v.end()); int t[n]; int used[n]; for (int i=0; i<n; i++) { t[v[i].second] = i; used[i] = false; } int longest = 0; vector<vector<int>> cycles; for (int i=0; i<n; i++) { if (!used[i]) { used[i] = true; if (t[i] == i) continue; int current = i; vector<int> cycle; cycle.push_back(i); current = t[i]; while (current != i) { used[current] = true; cycle.push_back(current); current = t[current]; } cycles.push_back(cycle); if (cycle.size() > longest) longest = cycle.size(); } } if (longest == 0) cout << 0 << endl; else cout << longest-1 << endl; int howManyLeft = 0; for (int i=0; i<longest-1; i++) { for (int j=0; j<n; j++) used[j] = false; cycles.clear(); for (int ii=0; ii<n; ii++) { if (!used[ii]) { used[ii] = true; if (t[ii] == ii) continue; int current = ii; vector<int> cycle; cycle.push_back(ii); current = t[ii]; while (current != ii) { used[current] = true; cycle.push_back(current); current = t[current]; } cycles.push_back(cycle); } } cout << cycles.size()*2 << endl; for (int j=0; j<cycles.size(); j++) cout << cycles[j][0]+1 << " "; for (int j=cycles.size()-1; j>=0; j--) cout << cycles[j][1]+1 << " "; for (int j=0; j<cycles.size(); j++) { int temp = t[cycles[j][0]]; t[cycles[j][0]] = t[cycles[j][1]]; t[cycles[j][1]] = temp; } cout << endl; } } |