#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif int N, M, k, l; const int INF = 1000 * 1000 * 1000; const int MAKSN = 1000 * 1000 + 13; const LL MOD = 123456789LL; VI v, vcp; int whereShouldBe[3010]; void readIn() { cin >> N; for (int i = 0; i < N; i++) { cin >> k; v.PB(k); vcp.PB(k); } sort(vcp.begin(), vcp.end()); for (int i = 0; i < N; i++) { whereShouldBe[vcp[i]] = i; } } void solve() { vector<vector<int>> res; while (true) { set<int> swapped; vector<pair<int, int> > cur; for (int i = 0; i < v.size(); i++) { if (i != whereShouldBe[v[i]] && swapped.find(i) == swapped.end() && swapped.find(whereShouldBe[v[i]]) == swapped.end()) { cur.PB({i, whereShouldBe[v[i]]}); swapped.insert(i); swapped.insert(whereShouldBe[v[i]]); swap(v[i], v[whereShouldBe[v[i]]]); } } vector<int> curRes; for (int i = 0; i < cur.size(); i++) { curRes.push_back(cur[i].ST); } for (int i = cur.size() - 1; i >= 0; i--) { curRes.push_back(cur[i].ND); } if(curRes.empty()) break; res.PB(curRes); } cout << res.size() << "\n"; for (int i = 0; i < res.size(); i++) { cout << res[i].size() << "\n"; for (int j = 0; j < res[i].size(); j++) { cout << res[i][j] + 1 << " "; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); readIn(); solve(); 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 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif int N, M, k, l; const int INF = 1000 * 1000 * 1000; const int MAKSN = 1000 * 1000 + 13; const LL MOD = 123456789LL; VI v, vcp; int whereShouldBe[3010]; void readIn() { cin >> N; for (int i = 0; i < N; i++) { cin >> k; v.PB(k); vcp.PB(k); } sort(vcp.begin(), vcp.end()); for (int i = 0; i < N; i++) { whereShouldBe[vcp[i]] = i; } } void solve() { vector<vector<int>> res; while (true) { set<int> swapped; vector<pair<int, int> > cur; for (int i = 0; i < v.size(); i++) { if (i != whereShouldBe[v[i]] && swapped.find(i) == swapped.end() && swapped.find(whereShouldBe[v[i]]) == swapped.end()) { cur.PB({i, whereShouldBe[v[i]]}); swapped.insert(i); swapped.insert(whereShouldBe[v[i]]); swap(v[i], v[whereShouldBe[v[i]]]); } } vector<int> curRes; for (int i = 0; i < cur.size(); i++) { curRes.push_back(cur[i].ST); } for (int i = cur.size() - 1; i >= 0; i--) { curRes.push_back(cur[i].ND); } if(curRes.empty()) break; res.PB(curRes); } cout << res.size() << "\n"; for (int i = 0; i < res.size(); i++) { cout << res[i].size() << "\n"; for (int j = 0; j < res[i].size(); j++) { cout << res[i][j] + 1 << " "; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); readIn(); solve(); return 0; } |