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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;

int main()
{
    FAST int n;
    cin >> n;
    int tmp;

    map<int, int> to_sort;
    vector<int> numbers(n);
    for (int i = 0; i < n; i++)
    {
        cin >> tmp;
        to_sort[tmp] = i;
        numbers[i] = tmp;
    }

    map<int, int> should_be;
    vector<int> sorted(n);
    int i = 0;
    for (auto const &[key, val] : to_sort)
    {
        should_be[key] = i;
        sorted[i] = key;
        i++;
    }

    vector<vector<pair<int, int>>> resp;
    resp.reserve(n);

    while (true)
    {
        map<int, int> swap;
        unordered_map<int, bool> used_idx;
        for (auto const &[val, idx] : to_sort)
        {
            if (should_be[val] == idx)
            {
                continue;
            }
            if (used_idx[idx] || used_idx[should_be[val]])
            {
                continue;
            }
            swap[idx] = should_be[val];
            used_idx[idx] = true;
            used_idx[should_be[val]] = true;
        }

        if (swap.size() == 0)
        {
            // last iteration
            break;
        }

        vector<pair<int, int>> resp_row;
        resp_row.reserve(n / 2);
        for (auto const &[idx1, idx2] : swap)
        {
            resp_row.push_back(make_pair(idx1, idx2));

            // fix numbers in array to sort
            tmp = numbers[idx2];
            numbers[idx2] = numbers[idx1];
            to_sort[numbers[idx1]] = idx2;
            numbers[idx1] = tmp;
            to_sort[tmp] = idx1;
        }

        swap.clear();
        for (auto const &[val, idx1] : to_sort)
        {
            if (should_be[val] == idx1 || used_idx[idx1])
            {
                continue;
            }
            if (numbers[should_be[val]] == sorted[idx1])
            {
                continue;
            }
            // fixme
            int idx2 = should_be[numbers[should_be[val]]];
            if (used_idx[idx2])
            {
                continue;
            }
            used_idx[idx2] = true;
            used_idx[idx1] = true;
            swap[idx1] = idx2;
            resp_row.push_back(make_pair(idx2, idx1));
        }
        for (auto const &[idx1, idx2] : swap)
        {
            tmp = numbers[idx2];
            numbers[idx2] = numbers[idx1];
            to_sort[numbers[idx1]] = idx2;
            numbers[idx1] = tmp;
            to_sort[tmp] = idx1;
        }
        resp.push_back(resp_row);
        used_idx.clear();
    }

    cout << resp.size() << endl;
    for (int i = 0; i < resp.size(); i++)
    {
        cout << resp[i].size() * 2 << endl;
        for (int j = 0; j < resp[i].size(); j++)
        {
            cout << resp[i][j].second + 1 << " ";
        }

        for (int j = resp[i].size() - 1; j >= 0; j--)
        {
            cout << resp[i][j].first + 1 << " ";
        }
        cout << endl;
    }
}