Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- signed main() {
- ios_base::sync_with_stdio(0), cin.tie(0);
- int n;
- cin >> n;
- vector<int> v(n);
- for(int &x: v) cin >> x, --x;
- vector<vector<int>> ans;
- int cnt = 0;
- while(!is_sorted(v.begin(), v.end())) {
- ans.emplace_back();
- auto &A = ans.back();
- vector<bool> vis(n);
- for(int i = 0; i < n; i++) {
- if(!vis[i] && v[i] != i) {
- for(int x = i; !vis[x]; x = v[x]) {
- A.push_back(x);
- vis[x] = true;
- }
- }
- }
- int last = v[A.back()];
- for(int pos: A)
- swap(last, v[pos]);
- /* for(int i = 0; i < n; i++) */
- /* cerr << v[i]+1 << (i+1==n ? '\n' : ' '); */
- reverse(A.begin(), A.end());
- assert(++cnt <= 2);
- }
- cout << ans.size() << '\n';
- for(const auto &A: ans) {
- cout << A.size();
- for(int x: A) cout << ' ' << x+1;
- cout << '\n';
- }
- }
Add Comment
Please, Sign In to add comment