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

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

using namespace std;

void closeNodes();

int main() {

    int nodeId = MyNodeId();
    int numberOfNodes = NumberOfNodes();
    int numberOfWorkers = numberOfNodes - 1;
    int pipeHeight = PipeHeight();
    int searchTo = NumberOfDiscs() + 1;
    bool singleNode = numberOfNodes == 1;

    // Nodes > 0 do searching
    if (nodeId != 0) {
        while (true) {
            if (searchTo > 100) {
                int numberOfInvolvedNodes = min(10, min((searchTo / 100) + 1, numberOfWorkers));

                if (nodeId <= numberOfInvolvedNodes) {
                    int searchSize = searchTo / (numberOfInvolvedNodes);
                    int localSearchFrom = (nodeId - 1) * searchSize;
                    int localSearchTo = nodeId * searchSize;
                    if (localSearchFrom == 0) {
                        localSearchFrom = 1;
                    }
                    if (nodeId == numberOfInvolvedNodes) {
                        localSearchTo = searchTo;
                    }
                    // cout<<"Size: "<<searchSize<<" From: "<<localSearchFrom<< " To: "<<localSearchTo<<endl;

                    // Count max in <from, to)
                    long long discDiameter, maxDiscDiameter = 0;
                    int maxDiscPointer;
                    for (int i = localSearchFrom; i < localSearchTo; i++) {
                        discDiameter = DiscDiameter(i);
                        if (discDiameter > maxDiscDiameter) {
                            maxDiscDiameter = discDiameter;
                            maxDiscPointer = i;
                        }
                    }
                    PutLL(0, maxDiscDiameter);
                    PutInt(0, maxDiscPointer);
                    Send(0);
                }
            }
            Receive(0);
            searchTo = GetInt(0);

            if (searchTo == -1) {
                break;
            }
        }
        return EXIT_SUCCESS;
    }


    // Node 0
    long long maxDiscDiameterArr[numberOfNodes + 1];
    int maxDiscPointerArr[numberOfNodes + 1];

    int topDiscPos = pipeHeight + 1;
    int discsAbove = 0;
    int stuckAt = 1;

    while (true) {
        long long maxDiscDiameter = 0;
        int maxDiscPointer;

        if (searchTo > 100 && !singleNode) {
            int numberOfInvolvedNodes = min(10, min((searchTo / 100) + 1, numberOfWorkers));
            for (int i = 1; i <= numberOfInvolvedNodes; ++i) {
                Receive(i);
                maxDiscDiameterArr[i] = GetLL(i);
                maxDiscPointerArr[i] = GetInt(i);
            }
            for (int i = 1; i <= numberOfInvolvedNodes; ++i) {
                if (maxDiscDiameterArr[i] > maxDiscDiameter) {
                    maxDiscDiameter = maxDiscDiameterArr[i];
                    maxDiscPointer = maxDiscPointerArr[i];
               }
            }
        } else {
            for (int i = 1; i < searchTo; i++) {
                long long discDiameter = DiscDiameter(i);
                if (discDiameter > maxDiscDiameter) {
                    maxDiscDiameter = discDiameter;
                    maxDiscPointer = i;
                }
            }
        }

        int discsAboveLocal = searchTo - maxDiscPointer;

        discsAbove += discsAboveLocal;
        for (int i = stuckAt; i <= pipeHeight; i++) {
            if (HoleDiameter(i) < maxDiscDiameter) {
                topDiscPos = min(topDiscPos, (i - discsAbove));
                stuckAt = i;
                break;
            }
            if (i == pipeHeight) {
                // We reached the bottom so all smaller disks can reach it too
                if (discsAbove < NumberOfDiscs()) {
                    discsAbove = NumberOfDiscs();
                }
                topDiscPos = max(0, min(topDiscPos, (pipeHeight - discsAbove + 1)));
                cout << topDiscPos << endl;
                closeNodes();
                return EXIT_SUCCESS;
            }
        }

        if (searchTo == 1 || topDiscPos <= 0) {
            topDiscPos = max(0, topDiscPos);
            cout << topDiscPos << endl;
            closeNodes();
            return EXIT_SUCCESS;
        }

        searchTo = maxDiscPointer;

        if (searchTo > 100) {
            int numberOfInvolvedNodes = min(10, min((searchTo / 100) + 1, numberOfWorkers));
            for (int i = 1; i <= numberOfInvolvedNodes; ++i) {
                PutInt(i, searchTo);
                Send(i);
            }
        }
    }

    return EXIT_SUCCESS;
}

void closeNodes() {
    for (int i = 1; i < NumberOfNodes(); ++i) {
        PutInt(i, -1);
        Send(i);
    }
}