bingxuan9112

cyc

Dec 22nd, 2020 (edited)
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. signed main() {
  6.     ios_base::sync_with_stdio(0), cin.tie(0);
  7.     int n;
  8.     cin >> n;
  9.     vector<int> v(n);
  10.     for(int &x: v) cin >> x, --x;
  11.     vector<vector<int>> ans;
  12.     int cnt = 0;
  13.     while(!is_sorted(v.begin(), v.end())) {
  14.         ans.emplace_back();
  15.         auto &A = ans.back();
  16.         vector<bool> vis(n);
  17.         for(int i = 0; i < n; i++) {
  18.             if(!vis[i] && v[i] != i) {
  19.                 for(int x = i; !vis[x]; x = v[x]) {
  20.                     A.push_back(x);
  21.                     vis[x] = true;
  22.                 }
  23.             }
  24.         }
  25.         int last = v[A.back()];
  26.         for(int pos: A)
  27.             swap(last, v[pos]);
  28.         /* for(int i = 0; i < n; i++) */
  29.         /*     cerr << v[i]+1 << (i+1==n ? '\n' : ' '); */
  30.         reverse(A.begin(), A.end());
  31.         assert(++cnt <= 2);
  32.     }
  33.     cout << ans.size() << '\n';
  34.     for(const auto &A: ans) {
  35.         cout << A.size();
  36.         for(int x: A) cout << ' ' << x+1;
  37.         cout << '\n';
  38.     }
  39. }
  40.  
Add Comment
Please, Sign In to add comment