#include <iostream>
#include <vector>
constexpr int maxN = 5000;
int t[maxN];
int count[maxN];
int out[maxN];
bool visited[maxN];
int vis[maxN];
std::vector<int> v;
int main()
{
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
int n;
std::cin >> n;
for (int i = 0; i < n; ++i)
{
std::cin >> t[i];
++count[t[i]];
}
for (int i = 1; i < maxN; ++i)
{
count[i] += count[i - 1];
}
for (int i = 0; i < n; ++i)
{
out[i] = --count[t[i]];
}
int maxsize = 0;
for (int i = 0; i < n; ++i)
{
int j = i;
int size = 0;
while (!visited[j])
{
visited[j] = 1;
++size;
j = out[j];
if (size == 1)
v.push_back(j);
}
maxsize = std::max(maxsize, size);
}
int m = 0;
int l = 1;
while (maxsize > l)
{
++m;
l *= 2;
}
std::cout << m << "\n";
std::vector<int> front, end;
for (int ij = 1; ij <= m; ++ij)
{
front.clear(), end.clear();
for (int i = 0; i < v.size(); ++i)
{
if (out[v[i]] == v[i])
{
std::swap(v[i], v[v.size() - 1]);
v.pop_back();
--i;
continue;
}
int j = out[v[i]];
while (vis[j] != ij && vis[out[j]] != ij)
{
vis[j] = ij;
vis[out[j]] = ij;
if (out[j] == j)
{
std::swap(v[i], v[v.size() - 1]);
v.pop_back();
--i;
break;
}
front.push_back(j);
end.push_back(out[j]);
std::swap(out[j], out[out[j]]);
j = out[j];
}
v[i] = j;
}
std::cout << front.size() * 2 << "\n";
for (int i = 0; i < front.size(); ++i)
std::cout << front[i] + 1 << " ";
for (int i = end.size() - 1; i >= 0; --i)
std::cout << end[i] + 1 << " ";
std::cout << "\n";
}
}
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 | #include <iostream> #include <vector> constexpr int maxN = 5000; int t[maxN]; int count[maxN]; int out[maxN]; bool visited[maxN]; int vis[maxN]; std::vector<int> v; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> t[i]; ++count[t[i]]; } for (int i = 1; i < maxN; ++i) { count[i] += count[i - 1]; } for (int i = 0; i < n; ++i) { out[i] = --count[t[i]]; } int maxsize = 0; for (int i = 0; i < n; ++i) { int j = i; int size = 0; while (!visited[j]) { visited[j] = 1; ++size; j = out[j]; if (size == 1) v.push_back(j); } maxsize = std::max(maxsize, size); } int m = 0; int l = 1; while (maxsize > l) { ++m; l *= 2; } std::cout << m << "\n"; std::vector<int> front, end; for (int ij = 1; ij <= m; ++ij) { front.clear(), end.clear(); for (int i = 0; i < v.size(); ++i) { if (out[v[i]] == v[i]) { std::swap(v[i], v[v.size() - 1]); v.pop_back(); --i; continue; } int j = out[v[i]]; while (vis[j] != ij && vis[out[j]] != ij) { vis[j] = ij; vis[out[j]] = ij; if (out[j] == j) { std::swap(v[i], v[v.size() - 1]); v.pop_back(); --i; break; } front.push_back(j); end.push_back(out[j]); std::swap(out[j], out[out[j]]); j = out[j]; } v[i] = j; } std::cout << front.size() * 2 << "\n"; for (int i = 0; i < front.size(); ++i) std::cout << front[i] + 1 << " "; for (int i = end.size() - 1; i >= 0; --i) std::cout << end[i] + 1 << " "; std::cout << "\n"; } } |
English