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
#define DEBUG 0
#include "cielib.h"
#include <vector>
#include <queue>
#include <algorithm>
#if DEBUG
#include <cstdio>
#endif

using namespace std;

const int max_d = 500;

int minpos[max_d];
int maxpos[max_d];
int tmp[max_d];
int d, k;
int attempts;
bool looping[max_d];

static inline int range(const int p) {
	return maxpos[p] - minpos[p];
}

struct Cmp {
	bool operator()(const pair<int,int> &a, const pair<int, int> &b) {
		const int ra = range(a.first);
		const int rb = range(b.first);
		return ra < rb || (ra == rb && a.second > b.second);
	}
};

static inline void clear_loop() {
	for (int i = 0; i < d; i++)
		looping[i] = false;
}


static inline void update_center(int i) {
	tmp[i] = (maxpos[i] + minpos[i]) / 2;
	clear_loop();
}

static inline void update_center() {
	for (int i = 0; i < d; i++) {
		tmp[i] = (maxpos[i] + minpos[i]) / 2;
	}
	clear_loop();
}

int czyCieploLoc() {
	int res = czyCieplo(tmp);
	attempts++;
	if (attempts == k) {
		znalazlem(tmp);
		exit(0);
	}
	return res;
}

bool quasi_bsearch(const int r) {
//	priority_queue<pair<int,int>, vector<pair<int,int>>, Cmp> queue;
	queue<pair<int,int>> queue;

	for (int i = 0; i < d; i++) {
		minpos[i] = 0;
		maxpos[i] = r;
		queue.push(make_pair(i, 0));
	}

	// go to center
	update_center();
	while(!queue.empty()) {
		pair<int,int> p = queue.front(); //top();
		queue.pop();
		const int curr = p.first;
		if (maxpos[curr] == minpos[curr])
			continue;

		if (looping[curr])
			return false;
#if DEBUG
		printf("GOT %d (%d %d) = %d\n", curr, minpos[curr], maxpos[curr], maxpos[curr] - minpos[curr]);
#endif

		czyCieploLoc();
		const int oldcenter = tmp[curr];

		// move max
		tmp[curr] = maxpos[curr];
		if (czyCieploLoc()) {
			minpos[curr] = (maxpos[curr] + oldcenter) / 2 + 1;
#if DEBUG
			printf("min[%d] <- %d\n", curr, minpos[curr]);
#endif
			update_center(curr);
		} else {
			tmp[curr] = oldcenter;
			// backward
			if (czyCieploLoc()) {
				maxpos[curr] = (maxpos[curr] + tmp[curr]) / 2;
#if DEBUG
				printf("max[%d] <- %d\n", curr, maxpos[curr]);
#endif
				update_center(curr);
			} else {
				// move min from center
				tmp[curr] = minpos[curr];
				if (czyCieploLoc()) {
					maxpos[curr] = (minpos[curr] + oldcenter) / 2;
					update_center(curr);
#if DEBUG
					printf("max[%d] <- %d\n", curr, maxpos[curr]);
#endif
				} else {
					tmp[curr] = oldcenter;
					// backward
					if (czyCieploLoc()) {
						minpos[curr] = (tmp[curr] + minpos[curr]) / 2 + 1;
						update_center(curr);
#if DEBUG
						printf("min2[%d] = %d\n", curr, minpos[curr]);
#endif
					}
					else {
#if DEBUG
						printf("Nothing\n");
#endif
						looping[curr] = true;
					}
				}
			}
		}

		queue.push(make_pair(curr, 0));
	}
#if DEBUG
	printf("Queue on exit: %d\n", queue.empty() ? 0 : 1);
#endif
	return queue.empty();
}

void iterate_search() {
	for (int i = 0; i < d; i++) {
		if (maxpos[i] - minpos[i] > 0)
			tmp[i] = minpos[i];
	}
	czyCieploLoc();

	do {
		for (int i = 0; i < d; i++) {
			if (maxpos[i] - minpos[i] > 0) {
				if (tmp[i] == maxpos[i])
					tmp[i] = minpos[i];
				else {
					tmp[i] = maxpos[i];
					break;
				}
			}
		}
	} while(!czyCieploLoc());
}

int main() {
	d = podajD();
	attempts = 0;
	const int r = podajR();
	k = podajK();
	bool res = quasi_bsearch(r);

	if (!res) {
#if DEBUG
		printf("Starting iterate\n");
#endif
		iterate_search();
	}

	znalazlem(tmp);
	return 0;
}