#include <bits/stdc++.h> using namespace std; #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define F first #define S second #define pb push_back #define eb emplace_back #define mkp make_pair #define pque priority_queue #define vt vector #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define END(x) { cout << x; return; } template<int D, typename T> struct vtd : public vector<vtd<D - 1, T>> { template<typename... Args> vtd(int n = 0, Args... args) : vector<vtd<D - 1, T>>(n, vtd<D - 1, T>(args...)) {} }; template<typename T> struct vtd<1, T> : public vector<T> { vtd(int n = 0, const T& val = T()) : vector<T>(n, val) {} }; #define PI (ld)3.1415926535897932384626433832795 #define MOD (int)1000000007 #define inf (int)1000000001 #define INF (ll)1000000000000000001 #define EPS 1e-9 typedef long double ld; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #ifdef NOT_OJ #define debug(x) cerr << #x << ": "; __print(x); cerr << "\n"; #else #define debug(x) #endif //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(ll t) { cerr << t; } void __print(int t) { cerr << t; } void __print(string t) { cerr << t; } void __print(char t) { cerr << t; } void __print(ld t) { cerr << t; } void __print(double t) { cerr << t; } void __print(ull t) { cerr << t; } void __print(uint t) { cerr << t; } void __print(bool t) { cerr << t; } template <class T, class V> void __print(pair <T, V> p); template <class T> void __print(vt <T> v); template <class T> void __print(set <T> v); template <class T, class V> void __print(map <T, V> v); template <class T> void __print(multiset <T> v); template <class T> void __print(vt <vt <T>> v); template <int D, class T> void __print(vt <vtd <D, T>> v); template <class T> void __print(unordered_set <T> v); template <class T, class V> void __print(unordered_map <T, V> v); template <class T, class V> void __print(pair <T, V> p) {cerr << "{"; __print(p.F); cerr << ","; __print(p.S); cerr << "}";} template <class T> void __print(vt <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(vt <vt <T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <int D, class T> void __print(vt <vtd <D, T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <class T> void __print(set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(multiset <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} void __runcase() { int n; cin >> n; vt <int> v(n); vt <pii> sc(n); for (int i = 0; i < n; i++) { cin >> v[i]; sc[i] = { v[i], i }; } sort(all(sc)); for (int i = 0; i < n; i++) v[sc[i].S] = i; vt <int> pos(n); for (int i = 0; i < n; i++) pos[v[i]] = i; vt <bool> vis(n, false); deque <int> first; for (int i = 0; i < n; i++) { if (vis[i] || pos[i] == i) continue; int a = i, b = v[i]; while(a != b && !vis[a] && !vis[b]) { int A = pos[a], B = v[b]; swap(pos[a], pos[b]); first.push_front(pos[a]); first.push_back(pos[b]); vis[a] = vis[b] = true; a = A; b = B; } vis[a] = true; } deque <int> second; for (int i = 0; i < n; i++) { if (pos[i] == i) continue; second.push_front(pos[i]); second.push_back(pos[pos[i]]); swap(pos[i], pos[pos[i]]); } cout << !first.empty() + !second.empty() << "\n"; if (sz(first)) { cout << sz(first) << "\n"; for (auto &x : first) cout << x + 1 << " "; cout << "\n"; } if (sz(second)) { cout << sz(second) << "\n"; for (auto &x : second) cout << x + 1 << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int __test_cases = 1; // cin >> __test_cases; while(__test_cases--) { __runcase(); } }
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #include <bits/stdc++.h> using namespace std; #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define F first #define S second #define pb push_back #define eb emplace_back #define mkp make_pair #define pque priority_queue #define vt vector #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define END(x) { cout << x; return; } template<int D, typename T> struct vtd : public vector<vtd<D - 1, T>> { template<typename... Args> vtd(int n = 0, Args... args) : vector<vtd<D - 1, T>>(n, vtd<D - 1, T>(args...)) {} }; template<typename T> struct vtd<1, T> : public vector<T> { vtd(int n = 0, const T& val = T()) : vector<T>(n, val) {} }; #define PI (ld)3.1415926535897932384626433832795 #define MOD (int)1000000007 #define inf (int)1000000001 #define INF (ll)1000000000000000001 #define EPS 1e-9 typedef long double ld; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #ifdef NOT_OJ #define debug(x) cerr << #x << ": "; __print(x); cerr << "\n"; #else #define debug(x) #endif //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(ll t) { cerr << t; } void __print(int t) { cerr << t; } void __print(string t) { cerr << t; } void __print(char t) { cerr << t; } void __print(ld t) { cerr << t; } void __print(double t) { cerr << t; } void __print(ull t) { cerr << t; } void __print(uint t) { cerr << t; } void __print(bool t) { cerr << t; } template <class T, class V> void __print(pair <T, V> p); template <class T> void __print(vt <T> v); template <class T> void __print(set <T> v); template <class T, class V> void __print(map <T, V> v); template <class T> void __print(multiset <T> v); template <class T> void __print(vt <vt <T>> v); template <int D, class T> void __print(vt <vtd <D, T>> v); template <class T> void __print(unordered_set <T> v); template <class T, class V> void __print(unordered_map <T, V> v); template <class T, class V> void __print(pair <T, V> p) {cerr << "{"; __print(p.F); cerr << ","; __print(p.S); cerr << "}";} template <class T> void __print(vt <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(vt <vt <T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <int D, class T> void __print(vt <vtd <D, T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <class T> void __print(set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(multiset <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} void __runcase() { int n; cin >> n; vt <int> v(n); vt <pii> sc(n); for (int i = 0; i < n; i++) { cin >> v[i]; sc[i] = { v[i], i }; } sort(all(sc)); for (int i = 0; i < n; i++) v[sc[i].S] = i; vt <int> pos(n); for (int i = 0; i < n; i++) pos[v[i]] = i; vt <bool> vis(n, false); deque <int> first; for (int i = 0; i < n; i++) { if (vis[i] || pos[i] == i) continue; int a = i, b = v[i]; while(a != b && !vis[a] && !vis[b]) { int A = pos[a], B = v[b]; swap(pos[a], pos[b]); first.push_front(pos[a]); first.push_back(pos[b]); vis[a] = vis[b] = true; a = A; b = B; } vis[a] = true; } deque <int> second; for (int i = 0; i < n; i++) { if (pos[i] == i) continue; second.push_front(pos[i]); second.push_back(pos[pos[i]]); swap(pos[i], pos[pos[i]]); } cout << !first.empty() + !second.empty() << "\n"; if (sz(first)) { cout << sz(first) << "\n"; for (auto &x : first) cout << x + 1 << " "; cout << "\n"; } if (sz(second)) { cout << sz(second) << "\n"; for (auto &x : second) cout << x + 1 << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int __test_cases = 1; // cin >> __test_cases; while(__test_cases--) { __runcase(); } } |