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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <cstdlib>
#include <iostream>
#include <limits>

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

struct DiscDescribe
{
    int id;
    long long size;
};

struct HoleDescribe
{
    int id;
    long long size;
};

struct MinMax
{
    int min;
    int max;
};

int main() {
    int myNodeId = MyNodeId();
    int numberOfNodes = NumberOfNodes();
    int pipeHeight = PipeHeight();
    int numberOfDiscs = NumberOfDiscs();
    if(pipeHeight < numberOfNodes)
        numberOfNodes = pipeHeight;

    if(numberOfDiscs > pipeHeight)
    {
        if(myNodeId == 0)
            std::cout << 0 << std::endl;
        return EXIT_SUCCESS;
    }

    if(myNodeId >= numberOfNodes)
        return EXIT_SUCCESS;

    int beginDisc = (1L * numberOfDiscs * myNodeId) / numberOfNodes + 1;
    int endDisc = (1L * numberOfDiscs * (myNodeId + 1)) / numberOfNodes;
    int beginPipe = (1L * pipeHeight * myNodeId) / numberOfNodes + 1;
    int endPipe = (1L * pipeHeight * (myNodeId + 1)) / numberOfNodes;

//    std::cout << beginDisc << endDisc << beginPipe << endPipe << std::endl;

    int minSectionID = 0;
    long long minSection = std::numeric_limits<long long>::max();
    int maxDiscID = 0;
    long long maxDisc = 0L;

    DiscDescribe* discDescribe = new DiscDescribe[numberOfNodes];
    HoleDescribe* holeDescribe = new HoleDescribe[numberOfNodes];
    MinMax* minMax = new MinMax[numberOfNodes];

    if(beginPipe <= endPipe)
    {
        for(int i = endPipe; i >= beginPipe; --i)
        {
            long long hole = HoleDiameter(i);
            if(minSection > hole)
            {
                minSection = hole;
                minSectionID = i;
            }
        }
    }

    if(beginDisc <= endDisc)
    {
        for(int i = beginDisc; i <= endDisc; ++i)
        {
            long long disc = DiscDiameter(i);
            if(maxDisc < disc)
            {
                maxDisc = disc;
                maxDiscID = i;
            }
        }
    }
    if(beginDisc <= endDisc)
        discDescribe[myNodeId].id = maxDiscID;
    else
        discDescribe[myNodeId].id = -1;
    discDescribe[myNodeId].size = maxDisc;
    holeDescribe[myNodeId].id = minSectionID;
    holeDescribe[myNodeId].size = minSection;

    // send to all
    for(int i = 0; i < numberOfNodes; i++)
    {
        if(i == myNodeId)
            continue;
//        PutInt(i, myNodeId);
        PutInt(i, minSectionID);
        PutLL(i, minSection);
        PutInt(i, maxDiscID);
        PutLL(i, maxDisc);
        Send(i);
    }

    // receive from all

    for(int i = 0; i < numberOfNodes - 1; i++)
    {
        int id = Receive(-1);
        discDescribe[id].id = GetInt(id);
        discDescribe[id].size = GetLL(id);
        holeDescribe[id].id = GetInt(id);
        holeDescribe[id].size = GetLL(id);
    }


    for(int i = 0; i < numberOfNodes - 1; i++)
    std::cout << discDescribe[i].id  <<  " " << discDescribe[i].size <<  " " << holeDescribe[i].id <<  " "<< holeDescribe[i].size << std::endl;

    int min = beginDisc;
    int mid = 0;
    int max = endDisc;

    int nodeNumber;
    for(nodeNumber = numberOfNodes - 1; nodeNumber >= 0; --nodeNumber)
    {
        if(holeDescribe[nodeNumber].size < maxDisc)
        {
            mid = holeDescribe[nodeNumber].id + 1;
            break;
        }
    }

    if(mid != 0)
    {
        min = mid;
        int minInNodeDisc = (1L * numberOfDiscs * nodeNumber) / numberOfNodes + 1;
        int maxInNodeDisc = (1L * numberOfDiscs * (nodeNumber + 1)) / numberOfNodes;
        int discID = discDescribe[nodeNumber].id;
        int wdol = discID - minInNodeDisc;
        int wgore = maxInNodeDisc - discDescribe[nodeNumber].id;
        for(int i = 1; i <= wdol; i++)
        {
            if(DiscDiameter(discID - i) > HoleDiameter(mid - i))
            {
                mid += 1;
            }
            else
                min -= 1;
        }
        max = mid + wgore;
    }
    if(discDescribe[myNodeId].id < 0)
        minMax[myNodeId].min = -1;
    else
        minMax[myNodeId].min = min;
    minMax[myNodeId].max = max;

    if(0 != myNodeId)
    {
        // send to 0
        PutInt(0, min);
        PutInt(0, max);
        Send(0);

        int id = Receive(-1);
        if(id == 0)
            return EXIT_SUCCESS;
    }
    else
    {
        // receive from all
        for(int i = 1; i < numberOfNodes; i++)
        {
            int id = Receive(-1);
            minMax[myNodeId].min = GetInt(id);
            minMax[myNodeId].max = GetInt(id);
        }

        // send to all
        for(int i = 1; i < numberOfNodes; i++)
        {
            PutInt(i, 0);
            Send(i);
        }
    }

    int globalMin = 0;
    int globalMax = 0;
    for(int i = 0; i < numberOfNodes; i++)
    {
        std::cout << minMax[i].min << " " << minMax[i].max << std::endl;
        if(minMax[i].min < 0)
            continue;
        int distance = minMax[i].max - minMax[i].min + 1;
        if(globalMax > minMax[i].min)
            globalMax += distance;
        else
            globalMax = minMax[i].max;
    }

    if(globalMax > pipeHeight)
        std::cout << 0 << std::endl;
    else
        std::cout << (pipeHeight - globalMax + 1) << std::endl;

    return EXIT_SUCCESS;
}