#include <bits/stdc++.h> using namespace std; typedef int ll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; ll a[n]; for (ll i = 0; i < n; i++) { cin >> a[i]; } vector <pair<ll, ll>> sortedA; for (ll i = 0; i < n; i++) { sortedA.emplace_back(a[i], i); } sort(sortedA.begin(), sortedA.end()); vector <pair<ll, ll>> where (n); for (ll i = 0; i < n; i++) { where[sortedA[i].second] = {sortedA[i].first, i}; } ll ans = 0; vector <ll> ansVec (n); vector <vector<ll>> ansVecVec; ll i = 0; ll lim = n/2; if (n % 2 == 1) { lim++; } bool ready = false; while (!ready) { ready = true; i = 0; ll j = 0; while (i < lim) { if (where[i].second != i) { ansVec[i] = i + 1; ansVec[n - i - 1] = where[i].second + 1; swap(where[i], where[where[i].second]); i++; ready = false; }/*else if (i == lim - 1) { break; }*/ j++; if (j == n) { break; } } if (!ready) { ansVecVec.push_back(ansVec); ans++; } } cout << ans << endl; for (auto & item : ansVecVec) { ll s = item.size(); for (ll i = 0; i < s; i++) { if (item[i] == 0) { s -= 1; } } cout << s << endl; for (ll j = 0; j < item.size(); j++) { if (item[j] != 0) { cout << item[j] << " "; } } cout << endl; } 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 | #include <bits/stdc++.h> using namespace std; typedef int ll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; ll a[n]; for (ll i = 0; i < n; i++) { cin >> a[i]; } vector <pair<ll, ll>> sortedA; for (ll i = 0; i < n; i++) { sortedA.emplace_back(a[i], i); } sort(sortedA.begin(), sortedA.end()); vector <pair<ll, ll>> where (n); for (ll i = 0; i < n; i++) { where[sortedA[i].second] = {sortedA[i].first, i}; } ll ans = 0; vector <ll> ansVec (n); vector <vector<ll>> ansVecVec; ll i = 0; ll lim = n/2; if (n % 2 == 1) { lim++; } bool ready = false; while (!ready) { ready = true; i = 0; ll j = 0; while (i < lim) { if (where[i].second != i) { ansVec[i] = i + 1; ansVec[n - i - 1] = where[i].second + 1; swap(where[i], where[where[i].second]); i++; ready = false; }/*else if (i == lim - 1) { break; }*/ j++; if (j == n) { break; } } if (!ready) { ansVecVec.push_back(ansVec); ans++; } } cout << ans << endl; for (auto & item : ansVecVec) { ll s = item.size(); for (ll i = 0; i < s; i++) { if (item[i] == 0) { s -= 1; } } cout << s << endl; for (ll j = 0; j < item.size(); j++) { if (item[j] != 0) { cout << item[j] << " "; } } cout << endl; } return 0; } |