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

#define LL long long

using namespace std;

LL tab[1100];
LL tabmin[1100];
LL holes[4000010];
LL val[4000010];
const LL INF = 2000000000000000000LL;

int main()
{
    int S = 200000000 / NumberOfNodes() + 1;
    int node = MyNodeId();
    int n = PipeHeight();
    int N = (n - 1) / S + 2;
    int r = NumberOfDiscs();

    if (node == 0)
    {
        tabmin[0] = INF;
        for (int i = 1; i < N; i++)
        {
            Receive(i);
            tab[i] = GetLL(i);
            tabmin[i] = min(tabmin[i - 1], tab[i]);
        }
        for (int i = 1; i < N; i++)
        {
            PutLL(i, tabmin[i - 1]);
            Send(i);
        }
        Receive(1);
        GetLL(1);
        GetLL(1);
        LL out = GetLL(1);
        printf("%lld\n", out);
    }
    else
    {
        if (node < N)
        {
            int pocz = (node - 1) * S + 1;
            int kon = min(n, node * S);

            for (int i = pocz; i <= kon; i++)
            {
                holes[i - pocz + 1] = HoleDiameter(i);
            }

            val[0] = INF;
            for (int i = pocz; i <= kon; i++)
            {
                val[i - pocz + 1] = min(val[i - pocz], holes[i - pocz + 1]);
            }

            PutLL(0, val[kon - pocz + 1]);
            Send(0);
            Receive(0);
            LL curval = GetLL(0);
            LL krid;
            LL krsize;
            LL out;
            if (node == N - 1)
            {
                krid = 1;
                krsize = DiscDiameter(1);
                out = 0;
            }
            else
            {
                Receive(node + 1);
                krid = GetLL(node + 1);
                krsize = GetLL(node + 1);
                out = GetLL(node + 1);
            }

            if (out == 0)
            {
                for (int i = kon; i >= pocz; i--)
                {
                    if (krsize > curval)
                        i = pocz - 1;
                    else
                    {
                        if (krsize <= min(curval, val[i - pocz + 1]))
                            if (krid < r)
                        {
                            krid++;
                            krsize = DiscDiameter(krid);
                        }
                        else out = i, i = pocz - 1;
                    }
                }
            }

            PutLL(node - 1, krid);
            PutLL(node - 1, krsize);
            PutLL(node - 1, out);
            Send(node - 1);
        }
    }
}