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
// C3-FOT.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>

using namespace std;
int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n;
    cin >> n;

    unordered_map<int, int> students; //initial position, next
    vector<pair<int, int>> sortedStudents; //height, initital position
    int temp;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        students.insert(make_pair(i,-1));
        sortedStudents.push_back(make_pair(temp, i));
    }

    sort(sortedStudents.begin(), sortedStudents.end());

    for (int i = 0; i < n; i++) {
        auto student = students.find(sortedStudents[i].second);
        student->second = i;
    }

    vector<bool> visited(n, false);
    int next;
    int howMany;
    unordered_map<int, int> cycles; //representative, count of members
    for (int i = 0; i < n; i++) {
        next = i;
        howMany = 0;
        while (!visited[next]) {
            howMany++;
            visited[next] = true;
            next = students.find(next)->second;
        }
        if (howMany != 0) {
            cycles.insert(make_pair(next, howMany));
        }
    }

    int numberOfCycles = cycles.size();

    vector<vector<int>> rounds;
    int rep, count;
    while (!cycles.empty()) {
        vector<int> prefix, sufix;
        unordered_map<int, int> cyclesCopy(cycles);
        for (auto iter = cyclesCopy.begin(); iter != cyclesCopy.end(); iter++) {
            rep = iter->first;
            count = iter->second;
            next = rep;

            if (count == 1) {
                cycles.erase(next);
            }
            else if (count == 2) {
                prefix.push_back(next+1);
                sufix.push_back(students.find(next)->second+1);
                cycles.erase(next);
            }
            else {
                int numberOfDeleted = count / 2;
                for (int i = 0; i < numberOfDeleted; i++) {
                    prefix.push_back(next + 1);
                    sufix.push_back(students.find(next)->second + 1);
                    int tempp = students.find(students.find(next)->second)->second;
                    students.find(students.find(next)->second)->second = -1;
                    students.find(next)->second = tempp;
                    /*next = students.find(next)->second;*/
                    next = students.find(next)->second;
                }
                count = count - numberOfDeleted;
                cycles.erase(rep);
                cycles.insert(make_pair(next, count));
            }
        }
        reverse(sufix.begin(), sufix.end());
        prefix.insert(prefix.end(), sufix.begin(), sufix.end());
        rounds.push_back(prefix);
    }
    cout << rounds.size() << "\n";
    for (int i = 0; i < rounds.size(); i++) {
        vector<int> current = rounds[i];
        cout << current.size() << "\n";
        for (int j = 0; j < current.size(); j++) {
            cout << current[j] << " ";
        }
        cout << "\n";
    }


}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file