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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "message.h"
#include "krazki.h"

#include <climits>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int main() {
  int n = PipeHeight();
  int m = NumberOfDiscs();

  // Handle the obvious case of m>n.
  if (m > n) {
    if (MyNodeId() == 0) {
      printf("0\n");
    }
    return 0;
  }

  // If n is small (smaller than the number of computing instances), just
  // calculate the result locally in task 0.
  if (n < NumberOfNodes() - 1) {
    if (MyNodeId() == 0) {
      long long *r = new long long[n];
      long long *k = new long long[m];
      long long *mm = new long long[n];

      for (int i = 0; i < n; i++) {
        r[i] = HoleDiameter(i + 1);

        if (i == 0) {
          mm[i] = r[i];
        } else {
          mm[i] = min(mm[i - 1], r[i]);
        }
      }
      for (int i = 0; i < m; i++) {
        k[i] = DiscDiameter(i + 1);
      }

      int it = n - 1;
      for (int i = 0; i < m; i++) {
        while (it >= 0 && mm[it] < k[i]) {
          it--;
        }

        if (it < 0) {
          printf("0\n");
          break;
        }

        //printf("Putting disc %d on pos %d.\n", i + 1, it + 1);

        mm[it] = 0;
      }

      printf("%d\n", it + 1);

      delete[] r;
      delete[] k;
      delete[] mm;
    }
    return 0;
  }

  // Here follows the actual logic of the distribution solution.
  if (MyNodeId() == 0) /* Master task logic. */ {
    // Load all local diameter minimals from the tasks.
    int tasks_done = 0;
    vector<long long> r_mins(NumberOfNodes());

    r_mins[0] = LLONG_MAX;

    while (tasks_done < NumberOfNodes() - 1) {
      int source = Receive(-1);
      long long r_min = GetLL(source);
      r_mins[source] = r_min;

      //printf("[MASTER] r_min for task %d: %lld\n", source, r_min);

      tasks_done++;
    }

    // Create a list of partial minimal diameters and send it to the tasks.
    vector<long long> r_min_partials(NumberOfNodes());
    r_min_partials[0] = LLONG_MAX;
    for (int i = 1; i < NumberOfNodes(); i++) {
      r_min_partials[i] = min(r_min_partials[i - 1], r_mins[i]);
      //printf("[MASTER] Min partial %d. %lld\n", i, r_min_partials[i]);
    }

    for (int i = 1; i < NumberOfNodes(); i++) {
      for (int j = 0; j < r_min_partials.size(); j++) {
        PutLL(i, r_min_partials[j]);
      }
      Send(i);
    }

    // Load all disk diameter maxes from the tasks.
    int k_workers = min(m, NumberOfNodes() - 1);
    vector<long long> k_maxes(k_workers + 1);

    k_maxes[0] = 0;

    tasks_done = 0;
    while (tasks_done < k_workers) {
      int source = Receive(-1);
      int k_max = GetLL(source);
      k_maxes[source] = k_max;

      tasks_done++;
    }

    // Create a list of partial maximum diameters and send it to the tasks.
    vector<long long> k_max_partials(k_workers + 1);
    k_max_partials[0] = 0;
    for (int i = 1; i <= k_workers; i++) {
      k_max_partials[i] = max(k_max_partials[i - 1], k_maxes[i]);
    }

    for (int i = 1; i <= k_workers; i++) {
      PutLL(i, k_max_partials[i - 1]);
      Send(i);
    }

    // Receive the index of the first recomputed max from each task.
    vector<int> first_max_offsets(k_workers + 1);
    tasks_done = 0;
    while (tasks_done < k_workers) {
      int source = Receive(-1);
      int offset = GetInt(source);
      first_max_offsets[source] = offset;

      //printf("[MASTER] Received %d as first max offset from %d.\n", offset, source);

      tasks_done++;
    }

    // Iterate from the back and send the index of the next max to each task.
    int next_max_offset = -1;
    for (int i = k_workers; i > 0; i--) {
      PutInt(i, next_max_offset);
      Send(i);

      if (first_max_offsets[i] != -1) {
        next_max_offset = first_max_offsets[i];
      }
    }

    // Read and combine the results of final worker assignments.
    map<int, pair<int, int> > per_worker_minmax;
    tasks_done = 0;
    while (tasks_done < k_workers) {
      int source = Receive(-1);
      int entries = GetInt(source);

      while (entries--) {
        int worker = GetInt(source);
        int min_value = GetInt(source);
        int max_value = GetInt(source);

        if (per_worker_minmax.find(worker) == per_worker_minmax.end()) {
          per_worker_minmax[worker] = make_pair(min_value, max_value);
        } else {
          per_worker_minmax[worker].first = min(per_worker_minmax[worker].first, min_value);
          per_worker_minmax[worker].second = max(per_worker_minmax[worker].second, max_value);
        }
      }

      tasks_done++;
    }

    /*printf("[MASTER] ===================================\n");
    for (auto minmax : per_worker_minmax) {
      printf("[MASTER] %d ---> (%d, %d)\n", minmax.first, minmax.second.first, minmax.second.second);
    }*/

    // Send the work assignments to tasks.
    for (int i = 1; i < NumberOfNodes(); i++) {
      if (per_worker_minmax.find(i) != per_worker_minmax.end()) {
        PutInt(i, per_worker_minmax[i].first);
        PutInt(i, per_worker_minmax[i].second);
      } else {
        PutInt(i, -1);
        PutInt(i, -1);
      }
      Send(i);
    }

    // Receive all results.
    vector<pair<int, int> > results(NumberOfNodes());
    tasks_done = 0;
    while (tasks_done < NumberOfNodes() - 1) {
      int source = Receive(-1);
      int free_space = GetInt(source);
      int extra = GetInt(source);
      //printf("[MASTER] Got free space: %d, extra: %d from %d.\n", free_space, extra, source);

      results[source] = make_pair(free_space, extra);

      tasks_done++;
    }

    int sum = 0;
    for (int i = NumberOfNodes() - 1; i > 0; i--) {
      //printf("[MASTER] i: %d, sum: %d\n", i, sum);

      if (results[i].second != 0) {
        if (sum < 0) {
          sum = 0;
        }
        sum += results[i].second;
      }

      if (i != 1) {
        sum += results[i - 1].first;
      }
    }

    if (sum > 0) {
      printf("0\n");
    } else {
      printf("%d\n", -sum + 1);
    }
  } else /* Worker task logic. */ {
    // Calculate the N ranges per each task.
    int start = 1 + (n * (MyNodeId() - 1)) / (NumberOfNodes() - 1);
    int end;
    if (MyNodeId() != NumberOfNodes() - 1) {
      end = ((n * MyNodeId()) / (NumberOfNodes() - 1));
    } else {
      end = n;
    }

    //printf("[%d] start: %d, end: %d\n", MyNodeId(), start, end);

    // Load the corresponding diameters.
    vector<long long> r;
    for (int i = start; i <= end; i++) {
      r.push_back(HoleDiameter(i));
    }

    // Calculate local minimals.
    vector<long long> r_mins;
    long long cur_min = LLONG_MAX;
    for (int i = 0; i < r.size(); i++) {
      if (r[i] < cur_min) {
        r_mins.push_back(i);
        cur_min = r[i];
      }
    }

    // Send the local minimum to the master.
    PutLL(0, cur_min);
    Send(0);

    // Receive the partial mins from the master.
    vector<long long> r_min_partials(NumberOfNodes() + 1);
    r_min_partials[NumberOfNodes()] = 0;

    Receive(0);
    for (int i = 0; i < NumberOfNodes(); i++) {
      r_min_partials[i] = GetLL(0);
    }

    long long partial_min = r_min_partials[MyNodeId() - 1];
    
    /*printf("--------------------------------");
    printf("[%d] Received partial min: %lld\n", MyNodeId(), partial_min);
    printf("[%d] Number of mins before: %lu\n", MyNodeId(), r_mins.size());*/

    // Remove insignificant mins.
    auto r_mins_it = r_mins.begin();
    while (r_mins_it != r_mins.end() && partial_min <= r[*r_mins_it]) {
      r_mins_it++;
    }
    r_mins.erase(r_mins.begin(), r_mins_it);

    //printf("[%d] Number of mins after: %lu\n", MyNodeId(), r_mins.size());
    /*for (int i = 0; i < r_mins.size(); i++) {
      printf("[%d] %d. %lld\n", MyNodeId(), i + 1, r[r_mins[i]]);
    }*/

    // Calculate the M ranges per each task.
    int k_start, k_end;
    if (m < NumberOfNodes() - 1) {
      if (MyNodeId() <= m) {
        k_start = MyNodeId();
        k_end = MyNodeId();
      } else {
        k_start = k_end = -1;
      }
    } else {
      k_start = 1 + (m * (MyNodeId() - 1)) / (NumberOfNodes() - 1);
      if (MyNodeId() != NumberOfNodes() - 1) {
        k_end = ((m * MyNodeId()) / (NumberOfNodes() - 1));
      } else {
        k_end = m;
      }
    }

    //printf("[%d] k_start: %d, k_end: %d\n", MyNodeId(), k_start, k_end);

    // Load the corresponding diameters.
    if (k_start != -1 && k_end != -1) {
      //printf("--------------------------------");

      vector<long long> k;
      for (int i = k_start; i <= k_end; i++) {
        k.push_back(DiscDiameter(i));
      }

      // Calculate local maxes.
      vector<int> k_maxes;
      long long cur_max = 0;
      for (int i = 0; i < k.size(); i++) {
        if (k[i] > cur_max) {
          k_maxes.push_back(i);
          cur_max = k[i];
        }
      }

      // Send the local max to the master.
      PutLL(0, cur_max);
      Send(0);

      // Get the partial max back for recalculation.
      Receive(0);
      long long k_partial_max = GetLL(0);

      /*printf("[%d] Received partial max: %lld\n", MyNodeId(), k_partial_max);
      printf("[%d] Number of maxes before: %lu\n", MyNodeId(), k_maxes.size());*/

      // Remove insignificant maxes.
      auto k_maxes_it = k_maxes.begin();
      while (k_maxes_it != k_maxes.end() && k_partial_max >= k[*k_maxes_it]) {
        k_maxes_it++;
      }
      k_maxes.erase(k_maxes.begin(), k_maxes_it);

      /*printf("[%d] Number of maxes after: %lu\n", MyNodeId(), k_maxes.size());
      for (int i = 0; i < k_maxes.size(); i++) {
        printf("[%d] %d. %d\n", MyNodeId(), i + 1, k[k_maxes[i]]);
      }*/

      // Send the index of the first local max to the master, or -1, if there
      // are none.
      if (!k_maxes.empty()) {
        PutInt(0, k_start + k_maxes[0]);
      } else {
        PutInt(0, -1);
      }
      Send(0);

      // Receive the index of the next offset (handled by a subsequent worker).
      Receive(0);
      int next_max_offset = GetInt(0);

      //printf("[%d] Next max offset: %d\n", MyNodeId(), next_max_offset);

      // Handle our local maxes, if we have any.
      //printf("-------------------------------------------\n");
      if (!k_maxes.empty()) {
        // Create a list of <max offset, sequence length, worker>.
        map<int, pair<int, int> > per_worker_minmax;

        int worker_it = NumberOfNodes();

        for (int i = 0; i < k_maxes.size(); i++) {
          int max_start, max_length, max_worker;

          if (i != k_maxes.size() - 1) {
            max_start = k_start + k_maxes[i];
            max_length = k_maxes[i + 1] - k_maxes[i];
          } else {
            if (next_max_offset != -1) {
              max_start = k_start + k_maxes[i];
              max_length = next_max_offset - (k_start + k_maxes[i]);
            } else {
              max_start = k_start + k_maxes[i];
              max_length = (m + 1) - (k_start + k_maxes[i]);
            }
          }

          while (r_min_partials[worker_it - 1] < k[k_maxes[i]]) {
            worker_it--;
          }
          if (worker_it == NumberOfNodes()) {
            max_worker = worker_it - 1;
          } else {
            max_worker = worker_it;
          }

          if (per_worker_minmax.find(max_worker) == per_worker_minmax.end()) {
            per_worker_minmax[max_worker] = make_pair(max_start, max_start + max_length);
          } else {
            per_worker_minmax[max_worker].first = min(per_worker_minmax[max_worker].first, max_start);
            per_worker_minmax[max_worker].second = max(per_worker_minmax[max_worker].second, max_start + max_length);
          }

          /*printf("[%d] Local maxes += (%d, %d, %d)\n",
                 MyNodeId(), max_start, max_length, max_worker);*/
        }

        /*printf("[%d] Mins and maxes for workers:\n", MyNodeId());
        for (auto it : per_worker_minmax) {
          printf("[%d] Worker: %d, min: %d, max: %d\n", MyNodeId(), it.first, it.second.first, it.second.second);
        }*/

        // Send a summary of worker assignments to the master for recalculation.
        PutInt(0, per_worker_minmax.size());
        for (auto it : per_worker_minmax) {
          PutInt(0, it.first);
          PutInt(0, it.second.first);
          PutInt(0, it.second.second);
        }
        Send(0);
      } else {
        // If there are no maxes, nothing to send.
        PutInt(0, 0);
        Send(0);
      }
    }

    // Receive instructions about work from master.
    Receive(0);
    int disc_min = GetInt(0);
    int disc_max = GetInt(0);

    if (disc_min == -1 && disc_max == -1) {
      PutInt(0, -((end + 1) - start)); // Lots of free space at the beginning.
      PutInt(0, 0);                    // No data sticks out.
      Send(0);
    } else {
      // Prepare the regular partial minimum array.
      vector<long long> mm(end - start + 1);
      for (int i = start; i <= end; i++) {
        if (i == start) {
          mm[i - start] = r[i - start];
        } else {
          mm[i - start] = min(mm[i - start - 1], r[i - start]);
        }
      }

      long long disc_val = DiscDiameter(disc_min);
      int pos;

      if (r_mins.empty() || r[r_mins.back()] >= disc_val) {
        pos = end - start;
      } else {
        pos = r_mins.back() - 1;
      }

      //printf("[%d] Starting pos: %d (%lld)\n", MyNodeId(), pos, r[pos]);

      int disc;
      int free_space = end - start + 1;
      for (disc = disc_min; disc < disc_max; disc++) {
        while (pos >= 0 && mm[pos] < DiscDiameter(disc)) {
          pos--;
        }

        if (pos < 0) {
          break;
        }

        if (disc == disc_min) {
          free_space = end - (pos + start);
        }

        //printf("Putting disc %d on pos %d.\n", disc, start + pos);

        mm[pos] = 0;
      }

      int remaining;
      if (disc == disc_max) {
        remaining = -pos;
      } else {
        remaining = disc_max - disc;
      }

      //printf("[%d] Free space: %d, extra: %d (%d - %d)\n", MyNodeId(), free_space, disc_max - disc, disc_max, disc);
      PutInt(0, -free_space);
      PutInt(0, remaining);
      Send(0);
    }
  }

  return 0;
}