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
#include <stdio.h>
#include "message.h"
#include "krazki.h"

#define min(a,b) \
       ({ __typeof__ (a) _a = (a); \
          __typeof__ (b) _b = (b); \
          _a < _b ? _a : _b; })
#define max(a,b) \
       ({ __typeof__ (a) _a = (a); \
          __typeof__ (b) _b = (b); \
          _a > _b ? _a : _b; })

#define MAX_INSTANCES 100

int n, m, k, id, S, from, toN, toM;
long long maxDiscs[MAX_INSTANCES + 5];
long long minHoles[MAX_INSTANCES + 5];

int main() {

    k = NumberOfNodes();
    id = MyNodeId();
    n = PipeHeight();
    m = NumberOfDiscs();
    S = (max(n, m) + k - 1) / k;
    from = id * S + 1;
    toN = min(n, (id + 1) * S);
    toM = min(m, (id + 1) * S);

    long long maxDisc = 0;
    long long minHole = 2e18;

    for (int i = from; i <= toN; i++) {
        minHole = min(minHole, HoleDiameter(i));
    }
    for (int i = from; i <= toM; i++) {
        maxDisc = max(maxDisc, DiscDiameter(i));
    }

    for (int i = 0; i < k; i++) {
        PutLL(i, minHole);
        PutLL(i, maxDisc);
        Send(i);
    }

    for (int i = 0; i < k; i++) {
        Receive(i);
        minHoles[i] = GetLL(i);
        maxDiscs[i] = GetLL(i);
    }
    for (int i = 1; i < k; i++) {
        minHoles[i] = min(minHoles[i - 1], minHoles[i]);
        maxDiscs[i] = max(maxDiscs[i - 1], maxDiscs[i]);
    }

    long long exactDiam[toM - from + 1];
    long long curDiam = id == 0 ? 0 : maxDiscs[id - 1];
    for (int i = from; i <= toM; i++) {
        curDiam = max(curDiam, DiscDiameter(i));
        exactDiam[i - from] = curDiam;
    }
    int ptr = k - 1;
    while (ptr >= 0 && minHoles[ptr] < curDiam) {
        ptr--;
    }
    int ans = 2e9;
    if (ptr == k - 1) {
        ans = n - (m - from);
    } else {
        ptr++;
        int first = ptr * S + 1;
        int last = min(n, (ptr + 2) * S);
        for (int i = toM; i >= from; i--) {
            while (first <= last && exactDiam[i - from] <= HoleDiameter(first)) {
                first++;
            }
            ans = min(ans, first - (m - i + 1));
        }
    }

    PutInt(0, ans);
    Send(0);

    if (id == 0) {
        ans = 2e9;
        for (int i = 0; i < k; i++) {
            Receive(i);
            ans = min(ans, GetInt(i));
        }
        printf("%d\n", max(0, ans));
    }

    return 0;
}