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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <vector>

using namespace std;

void read(vector<int>& output)
{
    int n ;
    cin >> n;

    vector<int> height(3000,-1);
    output.resize(n);

    for (int i = 0; i < n; i++)
    {
        cin >> output[i];
        output[i]--;
        height[output[i]] = i;
    }

    int counter = 0;
    for (int i = 0; i < 3000; i++)
    {
        if(height[i] > -1)
        {
            output[height[i]] = counter;
            counter++;
        }
    }
}

int calc_cycles(vector<int>& series,vector<int>& rev_perm, vector<pair<int,int>>& cycles)
{
    int l_cycle = 0;
    vector<bool> used(series.size(), false);

    for (int i = 0; i < series.size(); i++)
    {
        if(!used[i])
        {
            used[i] = true;
            
            int l = 1;
            int el = series[i];
            
            while (el != i)
            {
                used[el] = true;
                l++;
                el = series[el];
            }

            cycles.push_back({i,l});

            if(l > l_cycle)
            {
                l_cycle = l;
            }
        }
    }
    
    return l_cycle;
}

void simplify_cycle(vector<int>& next, vector<int>& prev, int pos, int length,vector<pair<int,int>>& swaps)
{
    while(length > 2)
    {
        int p1 = prev[pos];
        int p2 = prev[p1];

        int n1 = next[pos];

        prev[n1] = p2;
        next[p2] = n1;

        next[pos] = p1;
        prev[p1] = pos;
        

        swaps.push_back({p2,pos});
        pos = n1;

        length-=2;
    }
}

void swaps_to_result(vector<pair<int,int>>& swaps,vector<int>& result)
{
    for (int i = 0; i < swaps.size(); i++)
    {
        result.push_back(swaps[i].first);
    }
    
    for (int i = swaps.size() -1 ; i >= 0; i--)
    {
        result.push_back(swaps[i].second);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    vector<int> series;
    read(series);

    vector<int> rev_perm(series.size());

    for (int i = 0; i < series.size(); i++)
    {
        rev_perm[series[i]] = i;
    }
    
    vector<pair<int,int>> cycles;
    int l_cycle = calc_cycles(series,rev_perm,cycles);

    vector<vector<int>> result;
    if(l_cycle > 2)
    {
        result.resize(2);
    }
    else if(l_cycle == 2)
    {
        result.resize(1);
    }  

    if(l_cycle > 2)
    {
        l_cycle = 2;
        vector<pair<int,int>> swaps;

        for (int i = 0; i < cycles.size(); i++)
        {
            if(cycles[i].second > 2)
            {
                simplify_cycle(series, rev_perm, cycles[i].first, cycles[i].second,swaps);
            }
        }
        
        swaps_to_result(swaps,result[0]);
    }

    if(l_cycle == 2)
    {
        vector<pair<int,int>> swaps;
        vector<bool> used(series.size(), false);

        for (int i = 0; i < series.size(); i++)
        {
            if(!used[i])
            {
                used[i] = true;
                if(i != series[i])
                {
                    used[series[i]] = true;
                    swaps.push_back({i, series[i]});
                }
            }
        }

        swaps_to_result(swaps, result.back());        
    }

    cout << result.size() << '\n';

    if(result.size() > 0)
    {
        for (int i = 0; i < result.size(); i++)
        {
            cout << result[i].size() << '\n';
            for (int j = 0; j < result[i].size(); j++)
            {
                cout << result[i][j]+1 << ' ';
            }
            cout << '\n';
        }
        
    }  

    return 0;
}