#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Height { short Height; short Position; }; bool findPosition(short& position, Height& height) { return position = height.Position; } bool compareByHeight(const Height& height, const Height& height2) { return height.Height < height2.Height; } const short GetIndexWithSpecificNormalizeHeight(const short specificHeight, const std::vector<Height>& heights) { for (short i = 0; i < heights.size(); ++i) { if (heights[i].Position == specificHeight) { return i; } } } int olol(std::vector<Height> heigths) { int count = 0; // short graduates = 0; // cin >> graduates; // // std::vector<Height> heigths; // // for (short i = 0; i < graduates; ++i) // { // short currentHeight = 0; // cin >> currentHeight; // // Height height = { currentHeight, i }; // heigths.push_back(height); // } // // sort(heigths.begin(), heigths.end(), compareByHeight); std::vector<short> results; while (true) { for (auto& height : heigths) { height.Height = 1; // to znaczy ze mozna go uzyc w algorytmie ponizej } auto resultSizeBeforeLoop = results.size(); //auto tmpBegin = results.size() for (short i = 0; i < heigths.size(); ++i) { const auto currentHeight = heigths[i].Position; if (i != heigths[i].Position && heigths[i].Height == 1 && heigths[currentHeight].Height == 1) { heigths[i].Position = heigths[currentHeight].Position; heigths[currentHeight].Position = currentHeight; heigths[i].Height = 0; heigths[currentHeight].Height = 0; results.insert(results.begin(), i); results.push_back(currentHeight); } } //results.push_back(3001); if (resultSizeBeforeLoop == results.size()) { return count; // break; } // cout << results.size() << endl; //for (const auto result : results) //{ //} count++; results.clear(); } //return count; } int main() { // cout << olol() << endl; short graduates = 0; cin >> graduates; std::vector<Height> heigths; for (short i = 0; i < graduates; ++i) { short currentHeight = 0; cin >> currentHeight; Height height = { currentHeight, i }; heigths.push_back(height); } sort(heigths.begin(), heigths.end(), compareByHeight); std::vector<short> results; cout << olol(heigths) << endl; while (true) { for (auto& height : heigths) { height.Height = 1; // to znaczy ze mozna go uzyc w algorytmie ponizej } auto resultSizeBeforeLoop = results.size(); //auto tmpBegin = results.size() for (short i = 0; i < heigths.size(); ++i) { const auto currentHeight = heigths[i].Position; if (i != heigths[i].Position && heigths[i].Height == 1 && heigths[currentHeight].Height == 1) { heigths[i].Position = heigths[currentHeight].Position; heigths[currentHeight].Position = currentHeight; heigths[i].Height = 0; heigths[currentHeight].Height = 0; results.insert(results.begin(), i); results.push_back(heigths[currentHeight].Position); } } //results.push_back(3001); if (resultSizeBeforeLoop == results.size()) { break; } cout << results.size() << endl; for (const auto result : results) { cout << result + 1 << " "; } cout << endl; results.clear(); } 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 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Height { short Height; short Position; }; bool findPosition(short& position, Height& height) { return position = height.Position; } bool compareByHeight(const Height& height, const Height& height2) { return height.Height < height2.Height; } const short GetIndexWithSpecificNormalizeHeight(const short specificHeight, const std::vector<Height>& heights) { for (short i = 0; i < heights.size(); ++i) { if (heights[i].Position == specificHeight) { return i; } } } int olol(std::vector<Height> heigths) { int count = 0; // short graduates = 0; // cin >> graduates; // // std::vector<Height> heigths; // // for (short i = 0; i < graduates; ++i) // { // short currentHeight = 0; // cin >> currentHeight; // // Height height = { currentHeight, i }; // heigths.push_back(height); // } // // sort(heigths.begin(), heigths.end(), compareByHeight); std::vector<short> results; while (true) { for (auto& height : heigths) { height.Height = 1; // to znaczy ze mozna go uzyc w algorytmie ponizej } auto resultSizeBeforeLoop = results.size(); //auto tmpBegin = results.size() for (short i = 0; i < heigths.size(); ++i) { const auto currentHeight = heigths[i].Position; if (i != heigths[i].Position && heigths[i].Height == 1 && heigths[currentHeight].Height == 1) { heigths[i].Position = heigths[currentHeight].Position; heigths[currentHeight].Position = currentHeight; heigths[i].Height = 0; heigths[currentHeight].Height = 0; results.insert(results.begin(), i); results.push_back(currentHeight); } } //results.push_back(3001); if (resultSizeBeforeLoop == results.size()) { return count; // break; } // cout << results.size() << endl; //for (const auto result : results) //{ //} count++; results.clear(); } //return count; } int main() { // cout << olol() << endl; short graduates = 0; cin >> graduates; std::vector<Height> heigths; for (short i = 0; i < graduates; ++i) { short currentHeight = 0; cin >> currentHeight; Height height = { currentHeight, i }; heigths.push_back(height); } sort(heigths.begin(), heigths.end(), compareByHeight); std::vector<short> results; cout << olol(heigths) << endl; while (true) { for (auto& height : heigths) { height.Height = 1; // to znaczy ze mozna go uzyc w algorytmie ponizej } auto resultSizeBeforeLoop = results.size(); //auto tmpBegin = results.size() for (short i = 0; i < heigths.size(); ++i) { const auto currentHeight = heigths[i].Position; if (i != heigths[i].Position && heigths[i].Height == 1 && heigths[currentHeight].Height == 1) { heigths[i].Position = heigths[currentHeight].Position; heigths[currentHeight].Position = currentHeight; heigths[i].Height = 0; heigths[currentHeight].Height = 0; results.insert(results.begin(), i); results.push_back(heigths[currentHeight].Position); } } //results.push_back(3001); if (resultSizeBeforeLoop == results.size()) { break; } cout << results.size() << endl; for (const auto result : results) { cout << result + 1 << " "; } cout << endl; results.clear(); } return 0; } |