#include <cstdio> #include <string> #include <algorithm> #include <set> #include <vector> #include <iterator> #include "cielib.h" using namespace std; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define FOREACH(t, i,c) for(t::iterator i=(c).begin();i!=(c).end();++i) #define ZERO(a) memset(a,0,sizeof(a)) template<class T> inline int size(const T &c) { return c.size(); } bool arr_equals(int a[], int b[], int D) { for (int i=0; i < D; i++) { if (a[i] != b[i]) { return false; } } return true; } //#define K3_DEBUG #define DBG(X) //#define K3_TESTS int nosens_questions = 1; int czyCieploWrapper(int pozycja[], int dim) { static int *previous = NULL; if (!previous) { previous = new int[dim]; for (int i = 0; i < dim; i++) { previous[i] = pozycja[i]; } return 0; } if (arr_equals(previous, pozycja, dim)) { DBG(printf("Bezsensowne pytanie!\n")); nosens_questions++; return 0; } int res = czyCieplo(pozycja); for (int i = 0; i < dim; i++) { previous[i] = pozycja[i]; } return res; } void CPA(int* dist, int* src, int n) { for (int i = 0; i < n; i++) { dist[i] = src[i]; } } void print_arr(const char* msg, int a[], int dim) { printf("%s: ", msg); for (int i = 0; i < dim; i++) { printf("%03d ", a[i]); } printf("\n"); } int multidimension_binsearch() { int D = podajD(); int R = podajR(); int *low = new int[D]; for (int i = 0; i < D; i++) { low[i] = 0; } int *high = new int[D]; for (int i = 0; i < D; i++) { high[i] = R; } int* middle = new int[D]; for (int d = 0; d < D; d++) { middle[d] = (low[d] + high[d]) / 2; } czyCieploWrapper(middle, D); vector<pair<int,int> > perm; for (int i = 0; i < D * 10000; i++) { bool improvement_at_all = false; for (int d=0; d < D; d++) { #ifdef K3_DEBUG printf("************** Working on dimension %d\n", d); #endif bool was_improvement = true; int skip_next_question = false; int q[3] = {0,0,0}; for (int d = 0; d < D; d++) { middle[d] = (low[d] + high[d]) / 2; } while (was_improvement) { //int left = low[d] + (high[d] - low[d]) / 3; //int right = high[d] - (high[d] - low[d]) / 3; int left = low[d] + (high[d] - low[d]) / 3; int right = high[d] - (high[d] - low[d]) / 3; if (low[d] == high[d]) { break; } middle[d] = left; q[0] = czyCieploWrapper(middle, D); // left middle[d] = right; q[1] = czyCieploWrapper(middle, D); // right middle[d] = left; q[2] = czyCieploWrapper(middle, D); // left if (q[1]) { low[d] = (right + left) / 2; } if (q[2]) { high[d] = (right + left + 1) / 2 - 1; } was_improvement = q[1] + q[2]; DBG(print_arr("low", low, D)); DBG(print_arr("high", high, D)); skip_next_question = false; if (was_improvement) { improvement_at_all = true; } } } if (!improvement_at_all) { #ifdef K3_DEBUG printf("DONE!\n"); print_arr("low", low, D); print_arr("middle", middle, D); print_arr("high", high, D); printf("Questions asked=%d nosens_questions=%d\n", questions_asked_cnt, nosens_questions); print_arr("szukany_punkt orginalny: ", szukany_punkt, D); #endif int max_r = 0; for (int i=0; i < D; i++) { max_r = max(max_r, high[i] - low[i]); } if (max_r == 0) { break; } int* edge = new int[D]; int *low2 = new int[D]; CPA(edge, low, D); CPA(low2, low, D); // oblicz brzegi for (int i=0; i < D; i++) { edge[i] = low[i] - 1; czyCieploWrapper(edge, D); int q1 = czyCieploWrapper(low, D); if (q1) { DBG(printf("Found point at dimension %d = %d\n", i, high[i])); low2[i]++; } edge[i] = low[i]; } CPA(low, low2, D); #ifdef K3_DEBUG print_arr("New low", low, D); #endif } else { //printf("progres.."); } } // for loop #ifdef K3_DEBUG printf("output:\n"); for (int i=0; i < D; i++) { printf("%04d ", low[i]); } printf("\n"); for (int i=0; i < D; i++) { printf("%04d ", szukany_punkt[i]); } printf("\n"); for (int i=0; i < D; i++) { printf("%04d ", high[i]); } printf("dist=%d, %d\n", dist(szukany_punkt, low, D), dist(szukany_punkt, high, D)); #endif znalazlem(low); return 0; } int main() { multidimension_binsearch(); 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 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 | #include <cstdio> #include <string> #include <algorithm> #include <set> #include <vector> #include <iterator> #include "cielib.h" using namespace std; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define FOREACH(t, i,c) for(t::iterator i=(c).begin();i!=(c).end();++i) #define ZERO(a) memset(a,0,sizeof(a)) template<class T> inline int size(const T &c) { return c.size(); } bool arr_equals(int a[], int b[], int D) { for (int i=0; i < D; i++) { if (a[i] != b[i]) { return false; } } return true; } //#define K3_DEBUG #define DBG(X) //#define K3_TESTS int nosens_questions = 1; int czyCieploWrapper(int pozycja[], int dim) { static int *previous = NULL; if (!previous) { previous = new int[dim]; for (int i = 0; i < dim; i++) { previous[i] = pozycja[i]; } return 0; } if (arr_equals(previous, pozycja, dim)) { DBG(printf("Bezsensowne pytanie!\n")); nosens_questions++; return 0; } int res = czyCieplo(pozycja); for (int i = 0; i < dim; i++) { previous[i] = pozycja[i]; } return res; } void CPA(int* dist, int* src, int n) { for (int i = 0; i < n; i++) { dist[i] = src[i]; } } void print_arr(const char* msg, int a[], int dim) { printf("%s: ", msg); for (int i = 0; i < dim; i++) { printf("%03d ", a[i]); } printf("\n"); } int multidimension_binsearch() { int D = podajD(); int R = podajR(); int *low = new int[D]; for (int i = 0; i < D; i++) { low[i] = 0; } int *high = new int[D]; for (int i = 0; i < D; i++) { high[i] = R; } int* middle = new int[D]; for (int d = 0; d < D; d++) { middle[d] = (low[d] + high[d]) / 2; } czyCieploWrapper(middle, D); vector<pair<int,int> > perm; for (int i = 0; i < D * 10000; i++) { bool improvement_at_all = false; for (int d=0; d < D; d++) { #ifdef K3_DEBUG printf("************** Working on dimension %d\n", d); #endif bool was_improvement = true; int skip_next_question = false; int q[3] = {0,0,0}; for (int d = 0; d < D; d++) { middle[d] = (low[d] + high[d]) / 2; } while (was_improvement) { //int left = low[d] + (high[d] - low[d]) / 3; //int right = high[d] - (high[d] - low[d]) / 3; int left = low[d] + (high[d] - low[d]) / 3; int right = high[d] - (high[d] - low[d]) / 3; if (low[d] == high[d]) { break; } middle[d] = left; q[0] = czyCieploWrapper(middle, D); // left middle[d] = right; q[1] = czyCieploWrapper(middle, D); // right middle[d] = left; q[2] = czyCieploWrapper(middle, D); // left if (q[1]) { low[d] = (right + left) / 2; } if (q[2]) { high[d] = (right + left + 1) / 2 - 1; } was_improvement = q[1] + q[2]; DBG(print_arr("low", low, D)); DBG(print_arr("high", high, D)); skip_next_question = false; if (was_improvement) { improvement_at_all = true; } } } if (!improvement_at_all) { #ifdef K3_DEBUG printf("DONE!\n"); print_arr("low", low, D); print_arr("middle", middle, D); print_arr("high", high, D); printf("Questions asked=%d nosens_questions=%d\n", questions_asked_cnt, nosens_questions); print_arr("szukany_punkt orginalny: ", szukany_punkt, D); #endif int max_r = 0; for (int i=0; i < D; i++) { max_r = max(max_r, high[i] - low[i]); } if (max_r == 0) { break; } int* edge = new int[D]; int *low2 = new int[D]; CPA(edge, low, D); CPA(low2, low, D); // oblicz brzegi for (int i=0; i < D; i++) { edge[i] = low[i] - 1; czyCieploWrapper(edge, D); int q1 = czyCieploWrapper(low, D); if (q1) { DBG(printf("Found point at dimension %d = %d\n", i, high[i])); low2[i]++; } edge[i] = low[i]; } CPA(low, low2, D); #ifdef K3_DEBUG print_arr("New low", low, D); #endif } else { //printf("progres.."); } } // for loop #ifdef K3_DEBUG printf("output:\n"); for (int i=0; i < D; i++) { printf("%04d ", low[i]); } printf("\n"); for (int i=0; i < D; i++) { printf("%04d ", szukany_punkt[i]); } printf("\n"); for (int i=0; i < D; i++) { printf("%04d ", high[i]); } printf("dist=%d, %d\n", dist(szukany_punkt, low, D), dist(szukany_punkt, high, D)); #endif znalazlem(low); return 0; } int main() { multidimension_binsearch(); return 0; } |