#include <bits/stdc++.h> using namespace std; typedef long long ll; constexpr int mSize = 3010; int ostatnio[mSize]; int tajm = 1; struct theBestStruct { int value; int iter; }; theBestStruct tab[mSize]; bool cmp(theBestStruct a, theBestStruct b) { return a.value <= b.value; } bool cmpDwa(theBestStruct a, theBestStruct b) { return a.iter < b.iter; } vector<int> lewo[mSize]; vector<int> prawo[mSize]; void solve(int aktIter) { if (ostatnio[tab[aktIter].value] != tajm && ostatnio[aktIter] != tajm) { ostatnio[tab[aktIter].value] = tajm; ostatnio[aktIter] = tajm; lewo[tajm].push_back(aktIter); prawo[tajm].push_back(tab[aktIter].value); swap(tab[aktIter].value, tab[tab[aktIter].value].value); solve(tab[aktIter].value); } } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int n, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; tab[i].value = x; tab[i].iter = i; } sort(tab + 1, tab + n + 1, cmp); for (int i = 1; i <= n; i++) tab[i].value = i; sort(tab + 1, tab + n + 1, cmpDwa); bool good; int ans = 0; //value - gdzie chce byc //iter - gdzie jest do { /*for (int i = 1; i <= n; i++) { cout << "i=" << tab[i].iter << " value=" << tab[i].value << '\n'; } cout << "KONIEC TESTU:\n";*/ good = true; for (int i = 1; i <= n; i++) { if (tab[i].iter != tab[i].value) { good = false; if (ostatnio[tab[i].value] != tajm && ostatnio[tab[i].iter] != tajm) { //ostatnio[tab[i].value] = tajm; //ostatnio[tab[i].iter] = tajm; //lewo[tajm].push_back(tab[i].iter); //prawo[tajm].push_back(tab[i].value); //swap(tab[i].value, tab[tab[i].value].value); //solve(tab[i].value); solve(i); } } } /*cout << "VALUE: "; for (int i = 1; i <= n; i++) cout << tab[i].value << ' '; cout << "\nITER : "; for (int i = 1; i <= n; i++) cout << tab[i].iter << ' '; cout << '\n';*/ ++tajm; } while (!good); for (int i = 1; prawo[i].size() > 0; i++) ++ans; cout << ans << '\n'; for (int i = 1; prawo[i].size() > 0; i++) { cout << (prawo[i].size() * 2) << '\n'; for (auto z : lewo[i]) cout << z << ' '; for (int j = (int)prawo[i].size() - 1; j >= 0; j--) cout << prawo[i][j] << ' '; cout << '\n'; } 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 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 135 | #include <bits/stdc++.h> using namespace std; typedef long long ll; constexpr int mSize = 3010; int ostatnio[mSize]; int tajm = 1; struct theBestStruct { int value; int iter; }; theBestStruct tab[mSize]; bool cmp(theBestStruct a, theBestStruct b) { return a.value <= b.value; } bool cmpDwa(theBestStruct a, theBestStruct b) { return a.iter < b.iter; } vector<int> lewo[mSize]; vector<int> prawo[mSize]; void solve(int aktIter) { if (ostatnio[tab[aktIter].value] != tajm && ostatnio[aktIter] != tajm) { ostatnio[tab[aktIter].value] = tajm; ostatnio[aktIter] = tajm; lewo[tajm].push_back(aktIter); prawo[tajm].push_back(tab[aktIter].value); swap(tab[aktIter].value, tab[tab[aktIter].value].value); solve(tab[aktIter].value); } } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int n, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; tab[i].value = x; tab[i].iter = i; } sort(tab + 1, tab + n + 1, cmp); for (int i = 1; i <= n; i++) tab[i].value = i; sort(tab + 1, tab + n + 1, cmpDwa); bool good; int ans = 0; //value - gdzie chce byc //iter - gdzie jest do { /*for (int i = 1; i <= n; i++) { cout << "i=" << tab[i].iter << " value=" << tab[i].value << '\n'; } cout << "KONIEC TESTU:\n";*/ good = true; for (int i = 1; i <= n; i++) { if (tab[i].iter != tab[i].value) { good = false; if (ostatnio[tab[i].value] != tajm && ostatnio[tab[i].iter] != tajm) { //ostatnio[tab[i].value] = tajm; //ostatnio[tab[i].iter] = tajm; //lewo[tajm].push_back(tab[i].iter); //prawo[tajm].push_back(tab[i].value); //swap(tab[i].value, tab[tab[i].value].value); //solve(tab[i].value); solve(i); } } } /*cout << "VALUE: "; for (int i = 1; i <= n; i++) cout << tab[i].value << ' '; cout << "\nITER : "; for (int i = 1; i <= n; i++) cout << tab[i].iter << ' '; cout << '\n';*/ ++tajm; } while (!good); for (int i = 1; prawo[i].size() > 0; i++) ++ans; cout << ans << '\n'; for (int i = 1; prawo[i].size() > 0; i++) { cout << (prawo[i].size() * 2) << '\n'; for (auto z : lewo[i]) cout << z << ' '; for (int j = (int)prawo[i].size() - 1; j >= 0; j--) cout << prawo[i][j] << ' '; cout << '\n'; } return 0; } |