#include <algorithm> #include <fstream> #include <string> #include <sstream> #include <iostream> #include <vector> void tokenize(std::string const& str, const char delim, std::vector<std::string>& out) { // construct a stream from the string std::stringstream ss(str); std::string s; while (std::getline(ss, s, delim)) { out.push_back(s); } } int main() { std::ifstream file; file.open("orn.in"); if (file.is_open()) { //Get numbers from file std::string line; std::vector<std::string> string_numbers; std::getline(file, line); int length = std::stoi(line); std::getline(file, line); tokenize(line, ' ', string_numbers); std::vector<int> numbers; for (auto str_num : string_numbers) { numbers.push_back(std::stoi(str_num)); } std::vector<int> numbers1; //Do the algorithm int i = 0; int changed[6] = { -1, -1, -1, -1, -1, -1 }; /*int changed5 = -1; int changed6 = -1; int changed1 = -1; int changed2 = -1; int changed3 = -1; int changed4 = -1; */ numbers1 = numbers; if (numbers1[i + 1] > numbers1[i]) { changed[4] = 0; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = -(2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[4]; } } } i = 0; numbers1 = numbers; if (numbers1[i + 1] < numbers1[i]) { changed[5] = 0; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = (2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[5]; } } } i = 0; numbers1 = numbers; if (numbers1[i + 1] == numbers1[i]) { changed[0] = 0; changed[1] = 0; changed[2] = 0; changed[3] = 0; ++changed[0]; ++changed[1]; ++changed[2]; ++changed[3]; //4 possibilities: //first bigger i = 0; numbers1 = numbers; numbers1[i] = 2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = (2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[0]; } } //first smaller i = 0; numbers1 = numbers; numbers1[i] = -2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = -(2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[1]; } } //second bigger i = 0; numbers1 = numbers; numbers1[i + 1] = 2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = -(2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[2]; } } //second smaller i = 0; numbers1 = numbers; numbers1[i + 1] = -2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = (2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[3]; } } } /* std::cout << changed[0] << '\n'; std::cout << changed[1] << '\n'; std::cout << changed[2] << '\n'; std::cout << changed[3] << '\n'; std::cout << changed[4] << '\n'; std::cout << changed[5] << '\n'; */ std::vector<int> results; for (int n : changed) { if (n > -1) { results.push_back(n); } } std::sort(results.begin(), results.end()); std::cout << results[0]; } file.close(); }
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #include <algorithm> #include <fstream> #include <string> #include <sstream> #include <iostream> #include <vector> void tokenize(std::string const& str, const char delim, std::vector<std::string>& out) { // construct a stream from the string std::stringstream ss(str); std::string s; while (std::getline(ss, s, delim)) { out.push_back(s); } } int main() { std::ifstream file; file.open("orn.in"); if (file.is_open()) { //Get numbers from file std::string line; std::vector<std::string> string_numbers; std::getline(file, line); int length = std::stoi(line); std::getline(file, line); tokenize(line, ' ', string_numbers); std::vector<int> numbers; for (auto str_num : string_numbers) { numbers.push_back(std::stoi(str_num)); } std::vector<int> numbers1; //Do the algorithm int i = 0; int changed[6] = { -1, -1, -1, -1, -1, -1 }; /*int changed5 = -1; int changed6 = -1; int changed1 = -1; int changed2 = -1; int changed3 = -1; int changed4 = -1; */ numbers1 = numbers; if (numbers1[i + 1] > numbers1[i]) { changed[4] = 0; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = -(2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[4]; } } } i = 0; numbers1 = numbers; if (numbers1[i + 1] < numbers1[i]) { changed[5] = 0; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = (2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[5]; } } } i = 0; numbers1 = numbers; if (numbers1[i + 1] == numbers1[i]) { changed[0] = 0; changed[1] = 0; changed[2] = 0; changed[3] = 0; ++changed[0]; ++changed[1]; ++changed[2]; ++changed[3]; //4 possibilities: //first bigger i = 0; numbers1 = numbers; numbers1[i] = 2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = (2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[0]; } } //first smaller i = 0; numbers1 = numbers; numbers1[i] = -2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = -(2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[1]; } } //second bigger i = 0; numbers1 = numbers; numbers1[i + 1] = 2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = -(2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[2]; } } //second smaller i = 0; numbers1 = numbers; numbers1[i + 1] = -2'147'483'640; for (int i = 1; i < length - 1; ++i) { //for even i -1, for uneven i 1 int sign = (2 * (i % 2) - 1); if (sign * numbers1[i + 1] > sign * numbers1[i]) { ;//do nothing } else { numbers1[i + 1] = -2'147'483'640; //change to smallest possible (with some room) ++changed[3]; } } } /* std::cout << changed[0] << '\n'; std::cout << changed[1] << '\n'; std::cout << changed[2] << '\n'; std::cout << changed[3] << '\n'; std::cout << changed[4] << '\n'; std::cout << changed[5] << '\n'; */ std::vector<int> results; for (int n : changed) { if (n > -1) { results.push_back(n); } } std::sort(results.begin(), results.end()); std::cout << results[0]; } file.close(); } |