#include <iostream> #include <algorithm> #include <deque> #include <vector> struct TargetElem { int m_iNum; int m_iOriginalPos; }; struct CurrentElem { int m_iNum; int m_iTargetPos; int m_iMovedInCycle; }; static int iCycle = 0; static int iCnt; static std::vector<std::deque<int>> vdeq; static TargetElem aTargetElems[3002]; static CurrentElem aCurrentElems[3002]; static void ReadData() { int x; std::cin >> iCnt; for (int i = 0; i < iCnt; ++i) { std::cin >> x; aTargetElems[i].m_iNum = x; aTargetElems[i].m_iOriginalPos = i; aCurrentElems[i].m_iNum = x; aCurrentElems[i].m_iMovedInCycle = -1; } } static void SetGoals() { std::sort(aTargetElems, aTargetElems + iCnt, [](const TargetElem & e0, const TargetElem& e1) -> bool { return e0.m_iNum < e1.m_iNum; }); for (int i = 0; i < iCnt; ++i) { const TargetElem& t = aTargetElems[i]; aCurrentElems[t.m_iOriginalPos].m_iTargetPos = i; } } static inline void GetCycleIndices(std::vector<int> & vi, int iFirstIndex) { vi.clear(); int i = iFirstIndex; for (;;) { CurrentElem& e = aCurrentElems[i]; e.m_iMovedInCycle = iCycle; vi.push_back(i); i = e.m_iTargetPos; if (i == iFirstIndex) break; } } static inline void InvertCycle(std::vector<int>& vi, std::deque<int>& deq) { std::sort(vi.begin(), vi.end()); int iFirst = 0; int iLast = (int) vi.size() - 1; for (; iFirst < iLast; ++iFirst, --iLast) { int iFirstIndex = vi[iFirst]; int iLastIndex = vi[iLast]; std::swap(aCurrentElems[iFirstIndex], aCurrentElems[iLastIndex]); deq.push_front(iFirstIndex + 1); deq.push_back(iLastIndex + 1); } } static bool DoCycle() { std::deque<int> deq; std::vector<int> vi; for (int i = 0; i < iCnt; ++i) { CurrentElem& e = aCurrentElems[i]; if (e.m_iTargetPos == i) continue; if (e.m_iMovedInCycle == iCycle) continue; GetCycleIndices(vi, i); InvertCycle(vi, deq); } if (deq.size() == 0) return false; vdeq.push_back(deq); return true; } static void Solve() { while (DoCycle()) { iCycle++; } } static void Output() { std::cout << vdeq.size(); for (const auto& deq : vdeq) { std::cout << std::endl << deq.size() << std::endl; auto it = deq.begin(); std::cout << *it; while (++it != deq.end()) { std::cout << " " << *it; } } } int main() { ReadData(); SetGoals(); Solve(); Output(); }
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #include <iostream> #include <algorithm> #include <deque> #include <vector> struct TargetElem { int m_iNum; int m_iOriginalPos; }; struct CurrentElem { int m_iNum; int m_iTargetPos; int m_iMovedInCycle; }; static int iCycle = 0; static int iCnt; static std::vector<std::deque<int>> vdeq; static TargetElem aTargetElems[3002]; static CurrentElem aCurrentElems[3002]; static void ReadData() { int x; std::cin >> iCnt; for (int i = 0; i < iCnt; ++i) { std::cin >> x; aTargetElems[i].m_iNum = x; aTargetElems[i].m_iOriginalPos = i; aCurrentElems[i].m_iNum = x; aCurrentElems[i].m_iMovedInCycle = -1; } } static void SetGoals() { std::sort(aTargetElems, aTargetElems + iCnt, [](const TargetElem & e0, const TargetElem& e1) -> bool { return e0.m_iNum < e1.m_iNum; }); for (int i = 0; i < iCnt; ++i) { const TargetElem& t = aTargetElems[i]; aCurrentElems[t.m_iOriginalPos].m_iTargetPos = i; } } static inline void GetCycleIndices(std::vector<int> & vi, int iFirstIndex) { vi.clear(); int i = iFirstIndex; for (;;) { CurrentElem& e = aCurrentElems[i]; e.m_iMovedInCycle = iCycle; vi.push_back(i); i = e.m_iTargetPos; if (i == iFirstIndex) break; } } static inline void InvertCycle(std::vector<int>& vi, std::deque<int>& deq) { std::sort(vi.begin(), vi.end()); int iFirst = 0; int iLast = (int) vi.size() - 1; for (; iFirst < iLast; ++iFirst, --iLast) { int iFirstIndex = vi[iFirst]; int iLastIndex = vi[iLast]; std::swap(aCurrentElems[iFirstIndex], aCurrentElems[iLastIndex]); deq.push_front(iFirstIndex + 1); deq.push_back(iLastIndex + 1); } } static bool DoCycle() { std::deque<int> deq; std::vector<int> vi; for (int i = 0; i < iCnt; ++i) { CurrentElem& e = aCurrentElems[i]; if (e.m_iTargetPos == i) continue; if (e.m_iMovedInCycle == iCycle) continue; GetCycleIndices(vi, i); InvertCycle(vi, deq); } if (deq.size() == 0) return false; vdeq.push_back(deq); return true; } static void Solve() { while (DoCycle()) { iCycle++; } } static void Output() { std::cout << vdeq.size(); for (const auto& deq : vdeq) { std::cout << std::endl << deq.size() << std::endl; auto it = deq.begin(); std::cout << *it; while (++it != deq.end()) { std::cout << " " << *it; } } } int main() { ReadData(); SetGoals(); Solve(); Output(); } |