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
// Solution to 3CIE
// Author: Michal Czuczman <michal.czuczman@gmail.com>
// created on Thu Nov 24 17:12:14 CET 2016

#include <iostream>
#include <vector>
#include <queue>
#include "cielib.h"

using namespace std;

struct Range {
	int min;
	int max;

	Range(int min_, int max_) : min(min_), max(max_) {}
	int size() const { return max - min; }
	int mid() const { return (min + max) >> 1; }
};

struct RangeComparison {
	const vector<Range>& ranges;

	RangeComparison(const vector<Range>& ranges_)
		: ranges(ranges_) {}

	bool operator() (const int& lhs, const int& rhs) const {
		return ranges[lhs].size() < ranges[rhs].size();
	}
};

class CieSolver {
	public:
		CieSolver(int num_dimensions, int r)
			: dims(num_dimensions, Range(0, r)),
				q(RangeComparison(dims)),
				p(num_dimensions, dims[0].mid())
		{
			for(int i = 0; i < (int)dims.size(); ++i) {
				q.push(i);
			}
		}

		void query(int i) {
			p[i] = dims[i].min;
			czyCieplo(&p[0]);
			p[i] = dims[i].max;
			if(czyCieplo(&p[0])) {
				dims[i].min = dims[i].mid() + 1;
			} else if(dims[i].size() & 1) {
				dims[i].max = dims[i].mid() + 1;
			} else {
				dims[i].max = dims[i].mid();
			}
			p[i] = dims[i].mid();
		}

		void solve() {
			while(q.size()) {
				int i = q.top();
				q.pop();
				//printf("Analyzing dimension %d of size %d [%d..%d]: ", i, dims[i].size(),
				//		dims[i].min, dims[i].max);
				if(dims[i].size() > 1) {
					query(i);
					if(dims[i].size() > 0) {
						q.push(i);
					}
				} else {
					if(dims[i].min > 0) {
						int lowered_min = --dims[i].min;
						query(i);
						if(dims[i].min == lowered_min) {
							++dims[i].min;
						}
					} else {
						p[i] = dims[i].max + 1;
						czyCieplo(&p[0]);
						p[i] = dims[i].min;
						if(czyCieplo(&p[0])) {
							dims[i].max = dims[i].min;
						} else {
							dims[i].min = dims[i].max;
						}
					}
					p[i] = dims[i].min;
				}
				//printf(" changed to [%d..%d]\n", dims[i].min, dims[i].max);
			}
			znalazlem(&p[0]);
		}
	private:
		vector<Range> dims;
		priority_queue<int, vector<int>, RangeComparison> q;
		vector<int> p;
};

int main()
{
	CieSolver cs(podajD(), podajR());
	cs.solve();
}