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
public class cie {

    static int callCounter = 0;
    static int treshold = 10;

    public static void main(String[] args) {
        int d = cielib.podajD();
        int t[] = new int[d];
        int r = cielib.podajR();
        int expectedCalls = cielib.podajK();
        int step;
        treshold = r/10;

        //GET CENTER
        czyCieplo(t);
        search(t, 0, r, 0, t.length);

        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < t.length; i++) {
                search(t, 0, r, i, i + 1);
            }
        }

        czyCieplo(t);
        step = r - t[1]/2;
        int indexOfMax = 0;
        boolean isIncrease = true;

        while(true) {
            for (int i = 0; i < t.length; i++) {

                if (t[i] < r) {
                    t[i] += 1;
                    if (czyCieplo(t) == 1) {
                        indexOfMax = i;
                        isIncrease = true;
                        break;
                    } else {
                        t[i] -= 1;
                    }
                }

                if (t[i] > 0) {
                    t[i] -= 1;
                    if (czyCieplo(t) == 1) {
                        indexOfMax = i;
                        isIncrease = false;
                        break;
                    } else {
                        t[i] += 1;
                    }
                }

                if (i == t.length - 1) {
                    cielib.znalazlem(t);
                    return;
                }
            }

            do {
                if (isIncrease) {
                    t[indexOfMax] += step;
                } else {
                    t[indexOfMax] -= step;
                }
            } while (czyCieplo(t) == 1);

            if (isIncrease) {
                t[indexOfMax] -= step;
            } else {
                t[indexOfMax] += step;
            }

            step = step/2;
        }
    }

    private static void search(int[] t, int begin, int step, int indexStart, int indexEndExclusive) {
        int top = 0;
        int middle = 0;
        int bottom = 0;

        int start = begin;
        int end = step;
        int center = (start + end) / 2;
        int currentStep = 2*step;

        do {
            for (int i = indexStart; i < indexEndExclusive; i++)
            {
                t[i] = end;
            }

            top = czyCieplo(t);

            for (int i = indexStart; i < indexEndExclusive; i++)
            {
                t[i] = center;
            }

            middle = czyCieplo(t);

            for (int i = indexStart; i < indexEndExclusive; i++)
            {
                t[i] = start;
            }

            bottom = czyCieplo(t);

            currentStep = currentStep / 2;

            if (bottom == 0 && middle == 1 && top == 0) {
                start = start + currentStep / 4;
                end = end - currentStep / 4;
            } else if (bottom == 0 && middle == 1 && top == 1) {
                start = center - currentStep / 4;
                end = center + currentStep / 4;
            } else if (bottom == 1 && middle == 1 && top == 0) {
                start = (center + start) / 2 - currentStep / 4;
                end = (center + start) / 2 + currentStep / 4;
            } else if (bottom == 1 && top != 1) {
                end = start + currentStep / 2;
            } else if (top == 1 && bottom != 1) {
                start = start + currentStep / 2;
            }

            center = (start + end) / 2;

        } while(currentStep > treshold);

        czyCieplo(t);

        for (int i = start; i <= end; i++) {
            for (int j = indexStart; j < indexEndExclusive; j++)
            {
                t[j] = i;
            }

            if (start == end) {
                break;
            }

            if (czyCieplo(t) == 0) {
                for (int j = indexStart; j < indexEndExclusive; j++)
                {
                    if (i < 1) {
                        t[j] = 0;
                    } else {
                        t[j] = i - 1;
                    }
                }

            }
        }
    }

    private static int czyCieplo(int[] t) {
        callCounter++;
        return cielib.czyCieplo(t);
    }
}