#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run() { int n; cin >> n; vi elems(n); each(e, elems) cin >> e; vi ord(n); iota(all(ord), 0); sort(all(ord), [&](int l, int r) { return elems[l] < elems[r]; }); deque<int> ans[2]; vector<bool> seen(n); auto step = [&](int i, int j, int k) { ans[i].push_front(j); ans[i].push_back(k); }; rep(i, 0, n) if (!seen[i]) { vi cycle = {i}; while (ord[cycle.back()] != cycle[0]) cycle.pb(ord[cycle.back()]); each(v, cycle) seen[v] = 1; if (sz(cycle) == 2) { step(0, cycle[0], cycle[1]); } else if (sz(cycle) > 2) { for (int a = 0, b = sz(cycle)-1; a < b; a++, b--) { step(0, cycle[a], cycle[b]); } for (int a = 0, b = sz(cycle)-2; a < b; a++, b--) { step(1, cycle[a], cycle[b]); } } } cout << !ans[0].empty() + !ans[1].empty() << '\n'; each(vec, ans) if (!vec.empty()) { cout << sz(vec) << '\n'; each(v, vec) cout << v+1 << ' '; cout << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(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 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run() { int n; cin >> n; vi elems(n); each(e, elems) cin >> e; vi ord(n); iota(all(ord), 0); sort(all(ord), [&](int l, int r) { return elems[l] < elems[r]; }); deque<int> ans[2]; vector<bool> seen(n); auto step = [&](int i, int j, int k) { ans[i].push_front(j); ans[i].push_back(k); }; rep(i, 0, n) if (!seen[i]) { vi cycle = {i}; while (ord[cycle.back()] != cycle[0]) cycle.pb(ord[cycle.back()]); each(v, cycle) seen[v] = 1; if (sz(cycle) == 2) { step(0, cycle[0], cycle[1]); } else if (sz(cycle) > 2) { for (int a = 0, b = sz(cycle)-1; a < b; a++, b--) { step(0, cycle[a], cycle[b]); } for (int a = 0, b = sz(cycle)-2; a < b; a++, b--) { step(1, cycle[a], cycle[b]); } } } cout << !ans[0].empty() + !ans[1].empty() << '\n'; each(vec, ans) if (!vec.empty()) { cout << sz(vec) << '\n'; each(v, vec) cout << v+1 << ' '; cout << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |