#include <bits/stdc++.h> using namespace std; int n; int t[3001]; int p[3001]; vector < pair<int, int> > v; vector <vector <int>> wynik; vector <int> w1, w2; bitset <3001> b; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; v.push_back({t[i], i}); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { p[v[i].first] = i + 1; } //for (int i = 1; i <= n; i++) cout << p[t[i]] << endl; while (true) { for (int i = 1; i <= n; i++) { //cout << t[i] << " "; if (p[t[i]] == i) continue; if (b[i] || b[p[t[i]]]) continue; else { b[i] = 1; b[p[t[i]]] = 1; w1.push_back(i); w2.push_back(p[t[i]]); swap(t[i], t[p[t[i]]]); } } //cout << endl << endl; if (w1.empty()) break; reverse(w2.begin(), w2.end()); for (auto i : w2) w1.push_back(i); wynik.push_back(w1); //for (auto i : w1) cout << i << " "; //cout << endl; w1.clear(); w2.clear(); b.reset(); } cout << wynik.size() << endl; for (auto v : wynik) { cout << v.size() << endl; for (auto i : v) cout << i << " "; 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 | #include <bits/stdc++.h> using namespace std; int n; int t[3001]; int p[3001]; vector < pair<int, int> > v; vector <vector <int>> wynik; vector <int> w1, w2; bitset <3001> b; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; v.push_back({t[i], i}); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { p[v[i].first] = i + 1; } //for (int i = 1; i <= n; i++) cout << p[t[i]] << endl; while (true) { for (int i = 1; i <= n; i++) { //cout << t[i] << " "; if (p[t[i]] == i) continue; if (b[i] || b[p[t[i]]]) continue; else { b[i] = 1; b[p[t[i]]] = 1; w1.push_back(i); w2.push_back(p[t[i]]); swap(t[i], t[p[t[i]]]); } } //cout << endl << endl; if (w1.empty()) break; reverse(w2.begin(), w2.end()); for (auto i : w2) w1.push_back(i); wynik.push_back(w1); //for (auto i : w1) cout << i << " "; //cout << endl; w1.clear(); w2.clear(); b.reset(); } cout << wynik.size() << endl; for (auto v : wynik) { cout << v.size() << endl; for (auto i : v) cout << i << " "; cout << endl; } } |