#include<bits/stdc++.h> using namespace std; bool sorted(vector <int> v){ for (int i = 1; i < v.size(); i++){ if (v[i] <= v[i - 1]) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector <int> v(n + 1); for (int i = 1;i <= n; i++){ cin >> v[i]; } vector <int> cp = v, cp2 = v; sort(cp.begin(), cp.end()); for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (cp2[j] == cp[i]) v[j] = i; } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ vector <vector <int> > xd; while (!sorted(v)) { vector <int> lef, rig; bitset<3007> forbidden = 0; for (int i = 1; i < n; i++){ if (i == v[i] || forbidden[i] || forbidden[v[i]]) continue; lef.push_back(i); rig.push_back(v[i]); swap(v[i], v[v[i]]); forbidden[i] = true; forbidden[v[i]] = true; } reverse(rig.begin(), rig.end()); for (int i : rig) lef.push_back(i); xd.push_back(lef); } cout << xd.size() << "\n"; for (auto xdd : xd){ cout << xdd.size() << "\n"; for (auto xddd : xdd){ cout << xddd << " "; } 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 | #include<bits/stdc++.h> using namespace std; bool sorted(vector <int> v){ for (int i = 1; i < v.size(); i++){ if (v[i] <= v[i - 1]) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector <int> v(n + 1); for (int i = 1;i <= n; i++){ cin >> v[i]; } vector <int> cp = v, cp2 = v; sort(cp.begin(), cp.end()); for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (cp2[j] == cp[i]) v[j] = i; } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ vector <vector <int> > xd; while (!sorted(v)) { vector <int> lef, rig; bitset<3007> forbidden = 0; for (int i = 1; i < n; i++){ if (i == v[i] || forbidden[i] || forbidden[v[i]]) continue; lef.push_back(i); rig.push_back(v[i]); swap(v[i], v[v[i]]); forbidden[i] = true; forbidden[v[i]] = true; } reverse(rig.begin(), rig.end()); for (int i : rig) lef.push_back(i); xd.push_back(lef); } cout << xd.size() << "\n"; for (auto xdd : xd){ cout << xdd.size() << "\n"; for (auto xddd : xdd){ cout << xddd << " "; } cout << "\n"; } return 0; } |