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
#include <cstdlib>
#include <algorithm>

#include "cielib.h"

static const unsigned int MAX_D = 500;
static int pos[MAX_D];
static int dir[MAX_D];
static int d, k, r;

inline static bool cieplo(int pozycja[] = pos)
{
    return czyCieplo(pozycja) == 1;
}

void maximizeCoord(int cnum) // Returns direction in which to seek the center
{
    int current = 0;
    int direction = 1;

    // printf("maximizeCoord(%d)\n", cnum);

    // Check if we can detect a maximum in +1 direction
    pos[cnum] = r;
    cieplo();
    pos[cnum] = r - 1;
    if (!cieplo()) {
        direction = -1;
    }

    for (int step = 1 << 29; step > 0; step >>= 1) {
        if (current + step > r) {
            continue;
        }

        if (direction == 1) {
            pos[cnum] = current + step;
            cieplo();
            pos[cnum] = current + step - 1;
        }
        else {
            pos[cnum] = r - current - step;
            cieplo();
            pos[cnum] = r - current - step + 1;
        }

        if (!cieplo()) {
            current += step;
        }
    }

    pos[cnum] = (direction == 1) ? current : r - current;
    dir[cnum] = -direction;
}

void maximizeDir()
{
    // printf("maximizeDir!\n");
    int mypos[MAX_D];
    int current = 0;

    int bound = r;
    for (int i = 0; i < d; i++) {
        bound = std::min(bound, (dir[i] == 1) ? r - pos[i] : pos[i]);
    }

    for (int step = 1 << 29; step > 0; step >>= 1) {
        if (current + step > bound) {
            continue;
        }

        for (int i = 0; i < d; i++) {
            mypos[i] = pos[i] + (current + step) * dir[i];
        }

        cieplo(mypos);

        for (int i = 0; i < d; i++) {
            mypos[i] -= dir[i];
        }

        if (!cieplo(mypos)) {
            current += step;
        }
    }

    for (int i = 0; i < d; i++) {
        mypos[i] = pos[i] + current * dir[i];
    }

    // printf("current: %d\n", current);

    znalazlem(mypos);
}

int main()
{
    d = podajD();
    k = podajK();
    r = podajR();

    if (d == 1) {
        // For one dimension, if we tried
        // to run maximizeCoord and then
        // bisect with maximizeDir, we
        // could run out of queries.
        // Fortunately, we can omit
        // the call to maximizeCoord.
        dir[0] = 1;
        pos[0] = 0;
    }
    else {
        for (int i = 0; i < d; i++) {
            pos[i] = r / 2;
        }

        for (int i = 0; i < d; i++) {
            maximizeCoord(i);
        }
    }

    // printf("dirvec:");
    // for (int i = 0; i < d; i++) {
    //     printf(" %d", dir[i]);
    // }
    // putchar('\n');

    // printf("corner:");
    // for (int i = 0; i < d; i++) {
    //     printf(" %d", pos[i]);
    // }

    // putchar('\n');

    maximizeDir();

    return 42;
}