#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include <set>
#include <cmath>
#include <bit>
#include <bitset>
#include <cstdint>
#include <string>
#include <map>
#include <cassert>
#include <functional>
#include <tuple>
#include <numeric>
#include <queue>
#include <list>
#include <cassert>
using namespace std;
struct Entity
{
bool processed;
int next, initialPosition;
Entity(int a, int b) : next(a), initialPosition(b) {}
};
int n;
vector<Entity> v;
vector<deque<int>> res;
bool somethingToSimplify()
{
for (int i = 0; i < v.size(); ++i)
if (i != v[i].next)
return true;
return false;
}
void process(int x, deque<int> &operations)
{
if (v[x].processed)
return;
v[x].processed = true;
if (x == v[x].next)
return;
int next = v[x].next;
if (!v[next].processed)
{
v[next].processed = true;
v[x].next = v[next].next;
v[next].next = next;
operations.push_front(x);
operations.push_back(next);
}
process(v[x].next, operations);
}
deque<int> simplify()
{
deque<int> operations;
for (int i = 0; i < v.size(); ++i)
v[i].processed = false;
for (int i = 0; i < v.size(); ++i)
process(i, operations);
return operations;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
int x;
scanf("%d", &x);
v.push_back(Entity(x, i));
}
sort(v.begin(), v.end(), [](Entity a, Entity b)
{ return a.next < b.next; });
for (int i = 0; i < v.size(); ++i)
{
v[i].next = i;
}
sort(v.begin(), v.end(), [](Entity a, Entity b)
{ return a.initialPosition < b.initialPosition; });
while (somethingToSimplify())
res.push_back(simplify());
printf("%lu\n", res.size());
for (auto &row : res)
{
printf("%lu\n", row.size());
for (auto &col : row)
printf("%d ", col + 1);
printf("\n");
}
return 0;
}
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 | #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector #include <set> #include <cmath> #include <bit> #include <bitset> #include <cstdint> #include <string> #include <map> #include <cassert> #include <functional> #include <tuple> #include <numeric> #include <queue> #include <list> #include <cassert> using namespace std; struct Entity { bool processed; int next, initialPosition; Entity(int a, int b) : next(a), initialPosition(b) {} }; int n; vector<Entity> v; vector<deque<int>> res; bool somethingToSimplify() { for (int i = 0; i < v.size(); ++i) if (i != v[i].next) return true; return false; } void process(int x, deque<int> &operations) { if (v[x].processed) return; v[x].processed = true; if (x == v[x].next) return; int next = v[x].next; if (!v[next].processed) { v[next].processed = true; v[x].next = v[next].next; v[next].next = next; operations.push_front(x); operations.push_back(next); } process(v[x].next, operations); } deque<int> simplify() { deque<int> operations; for (int i = 0; i < v.size(); ++i) v[i].processed = false; for (int i = 0; i < v.size(); ++i) process(i, operations); return operations; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); v.push_back(Entity(x, i)); } sort(v.begin(), v.end(), [](Entity a, Entity b) { return a.next < b.next; }); for (int i = 0; i < v.size(); ++i) { v[i].next = i; } sort(v.begin(), v.end(), [](Entity a, Entity b) { return a.initialPosition < b.initialPosition; }); while (somethingToSimplify()) res.push_back(simplify()); printf("%lu\n", res.size()); for (auto &row : res) { printf("%lu\n", row.size()); for (auto &col : row) printf("%d ", col + 1); printf("\n"); } return 0; } |
English