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

using namespace std;

struct dysk
{
    int nr;
    long long r;
    long long deep;
};
long long abc = 0;
int n, m;
int f;
dysk disc[10000000];
long long wynik;

bool cmp_1(dysk x, dysk y)
{
    if(x.r > y.r)
        return true;
    else
        return false;
}

bool cmp_2(dysk x, dysk y)
{
    if(x.nr < y.nr)
        return true;
    else
        return false;
}

void sito()
{
    long long sher = 2000000000000000000;
    long long iter = 0;

    for(int i=1; i<=n; i++)
    {
        sher = min(sher,HoleDiameter(i));

        if(disc[iter].r > sher)
        {
            disc[iter].deep = i-1;
            iter++;
        }
    }
    if(disc[iter].r <= sher)
        disc[iter].deep = n;
}

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

    if(NumberOfNodes() <= m)
    {
        if(MyNodeId() == NumberOfNodes()-1)
            f = m;
        else
            f = ((MyNodeId()+1)*(m/NumberOfNodes()));


        for(long long i=1+(MyNodeId()*(m/NumberOfNodes())); i<=f; i++)
        {
            disc[j].r = DiscDiameter(i);
            disc[j].nr = j;
            j++;
        }
    }
    else
    {
        for(long long i=1; i<=m; i++)
        {
            disc[j].r = DiscDiameter(i);
            disc[j].nr = j;
            j++;
        }
    }


    sort(disc, disc+j, cmp_1);

    sito();

    sort(disc, disc+j, cmp_2);

    for(int i=1; i<j; i++)
        disc[i].deep = max(min(disc[i-1].deep-1,disc[i].deep),abc);

    wynik = disc[j-1].deep;

    if(NumberOfNodes() <= m)
    {
        if(MyNodeId() == 0)
        {
            for(int instance = 1; instance < NumberOfNodes(); instance++)
            {
                PutLL(instance,wynik);
                Send(instance);
                Receive(instance);
                wynik = GetLL(instance);
            }
            cout<<wynik;
        }
        else
        {
            Receive(0);
            wynik = GetLL(0);
            for(int i=0; i<j; i++)
                wynik = max(min(disc[i].deep,wynik-1),abc);
            PutLL(0,wynik);
            Send(0);
        }
    }
    else
    {
        if(MyNodeId() == 0)
            cout<<wynik;
    }

    return 0;
}