#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;
int n, arr[maxN];
int sorted[maxN];
int goal[maxN];
vector<vi> ans;
vector<vi> get_cycles() {
vector<bool> visited(n);
vector<vi> cycles;
REP(i, n) {
if (!visited[i]) {
vi tmp;
int start = i;
while (goal[arr[start]] != i) {
visited[start] = true;
tmp.pb(start);
start = goal[arr[start]];
}
visited[start] = true;
tmp.pb(start);
cycles.pb(tmp);
}
}
return cycles;
}
vi getSwaps(vi toSwap, vi toSwapEnd) {
reverse(toSwapEnd.begin(), toSwapEnd.end());
vi tmp;
for (auto x: toSwap) tmp.pb(x);
for (auto x: toSwapEnd) tmp.pb(x);
return tmp;
}
int main() {
cin >> n;
REP(i, n) {
cin >> arr[i];
sorted[i] = arr[i];
}
sort(sorted, sorted + n);
REP(i, n) goal[sorted[i]] = i;
auto cycles = get_cycles();
vi swap1, swap2;
for (auto cycle: cycles) {
if (sz(cycle) == 2) {
swap1.pb(cycle[0]);
swap2.pb(cycle[1]);
swap(arr[cycle[0]], arr[cycle[1]]);
}
if (sz(cycle) > 2) {
REP(i, (sz(cycle) - 1) / 2) {
swap1.pb(cycle[i]);
swap2.pb(cycle[sz(cycle) - 2 - i]);
swap(arr[cycle[i]], arr[cycle[sz(cycle) - 2 - i]]);
}
}
}
vi tmp = getSwaps(swap1, swap2);
if (!tmp.empty()) {
ans.pb(tmp);
}
swap1.clear(); swap2.clear();
REP(i, n) {
if (arr[i] != sorted[i]) {
assert(goal[arr[goal[arr[i]]]] == i);
swap1.pb(goal[arr[i]]);
swap2.pb(i);
swap(arr[i], arr[goal[arr[i]]]);
}
}
tmp = getSwaps(swap1, swap2);
if (!tmp.empty()) {
ans.pb(tmp);
}
cout << sz(ans) << "\n";
for (auto x: ans) {
cout << sz(x) << "\n";
for (auto e: x) cout << e + 1 << " ";
cout << "\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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) #define FORD(i,a,b) for(int i = (a); i >= (b); --i) #define RI(i,n) FOR(i,1,(n)) #define REP(i,n) FOR(i,0,(n)-1) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) #define pb push_back #define st first #define nd second #define sz(w) (int) w.size() typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<pii, int> para; const ll inf = 1e18 + 7; const ll maxN = 1e6 + 5; const ll MOD = 1e9 + 7; int n, arr[maxN]; int sorted[maxN]; int goal[maxN]; vector<vi> ans; vector<vi> get_cycles() { vector<bool> visited(n); vector<vi> cycles; REP(i, n) { if (!visited[i]) { vi tmp; int start = i; while (goal[arr[start]] != i) { visited[start] = true; tmp.pb(start); start = goal[arr[start]]; } visited[start] = true; tmp.pb(start); cycles.pb(tmp); } } return cycles; } vi getSwaps(vi toSwap, vi toSwapEnd) { reverse(toSwapEnd.begin(), toSwapEnd.end()); vi tmp; for (auto x: toSwap) tmp.pb(x); for (auto x: toSwapEnd) tmp.pb(x); return tmp; } int main() { cin >> n; REP(i, n) { cin >> arr[i]; sorted[i] = arr[i]; } sort(sorted, sorted + n); REP(i, n) goal[sorted[i]] = i; auto cycles = get_cycles(); vi swap1, swap2; for (auto cycle: cycles) { if (sz(cycle) == 2) { swap1.pb(cycle[0]); swap2.pb(cycle[1]); swap(arr[cycle[0]], arr[cycle[1]]); } if (sz(cycle) > 2) { REP(i, (sz(cycle) - 1) / 2) { swap1.pb(cycle[i]); swap2.pb(cycle[sz(cycle) - 2 - i]); swap(arr[cycle[i]], arr[cycle[sz(cycle) - 2 - i]]); } } } vi tmp = getSwaps(swap1, swap2); if (!tmp.empty()) { ans.pb(tmp); } swap1.clear(); swap2.clear(); REP(i, n) { if (arr[i] != sorted[i]) { assert(goal[arr[goal[arr[i]]]] == i); swap1.pb(goal[arr[i]]); swap2.pb(i); swap(arr[i], arr[goal[arr[i]]]); } } tmp = getSwaps(swap1, swap2); if (!tmp.empty()) { ans.pb(tmp); } cout << sz(ans) << "\n"; for (auto x: ans) { cout << sz(x) << "\n"; for (auto e: x) cout << e + 1 << " "; cout << "\n"; } return 0; } |
English