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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <cstdlib>
#include <iostream>
#include <climits>
#include <vector>
#include "krazki.h"
#include "message.h"
using namespace std;

long long id, maxId, n, m, k;
long long pstart, pend;  // of the pipe segment
vector<long long> pipe;  // width of the pipe on positions [pstart, pend]
long long dstart, dend;  // of the pipe segment
vector<long long> disc;  // width of the discs on positions [dstart, dend]

vector<long long> pipe_necks;  // width of pipe for all nodes at their pstart

void calcMin() {  // of the pipe width for [pstart, pend)
  long long mini_down = LLONG_MAX, x;
  for (long long i = pstart; i < pend; ++i) {
    x = min(HoleDiameter(i), mini_down);
    if (x < mini_down)
      mini_down = x;
    pipe.push_back(x);
  }
  long long mini_up = LLONG_MAX;
  if (id > 0) {
    Receive(id-1);
    mini_up = GetLL(id-1);
    if (mini_down > mini_up)
      mini_down = mini_up;
  }
  if (id < maxId - 1) {
    PutLL(id+1, mini_down);
    Send(id+1);
  }
  for (long long i = pstart; i < pend; ++i) {
    if (pipe[i-pstart] > mini_up) {
      pipe[i-pstart] = mini_up;
    }
    else
      break;
  }
}

void calcNecks() {
  if (id < maxId - 1) {
    PutLL(maxId-1, pipe[0]);
    Send(maxId-1);
    Receive(id+1);
    for (long long i = 0; i < maxId; ++i)
      pipe_necks.push_back(GetLL(id+1));
    if (id > 0) {
      for (long long i = 0; i < maxId; ++i)
        PutLL(id-1, pipe_necks[i]);
      Send(id-1);
    }
  }
  else {
    pipe_necks = vector<long long>(maxId);
    pipe_necks[maxId-1] = pipe[0];

    if (maxId > 1) {
      for (long long i = 1; i < maxId; ++i) {
        long long from = Receive(-1);
        pipe_necks[from] = GetLL(from);
      }

      for (long long i = 0; i < maxId; ++i) {
        PutLL(id-1, pipe_necks[i]);
      }
      Send(id-1);
    }
  }
}

void calcMax() {  // of the discs for [dstart, dend]
  long long x, maxi_up = 0;
  for (long long i = dstart; i < dend; ++i) {
    x = max(DiscDiameter(i), maxi_up);
    if (x > maxi_up)
      maxi_up = x;
    disc.push_back(x);
  }
  long long maxi_down = 0;
  if (id < maxId - 1) {
    Receive(id+1);
    maxi_down = GetLL(id+1);
    if (maxi_down < maxi_up)
      maxi_up = maxi_down;
  }
  if (id > 0) {
    PutLL(id-1, maxi_up);
    Send(id-1);
  }
  for (long long i = dstart; i < dend; ++i) {
    if (disc[i-dstart] < maxi_down) {
      disc[i-dstart] = maxi_down;
    }
    else
      break;
  }
}

void solve(long long s, long long e) {  // disc s do not fit in the lower level; all discs from [s, e) fit in this level (i.e. in the first row); e - s <= pstart - pend
  long long last, free = pend - pstart - 1, prev_last = 0;
  for (long long i = s; i < e; ++i) {
    while (free >= 0 && pipe[free] < DiscDiameter(i)) {
      --free;
    }
    if (free < 0) {
      free -= e - i;
      break;
    }
    else 
      --free;
  }
  last = -free - 1;

  if (id + 1 < maxId) {
    Receive(id+1);
    prev_last = GetLL(id+1);
    if (prev_last > 0 && last < prev_last + (e - s) + (pstart - pend))
      last = prev_last + (e - s) + (pstart - pend);
    else if (prev_last < 0 && s == e)
      last += prev_last;
  }
  if (id > 0) {
    PutLL(id - 1, last);
    Send(id - 1);
  }
  else {
    if (last > 0)
      printf("0\n");
    else 
      printf("%lld\n", -last + 1);
  }
}

long long findFor(long long i, long long beg) {  // first bigger than pipe_neck[i+1] from local positions >= beg
  long long end = dend - dstart, s;
  if (beg == end || pipe_necks[i+1] < disc[beg])
    return beg;
  while (beg + 1 < end) {  // searching first > in (beg, end]
    s = (beg + end)/2;
    if (pipe_necks[i+1] < disc[s]) {  // s is ok
      end = s;
    }
    else {
      beg = s;
    }
  }
  return end;
}

void findAndSolve() {
  vector<long long> breaks;
  if (id + 1 < maxId) {
    Receive(id+1);
    long long x;
    while (true) {
      x = GetInt(id+1);
      if (x == -1)
        break;

      breaks.push_back(x);
    } 
  }
  if (breaks.empty()) {
    breaks.push_back(dstart);
  }
  long long beg = 0;
  for (unsigned i = breaks.size(); i <= id; ++i) {
    long long x = findFor(id - i, beg);
    beg = x;
    if (x == dend - dstart) {  // not found
      if (id < maxId -1 && dend - breaks[i-1] > k) {
        breaks.push_back(breaks[i-1] + k);
      }
      break;
    }
    else {
      if (id < maxId - 1 && x + dstart - breaks[i-1] > k)
        x = breaks[i-1] - dstart + k;
      breaks.push_back(x + dstart);
    }
  }
  if (id > 0) {
    for (unsigned i = 1; i < breaks.size(); ++i) 
      PutInt(id-1, breaks[i]);
    PutInt(id-1, -1);
    Send(id-1);
  }
  long long a = breaks[0], b = dend;
  if (breaks.size() > 1)
    b = breaks[1];

  solve(a, b);
}

int main() {
  id = MyNodeId();
  maxId = NumberOfNodes();
  n = PipeHeight();
  m = NumberOfDiscs();

  if (n <= maxId)
    maxId = n;
  if (id >= n)
    return 0;
  if (m > n) {
    if (id == 0)
      printf("0\n");
    return 0;
  }

  k = n / maxId;  // TODO set k = n / maxId + 1 ??
  pstart = k * id + 1;
  pend = pstart + k;
  if (id == maxId - 1)
    pend = n + 1;
  calcMin();

  if (n - pstart + 2 <= m)
    dend = n - pstart + 2;
  else
    dend = m + 1;
  if (n - pend + 2 <= m)
    dstart = n - pend + 2;
  else
    dstart = m + 1;


  calcMax();

  calcNecks();

  findAndSolve();

  return 0;
}