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
import java.util.Arrays;

public class cie {

	public static void main(String[] args) {

		int dimension = cielib.podajD();
		int maxCoord = cielib.podajR();

		int[] leftBounds = new int[dimension];
		int[] rightBounds = new int[dimension];
		Arrays.fill(rightBounds, maxCoord);

		processBigCube(dimension, maxCoord, leftBounds, rightBounds);
		int[] krotkaPosition = processCubeWithSegment2(dimension, leftBounds, rightBounds);
		cielib.znalazlem(krotkaPosition);
	}

	private static void processBigCube(int dimension, int maxCoord, int[] leftBounds, int[] rightBounds) {
		int qubeSegment = maxCoord;
		int[] testedPosition = new int[dimension];
		boolean[] alreadyFound2Interval = new boolean[dimension];
		while (qubeSegment >= 2) {
			qubeSegment /= 2;
			boolean[] isRightBoundCloser = new boolean[dimension];
			for (int coord = 0; coord < dimension; ++coord) {
				testedPosition[coord] = leftBounds[coord] + qubeSegment;
			}
			for (int coord = 0; coord < dimension; ++coord) {
				if (!alreadyFound2Interval[coord]) {
					testedPosition[coord] = leftBounds[coord];
					cielib.czyCieplo(testedPosition);
					testedPosition[coord] = rightBounds[coord];
					if (cielib.czyCieplo(testedPosition) == 1) {
						isRightBoundCloser[coord] = true;
					} else {
						testedPosition[coord] = leftBounds[coord];
						if (cielib.czyCieplo(testedPosition) != 1) {
							leftBounds[coord] += qubeSegment;
							rightBounds[coord] = leftBounds[coord] + 1;
							alreadyFound2Interval[coord] = true;
						}
					}
					testedPosition[coord] = leftBounds[coord] + qubeSegment;
				}
			}
			for (int coord = 0; coord < dimension; ++coord) {
				if (isRightBoundCloser[coord] && !alreadyFound2Interval[coord]) {
					leftBounds[coord] = rightBounds[coord] - qubeSegment;
				} else {
					rightBounds[coord] = leftBounds[coord] + qubeSegment;
				}
			}
		}
	}

	private static int[] processCubeWithSegment2(int dimension, int[] leftBounds, int[] rightBounds) {
		int[] krotkaPosition = new int[dimension];
		int[] centers = new int[dimension];
		int[] testedPosition = new int[dimension];
		for (int coord = 0; coord < dimension; ++coord) {
			centers[coord] = (leftBounds[coord] > 0) ? leftBounds[coord] : rightBounds[coord];
			testedPosition[coord] = centers[coord];
		}
		for (int coord = 0; coord < dimension; ++coord) {
			if (leftBounds[coord] > 0) {
				testedPosition[coord] = leftBounds[coord] - 1;
				cielib.czyCieplo(testedPosition);
				testedPosition[coord] = rightBounds[coord];
				if (cielib.czyCieplo(testedPosition) == 1) {
					krotkaPosition[coord] = rightBounds[coord];
				} else {
					krotkaPosition[coord] = leftBounds[coord];
				}
			} else {
				testedPosition[coord] = rightBounds[coord] + 1;
				cielib.czyCieplo(testedPosition);
				testedPosition[coord] = leftBounds[coord];
				if (cielib.czyCieplo(testedPosition) == 1) {
					krotkaPosition[coord] = leftBounds[coord];
				} else {
					krotkaPosition[coord] = rightBounds[coord];
				}
			}
			testedPosition[coord] = centers[coord];
		}
		return krotkaPosition;
	}
}