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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* 2016
 * Maciej Szeptuch
 */
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <vector>

#define NDEBUG

#include "message.h"
#include "krazki.h"

typedef long long ll;

int node;
int nodes;
int pipe_height;
int discs;
int local_pipe_height;
int local_pipe_start;

void run_node(void);
void calculate_pipe_diameter(std::vector<ll> &local_pipe);
ll receive_upper_limit(void);
void recalculate_and_send_lower_limit(std::vector<ll> &local_pipe, ll upper_limit);
int try_pushing_discs_down(ll lower_limit, int start, int count);

int main(void)
{
    node    = MyNodeId();
    nodes   = NumberOfNodes();

    pipe_height = PipeHeight();
    discs       = NumberOfDiscs();

    nodes = std::min(nodes, std::min(pipe_height, discs));

    // Too many nodes
    if(node >= nodes)
        return 0;

#ifndef NDEBUG
    fprintf(stderr, "%d: FIRST\n", node);
    assert(nodes > 0 && "Invalid number of nodes");
    fprintf(stderr, "%d: Running node\n", node);
    fprintf(stderr, "%d:     Nodes:         %d\n", node, nodes);
    fprintf(stderr, "%d:     PipeHeight:    %d\n", node, pipe_height);
    fprintf(stderr, "%d:     Discs:         %d\n", node, discs);
#endif // NDEBUG
    run_node();
    return 0;
}

void run_node(void)
{
#ifndef NDEBUG
    fprintf(stderr, "%d: Gathering pipe info\n", node);
#endif // NDEBUG
    local_pipe_height = pipe_height / nodes;
    local_pipe_start = local_pipe_height * (nodes - node - 1);
    if(!node)
        local_pipe_height += pipe_height % nodes;

#ifndef NDEBUG
    fprintf(stderr, "%d:    local_pipe_height=%d\n", node, local_pipe_height);
    fprintf(stderr, "%d:    local_pipe_start=%d\n", node, local_pipe_start);

    assert(local_pipe_height >= 0);
    assert(local_pipe_start >= 0);
#endif // NDEBUG

    std::vector<ll> local_pipe(local_pipe_height);
    calculate_pipe_diameter(local_pipe);

    ll upper_limit = receive_upper_limit();
    recalculate_and_send_lower_limit(local_pipe, upper_limit);

#ifndef NDEBUG
    fprintf(stderr, "%d: Pushing discs through pipe\n", node);
#endif // NDEBUG

    int local_discs_count = discs / nodes;
    int local_discs_start = local_discs_count * node;
    if(!node)
        local_discs_count += discs % nodes;

#ifndef NDEBUG
    fprintf(stderr, "%d:    local_discs_count=%d\n", node, local_discs_count);
    fprintf(stderr, "%d:    local_discs_start=%d\n", node, local_discs_start);
#endif // NDEBUG

    int pull_up = local_discs_count;
    int push_down = 0;
    int l = 0;
    bool pushed_everything_down = true;
    int max_level = local_pipe_height;
    int air_gap = 0;
    while(pull_up > 0)
    {
        if(pushed_everything_down)
        {
            push_down = try_pushing_discs_down(local_pipe[local_pipe_height - 1], local_discs_start + l, pull_up);
            pushed_everything_down = push_down == pull_up;
            l += push_down;
        }

        if(!pushed_everything_down)
        {
#ifndef NDEBUG
            fprintf(stderr, "%d: Pushing discs into pipe\n", node);
#endif // NDEBUG
            while(max_level >= 0 && l < local_discs_count)
            {
                ll disc = DiscDiameter(1 + l + local_discs_start);
                -- max_level;
                while(max_level >= 0 && local_pipe[max_level] < disc)
                {
                    ++ air_gap;
                    -- max_level;
                }

#ifndef NDEBUG
                fprintf(stderr, "%d: disc %d, %lld landed on %d, %d\n", node, 1 + l + local_discs_start, disc, max_level, local_pipe_start + max_level + 1);
#endif // NDEBUG

                ++ l;
            }

#ifndef NDEBUG
            assert(air_gap >= 0);
            fprintf(stderr, "%d: Finished on disc=%d with max_level=%d air_gap=%d\n", node, l, max_level, air_gap);
#endif // NDEBUG
        }

        pull_up = 0;
        if(node + 1 < nodes)
        {
#ifndef NDEBUG
            fprintf(stderr, "%d: Waiting for pull_up from %d\n", node, node + 1);
#endif // NDEBUG
            Receive(node + 1);
            pull_up = GetInt(node + 1);
            local_discs_count += pull_up;
#ifndef NDEBUG
            fprintf(stderr, "%d: Got pull_up=%d from %d\n", node, pull_up, node + 1);
#endif // NDEBUG
        }
    }

    if(node && pushed_everything_down)
    {
#ifndef NDEBUG
        fprintf(stderr, "%d: There will be no more discs to push down, sending push_down=0 to %d\n", node, node - 1);
#endif // NDEBUG
        PutInt(node - 1, 0);
        Send(node - 1);
    }

#ifndef NDEBUG
    fprintf(stderr, "%d: Pre-finished on disc=%d with max_level=%d air_gap=%d\n", node, l, max_level, air_gap);
#endif // NDEBUG
    if(node)
    {
#ifndef NDEBUG
        fprintf(stderr, "%d: Waiting for pull_down from %d\n", node, node - 1);
#endif // NDEBUG
        Receive(node - 1);
        int pull_down = GetInt(node - 1);

#ifndef NDEBUG
        fprintf(stderr, "%d: Received pull_down=%d from %d\n", node, pull_down, node - 1);
#endif // NDEBUG

        pull_down -= std::min(pull_down, air_gap);
        max_level -= pull_down;
    }

    int addon = std::max(0, -max_level);
    if(node + 1 < nodes)
    {
#ifndef NDEBUG
        fprintf(stderr, "%d: Sending push_up=%d to %d\n", node, local_discs_count - l + addon, node + 1);
#endif // NDEBUG

        PutInt(node + 1, local_discs_count - l + addon);
        Send(node + 1);

    }

#ifndef NDEBUG
    fprintf(stderr, "%d: Finished on disc=%d with max_level=%d air_gap=%d\n", node, l, max_level, air_gap);
#endif // NDEBUG
    if(node + 1 == nodes)
    {
#ifndef NDEBUG
        assert(local_pipe_start == 0);
#endif // NDEBUG
        printf("%d\n", std::max(0, max_level + 1));
    }
}

    inline
void calculate_pipe_diameter(std::vector<ll> &local_pipe)
{
#ifndef NDEBUG
    fprintf(stderr, "%d: Calculating pipes diameter\n", node);
#endif // NDEBUG

    ll current = 1 << 30;
    for(int p = 0; p < local_pipe_height; ++ p)
    {
        current = local_pipe[p] = std::min(current, HoleDiameter(1 + local_pipe_start + p));
#ifndef NDEBUG
        fprintf(stderr, "%d:    local_pipe[%d] = %lld\n", node, p, local_pipe[p]);
#endif // NDEBUG
    }

}

    inline
ll receive_upper_limit(void)
{
    ll upper_limit = 1 << 30;
    if(node + 1 < nodes)
    {
#ifndef NDEBUG
        fprintf(stderr, "%d: Waiting for upper_limit from %d\n", node, node + 1);
#endif // NDEBUG
        Receive(node + 1);
        upper_limit = std::min(upper_limit, GetLL(node + 1));
#ifndef NDEBUG
        fprintf(stderr, "%d: Received upper_limit=%lld from %d\n", node, upper_limit, node + 1);
#endif // NDEBUG
    }

#ifndef NDEBUG
    fprintf(stderr, "%d: upper_limit=%lld\n", node, upper_limit);
#endif // NDEBUG
    return upper_limit;
}

    inline
void recalculate_and_send_lower_limit(std::vector<ll> &local_pipe, ll upper_limit)
{
#ifndef NDEBUG
    fprintf(stderr, "%d: Recalculating lower_limit\n", node);
#endif // NDEBUG

    if(local_pipe[local_pipe_height - 1] > upper_limit)
    {
        if(node)
        {
#ifndef NDEBUG
            fprintf(stderr, "%d: Sending lower_limit=%lld to %d\n", node, upper_limit, node - 1);
#endif // NDEBUG
            PutLL(node - 1, upper_limit);
            Send(node - 1);
        }

        for(int p = 0; p < local_pipe_height; ++ p)
        {
            local_pipe[p] = upper_limit;
#ifndef NDEBUG
            fprintf(stderr, "%d:    local_pipe[%d] = %lld\n", node, p, local_pipe[p]);
#endif // NDEBUG
        }
    }

    else
    {
        if(node)
        {
#ifndef NDEBUG
            fprintf(stderr, "%d: Sending lower_limit=%lld to %d\n", node, local_pipe[local_pipe_height - 1], node - 1);
#endif // NDEBUG
            PutLL(node - 1, local_pipe[local_pipe_height - 1]);
            Send(node - 1);
        }

        for(int p = 0; p < local_pipe_height && local_pipe[p] > upper_limit; ++ p)
            local_pipe[p] = upper_limit;

#ifndef NDEBUG
        for(int p = 0; p < local_pipe_height; ++ p)
        {
            fprintf(stderr, "%d:    local_pipe[%d] = %lld\n", node, p, local_pipe[p]);
        }
#endif // NDEBUG
    }
}

    inline
int try_pushing_discs_down(ll lower_limit, int start, int count)
{
    int pushed_down = 0;
    if(node)
    {
        while(pushed_down < count && DiscDiameter(1 + pushed_down + start) <= lower_limit)
            ++ pushed_down;

#ifndef NDEBUG
        fprintf(stderr, "%d: Have %d discs to push down, sending to %d\n", node, pushed_down, node - 1);
#endif // NDEBUG

        PutInt(node - 1, pushed_down);
        Send(node - 1);

        if(pushed_down && pushed_down != count)
        {
#ifndef NDEBUG
            fprintf(stderr, "%d: There will be no more discs to push down, sending push_down=0 to %d\n", node, node - 1);
#endif // NDEBUG
            PutInt(node -1, 0);
            Send(node - 1);
        }
    }

#ifndef NDEBUG
    fprintf(stderr, "%d: Pushed down %d discs\n", node, pushed_down);
#endif // NDEBUG
    return pushed_down;
}