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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


void prepHeights(vector<pair<int, int>>& nums, vector<int>& heights) {
    int n = nums.size();
    sort(nums.begin(), nums.end());
    for(int i = 0; i < n; ++i) {
        heights[nums[i].second] = i;
    }
}

void solve(vector<int>& heights) {
    int n = heights.size();
    vector<vector<pair<int, int>>> result;
    while(true) {
        vector<bool> marked(n);
        vector<pair<int, int>> nums;
        for(int i = 0; i < n; ++i) {
            if(heights[i] != i && !marked[i] && !marked[heights[i]]) {
                marked[i] = marked[heights[i]] = true;
                nums.push_back({heights[i], i});
            }
        }
        if(nums.empty()) {
            break;
        }
        result.push_back(nums);
        for(auto p : nums) {
            swap(heights[p.first], heights[p.second]);
        }
    }
    cout << result.size() << "\n";
    for(auto nums : result) {
        cout << nums.size() * 2 << "\n";
        for(auto p : nums) {
            cout << p.first + 1 << " ";
        }
        for(int i = nums.size() - 1; i >= 0; --i) {
            cout << nums[i].second + 1 << " ";
        }
        cout << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector<pair<int, int>> nums(n);
    vector<int> heights(n);
    for(int i = 0; i < n; ++i) {
        cin >> nums[i].first;
        nums[i].second = i;
    }
    prepHeights(nums, heights);
    solve(heights);
}