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
181
182
183
184
185
186
187
188
189
190
191
192
#include "cielib.h"
typedef int I;

static const I MAXD = 500;
static I at_least[MAXD+1];
static I at_most[MAXD+1];
static I curr[MAXD+1];
static I work[MAXD+1];
static I dir[MAXD+1];

static inline bool cieplej(I i, I before, I after) {
        I saved = curr[i];
        curr[i] = before;
        czyCieplo(curr);
        curr[i] = after;
        I ret = czyCieplo(curr);
        curr[i] = saved;
        return ret;
}

static bool przesun_cieplej_prawo(I i) {
        I start = curr[i];
        I end = podajR()+1;
        for (;;) {
                I diff = (end - start) / 2;
                if (!diff)
                        return false;
                if (cieplej(i, start, start + diff)) {
                        curr[i] = start + diff;
                        return true;
                }
                end = start + diff;
        }
}

static bool przesun_cieplej_lewo(I i) {
        I start = curr[i];
        I end = -1;
        for (;;) {
                I diff = (start - end) / 2;
                if (!diff)
                        return false;
                if (cieplej(i, start, start - diff)) {
                        curr[i] = start - diff;
                        return true;
                }
                end = start - diff;
        }
}

static void blizej() {
	I D = podajD();
        I R = podajR();
	for (;;) {
		bool changed = false;
		for(I i=0; i<D; ++i) {
			// w prawo
                        if (curr[i] < R && cieplej(i, curr[i], curr[i] + 1)) {
                                curr[i] += 1;
                                while (przesun_cieplej_prawo(i));
                                changed = true;
                                continue;
                        }
			// w lewo
                        if (curr[i] > 0 && cieplej(i, curr[i], curr[i] - 1)) {
                                curr[i] -= 1;
                                while (przesun_cieplej_lewo(i));
                                changed = true;
                                continue;
                        }
		}
		if (!changed)
			break;
	}
}

static bool przesun_dogranicy_prawo(I i) {
        I start = curr[i];
        I end = podajR()+1;
        if (!cieplej(i, end-1, start)) {
                return false;
        }
        for (;;) {
                I diff = (end - start) / 2;
                if (!diff)
                        break;
                if (cieplej(i, start + diff, start)) {
                        end = start + diff;
                } else {
                        start += diff;
                }
        }
        curr[i] = start;
        return true;
}

static bool przesun_dogranicy_lewo(I i) {
        I start = curr[i];
        I end = -1;
        if (!cieplej(i, 0, start)) {
                return false;
        }
        for (;;) {
                I diff = (start - end) / 2;
                if (!diff)
                        break;
                if (cieplej(i, start - diff, start)) {
                        end = start - diff;
                } else {
                        start -= diff;
                }
        }
        curr[i] = start;
        return true;
}

static bool dalej() {
	I D = podajD();
        bool ret = true;
        for (I i=0; i<D; ++i) {
                if (przesun_dogranicy_prawo(i)) {
                        dir[i] = 0;
                } else if (przesun_dogranicy_lewo(i)) {
                        dir[i] = 1;
                } else {
                        ret = false;
                }
        }

        return ret;
}

static void copy(I* to, I* from) {
	I D = podajD();
        for (I i=0; i<D; ++i)
                to[i] = from[i];
}

static void przesun(I ile) {
	I D = podajD();
	I R = podajR();
        for (I i=0; i<D; ++i) {
                if (dir[i] == 0) {
                        curr[i] -= ile;
                        if (curr[i] < 0)
                                curr[i] = 0;
                } else if (dir[i] == 1) {
                        curr[i] += 1;
                        if (curr[i] > R)
                                curr[i] = R;
                }
        }
}

static void docelu() {
        for(;;) {
                czyCieplo(curr);
                copy(work, curr);
                przesun(1); //TODO to też log?

                if (!czyCieplo(curr)) {
                        copy(curr, work);
                        break;
                }
        }
}

int main() {
	I D = podajD();
	I R = podajR();

	for(I i=0; i<D; ++i) {
		at_least[i] = 0;
		at_most[i] = R;
		curr[i] = R/2;
                dir[i] = -1;
	}

        if (D == 1) {
                blizej();
        } else {
                for (;;) {
                        blizej();
                        bool r = dalej();
                        docelu();
                        if (r)
                                break;
                }
        }

        znalazlem(curr);
}