#include <iostream> #include <utility> #include <algorithm> #include <deque> #include <vector> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> tab(n); for(int i = 0; i < n; i++) { cin >> tab[i].first; tab[i].second = i; } vector<vector<int>> wynik; while(!is_sorted(tab.begin(), tab.end())){ vector<pair<int, int>> c = tab; vector<bool> taken(n); deque<int> l, r; sort(c.begin(), c.end()); for(int i = 0; i < n; i++) { if (!taken[i] && !taken[c[i].second] && c[i].second != i) { l.push_back(c[i].second); r.push_front(i); taken[i] = true; taken[c[i].second] = true; swap(tab[i].first, tab[c[i].second].first); } } wynik.push_back(vector<int>(0)); while(!l.empty()) { wynik.back().push_back(l.front()); l.pop_front(); } while(!r.empty()) { wynik.back().push_back(r.front()); r.pop_front(); } } cout << wynik.size() << endl; for(auto v : wynik) { cout << v.size() << endl; for(auto i : v) { cout << i + 1 << " "; } 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 | #include <iostream> #include <utility> #include <algorithm> #include <deque> #include <vector> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> tab(n); for(int i = 0; i < n; i++) { cin >> tab[i].first; tab[i].second = i; } vector<vector<int>> wynik; while(!is_sorted(tab.begin(), tab.end())){ vector<pair<int, int>> c = tab; vector<bool> taken(n); deque<int> l, r; sort(c.begin(), c.end()); for(int i = 0; i < n; i++) { if (!taken[i] && !taken[c[i].second] && c[i].second != i) { l.push_back(c[i].second); r.push_front(i); taken[i] = true; taken[c[i].second] = true; swap(tab[i].first, tab[c[i].second].first); } } wynik.push_back(vector<int>(0)); while(!l.empty()) { wynik.back().push_back(l.front()); l.pop_front(); } while(!r.empty()) { wynik.back().push_back(r.front()); r.pop_front(); } } cout << wynik.size() << endl; for(auto v : wynik) { cout << v.size() << endl; for(auto i : v) { cout << i + 1 << " "; } cout << endl; } return 0; } |