#include <iostream> #include <algorithm> #include <queue> #include <stack> using namespace std; const int N = 3003; int tab[N]; pair<int, int> ska[N]; queue <int> kol[N]; stack <int> sto[N]; bool zab[N]; int n; void wczyska() { for (int i = 1; i <= n; i++) cin >> ska[i].first; for (int i = 1; i <= n; i++) ska[i].second = i; sort(ska + 1, ska + n + 1); for (int i = 1; i <= n; i++) ska[i].first = i; for (int i = 1; i <= n; i++) swap(ska[i].first, ska[i].second); sort(ska + 1, ska + n + 1); for (int i = 1; i <= n; i++) swap(ska[i].first, ska[i].second); //for (int i = 1; i <= n; i++) cout << ska[i].first << " " << ska[i].second << "\n"; for (int i = 1; i <= n; i++) tab[i] = ska[i].first; } bool roz() { for (int i = 1; i <= n; i++) if (tab[i] != i) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; wczyska(); int bruch = 0; while (roz()) { bruch++; for (int i = 1; i <= n; i++) zab[i] = false; for (int i = 1; i <= n; i++) { if (!zab[i] && !zab[tab[i]] && tab[i] != i) { //cout << i << " " << tab[i] << "\n"; int a = i; int b = tab[i]; zab[a] = 1; zab[b] = 1; kol[bruch].push(a); sto[bruch].push(b); swap(tab[a], tab[b]); } } /*cout << 2 * kol[bruch].size() << "\n"; while (!kol[bruch].empty()) { cout << kol[bruch].front() << " "; kol[bruch].pop(); } while (!sto[bruch].empty()) { cout << sto[bruch].top() << " "; sto[bruch].pop(); } cout << "\n"; */ } cout << bruch << "\n"; for (int i = 1; i <= bruch; i++) { cout << 2 * kol[i].size() << "\n"; while (!kol[i].empty()) { cout << kol[i].front() << " "; kol[i].pop(); } while (!sto[i].empty()) { cout << sto[i].top() << " "; sto[i].pop(); } cout << "\n"; } }
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 | #include <iostream> #include <algorithm> #include <queue> #include <stack> using namespace std; const int N = 3003; int tab[N]; pair<int, int> ska[N]; queue <int> kol[N]; stack <int> sto[N]; bool zab[N]; int n; void wczyska() { for (int i = 1; i <= n; i++) cin >> ska[i].first; for (int i = 1; i <= n; i++) ska[i].second = i; sort(ska + 1, ska + n + 1); for (int i = 1; i <= n; i++) ska[i].first = i; for (int i = 1; i <= n; i++) swap(ska[i].first, ska[i].second); sort(ska + 1, ska + n + 1); for (int i = 1; i <= n; i++) swap(ska[i].first, ska[i].second); //for (int i = 1; i <= n; i++) cout << ska[i].first << " " << ska[i].second << "\n"; for (int i = 1; i <= n; i++) tab[i] = ska[i].first; } bool roz() { for (int i = 1; i <= n; i++) if (tab[i] != i) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; wczyska(); int bruch = 0; while (roz()) { bruch++; for (int i = 1; i <= n; i++) zab[i] = false; for (int i = 1; i <= n; i++) { if (!zab[i] && !zab[tab[i]] && tab[i] != i) { //cout << i << " " << tab[i] << "\n"; int a = i; int b = tab[i]; zab[a] = 1; zab[b] = 1; kol[bruch].push(a); sto[bruch].push(b); swap(tab[a], tab[b]); } } /*cout << 2 * kol[bruch].size() << "\n"; while (!kol[bruch].empty()) { cout << kol[bruch].front() << " "; kol[bruch].pop(); } while (!sto[bruch].empty()) { cout << sto[bruch].top() << " "; sto[bruch].pop(); } cout << "\n"; */ } cout << bruch << "\n"; for (int i = 1; i <= bruch; i++) { cout << 2 * kol[i].size() << "\n"; while (!kol[i].empty()) { cout << kol[i].front() << " "; kol[i].pop(); } while (!sto[i].empty()) { cout << sto[i].top() << " "; sto[i].pop(); } cout << "\n"; } } |