#include "cielib.h" #include <vector> std::vector<int>& operator+=(std::vector<int> &lhs, std::vector<int> const &rhs) { for (int i = 0; i < lhs.size(); ++i) { lhs[i] += rhs[i]; } return lhs; } std::vector<int>& operator/=(std::vector<int> &lhs, int val) { for (int i = 0; i < lhs.size(); ++i) { lhs[i] /= val; } return lhs; } std::vector<int>& operator+=(std::vector<int> &lhs, int val) { for (int i = 0; i < lhs.size(); ++i) { lhs[i] += val; } return lhs; } bool check_even(int dim, int low, int high, std::vector<int> &pos) { int tmp = pos[dim]; pos[dim] = low; czyCieplo(pos.data()); pos[dim] = high; int ret = czyCieplo(pos.data()); pos[dim] = tmp; return ret; } bool check_odd(int dim, int low, int high, std::vector<int> &pos_low, std::vector<int> &pos_high) { int tmp = pos_low[dim]; pos_low[dim] = low; czyCieplo(pos_low.data()); pos_low[dim] = tmp; tmp = pos_high[dim]; pos_high[dim] = high; int ret = czyCieplo(pos_high.data()); pos_high[dim] = tmp; return ret; } std::vector<int> bin_search(int dims) { std::vector<int> low(dims, 0), high(dims, podajR()), mid(dims), mid2(dims); while (high[0] - low[0] > 2) { mid = low; mid += high; mid /= 2; int ps = high[0] - low[0] + 1; if (ps % 2 == 0) { mid2 = mid; mid2 += 1; for (int d = 0; d < dims; ++d) { if (check_odd(d, low[d], high[d], mid, mid2)) { low[d] = mid[d]; } else { high[d] = mid2[d]; } } } else { for (int d = 0; d < dims; ++d) { if (check_even(d, low[d], high[d], mid)) { low[d] = mid[d]; } else { high[d] = mid[d]; } } } } if (high[0] - low[0] > 0) { mid = low; mid += high; mid /= 2; // ----- for (int d = 0; d < dims; ++d) { int tmp = mid[d]; mid[d] = low[d]; czyCieplo(mid.data()); mid[d] = high[d]; bool c1 = czyCieplo(mid.data()); mid[d] = low[d]; bool c2 = czyCieplo(mid.data()); mid[d] = tmp; // ----- if (!c1 && !c2) { low[d] = (low[d] + high[d]) / 2; } else if (!c1 && c2) { // low[d] = low[d]; } else { low[d] = high[d]; } } } return low; } int main() { int dims = podajD(); auto krotka = bin_search(dims); znalazlem(krotka.data()); 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 | #include "cielib.h" #include <vector> std::vector<int>& operator+=(std::vector<int> &lhs, std::vector<int> const &rhs) { for (int i = 0; i < lhs.size(); ++i) { lhs[i] += rhs[i]; } return lhs; } std::vector<int>& operator/=(std::vector<int> &lhs, int val) { for (int i = 0; i < lhs.size(); ++i) { lhs[i] /= val; } return lhs; } std::vector<int>& operator+=(std::vector<int> &lhs, int val) { for (int i = 0; i < lhs.size(); ++i) { lhs[i] += val; } return lhs; } bool check_even(int dim, int low, int high, std::vector<int> &pos) { int tmp = pos[dim]; pos[dim] = low; czyCieplo(pos.data()); pos[dim] = high; int ret = czyCieplo(pos.data()); pos[dim] = tmp; return ret; } bool check_odd(int dim, int low, int high, std::vector<int> &pos_low, std::vector<int> &pos_high) { int tmp = pos_low[dim]; pos_low[dim] = low; czyCieplo(pos_low.data()); pos_low[dim] = tmp; tmp = pos_high[dim]; pos_high[dim] = high; int ret = czyCieplo(pos_high.data()); pos_high[dim] = tmp; return ret; } std::vector<int> bin_search(int dims) { std::vector<int> low(dims, 0), high(dims, podajR()), mid(dims), mid2(dims); while (high[0] - low[0] > 2) { mid = low; mid += high; mid /= 2; int ps = high[0] - low[0] + 1; if (ps % 2 == 0) { mid2 = mid; mid2 += 1; for (int d = 0; d < dims; ++d) { if (check_odd(d, low[d], high[d], mid, mid2)) { low[d] = mid[d]; } else { high[d] = mid2[d]; } } } else { for (int d = 0; d < dims; ++d) { if (check_even(d, low[d], high[d], mid)) { low[d] = mid[d]; } else { high[d] = mid[d]; } } } } if (high[0] - low[0] > 0) { mid = low; mid += high; mid /= 2; // ----- for (int d = 0; d < dims; ++d) { int tmp = mid[d]; mid[d] = low[d]; czyCieplo(mid.data()); mid[d] = high[d]; bool c1 = czyCieplo(mid.data()); mid[d] = low[d]; bool c2 = czyCieplo(mid.data()); mid[d] = tmp; // ----- if (!c1 && !c2) { low[d] = (low[d] + high[d]) / 2; } else if (!c1 && c2) { // low[d] = low[d]; } else { low[d] = high[d]; } } } return low; } int main() { int dims = podajD(); auto krotka = bin_search(dims); znalazlem(krotka.data()); return 0; } |