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

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

int main()
{
    int depth;
    int m = NumberOfDiscs(); 
    int n = PipeHeight();    
    int * jump = new int [n+1];
    int mn = m/NumberOfNodes(), mo;
    int i=1+MyNodeId()*mn, j=1;
    long long mit = HoleDiameter(1)+1;
    
    while(DiscDiameter(i) <= HoleDiameter(j)) {
        if(HoleDiameter(j) < mit) {
            jump[j]=0;
            mit = HoleDiameter(j);
        }
        else
            jump[j]=jump[j-1]+1;
        j++;
        if(j>n)
            break;
    }
    j-=2;
    
    if(MyNodeId()==NumberOfNodes()-1)
        mo = m-mn*NumberOfNodes()-1;
    else
        mo=0;
    
    for (i = 2+MyNodeId()*mn; i <= (MyNodeId()+1)*mn+1+mo; i++) {
        while(DiscDiameter(i) > HoleDiameter(j-jump[j]))    
            j--;
        j--;
    }
    j++;
    depth = j;
    
    if (MyNodeId() > 0) {
        PutInt(0, j);
        Send(0);
    }
    else {
        for (int inst = 1; inst < NumberOfNodes(); ++inst) {
            Receive(inst);
            j = GetInt(inst);
            mn = mn + mo;
            depth = std::min( depth-mn, j );
            
        }
        if (depth<0)
            depth=0;
        std::cout << depth << std::endl;
    }
    return EXIT_SUCCESS;
}