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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "krazki.h"
#include "message.h"
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;

typedef long long LL;
const int INF = 1000000000; const LL INFLL = LL(INF) * LL(INF);
template<class T> inline int size(const T&x) { return x.size(); }

int NODE;
int NNODES;

template<class T>
void Put(int node, const T&x) {
  static_assert(is_pod<T>(), "Put pod");
  const char *p = reinterpret_cast<const char*>(&x);
  for(size_t i=0;i<sizeof(T);++i) PutChar(node, p[i]);
}

template<class T>
void Put(int node, const vector<T> &x) {
  PutLL(node, x.size());
  for(const T&a : x) Put(node, a);
}

template<class T>
void SendTo(int node, const T&x) {
  Put(node, x);
  Send(node);
}

template<class T>
void Get(int node, T &x) {
  static_assert(is_pod<T>(), "Get pod");
  char *p = reinterpret_cast<char*>(&x);
  for(size_t i=0;i<sizeof(T);++i) p[i] = GetChar(node);
}

template<class T>
void Get(int node, vector<T> &x) {
  x.resize(GetLL(node));
  for(T &a : x) Get(node, a);
}

template<class T>
int ReceiveFrom(int node, T &x) {
  node = Receive(node);
  Get(node, x);
  return node;
}

/////////

template<class T>
class Array {
 public:
  explicit Array() : a(0), b(0), v() {}
  explicit Array(long long _a, long long _b) : a(_a), b(_b), v(_b - _a) {}
  void resize(long long _a, long long _b) {
    a = _a;
    b = _b;
    v.assign(_b - _a, T());
  }
  void assign(long long _a, long long _b, const T &x) {
    a = _a;
    b = _b;
    v.assign(_b - _a, x);
  }
  T &operator[](long long x) { return v[x-a]; }
  const T &operator[](long long x) const { return v[x-a]; }
  long long beginIdx() const { return a; }
  long long endIdx() const { return b; }
  typename vector<T>::const_iterator begin() const { return v.begin(); }
  typename vector<T>::iterator begin() { return v.begin(); }
  typename vector<T>::const_iterator end() const { return v.end(); }
  typename vector<T>::iterator end() { return v.end(); }
 private:
  long long a, b;
  vector<T> v;
};

LL numHoles;
LL numDiscs;

LL holeSize(LL x) {
  assert(x >= 0 && x < numHoles);
  return 2*HoleDiameter(numHoles - x)+1;
}

LL discSize(LL x) {
  assert(x >= 0 && x < numDiscs);
  return 2*DiscDiameter(1 + x);
}

struct Position {
  LL size;
  LL idx;
};

inline bool operator<(const Position &a, const Position &b) {
  if(a.size != b.size) return a.size < b.size;
  return a.idx < b.idx;
}

struct Job {
  Position positionStart;
  Position positionEnd;
  Position holeStart;
  Position holeEnd;
  Position discStart;
  Position discEnd;
};

vector<LL> equalSplit(const LL n, const int elems) {
  vector<LL> res(elems+1);
  FOR(i,0,elems) res[i] = i * n / elems;
  return res;
}

LL minHoleSize(const LL alpha, const LL beta) {
  LL res = std::numeric_limits<LL>::max();
  for(LL i=alpha;i<beta;++i) {
    LL x = holeSize(i);
    res = min(res, x);
  }
  return res;
}

LL maxDiscSize(const LL alpha, const LL beta) {
  LL res = 0;
  for (LL i=alpha;i<beta;++i) {
    LL x = discSize(i);
    res = max(res, x);
  }
  return res;
}

LL ComputeJob(const Job &job) {
  Array<LL> holeSizes(job.holeStart.idx, job.holeEnd.idx);
  {
    LL a = job.holeEnd.size;
    for(LL i=job.holeEnd.idx-1; i>=job.holeStart.idx; --i) {
      a = min(a, holeSize(i));
      holeSizes[i] = a;
    }
  }

  Array<LL> discSizes(job.discStart.idx, job.discEnd.idx);
  {
    LL a = job.discStart.size;
    for (LL i = job.discStart.idx; i < job.discEnd.idx; ++i) {
      a = max(a, discSize(i));
      discSizes[i] = a;
    }
  }

  LL holeIdx = job.holeStart.idx;

  LL maxRaise = 0;
  for (LL discIdx = job.discStart.idx; discIdx < job.discEnd.idx; ++discIdx) {
    while (holeIdx < job.holeEnd.idx && discSizes[discIdx] > holeSizes[holeIdx]) {
      ++holeIdx;
    }
    Position p;
    p.idx = discIdx;
    p.size = discSizes[discIdx];
    if (!(p < job.positionStart) && p < job.positionEnd) {
      LL raise = holeIdx - discIdx;
      maxRaise = max(maxRaise, raise);
    }
  }
  return maxRaise;
}

int main() {
  NNODES = NumberOfNodes();
  NODE = MyNodeId();
  numHoles = PipeHeight();
  numDiscs = NumberOfDiscs();
  if (NODE>=NNODES) return 0;

  const int NUM_PIECES = 10 * NNODES;
  vector<LL> holeCutpoints = equalSplit(numHoles, NUM_PIECES);
  vector<LL> discCutpoints = equalSplit(numDiscs, NUM_PIECES);

  // Phase 1: Compute min hole in each segments and max disc in each segment.
  {
    REP(i, size(holeCutpoints)-1) {
      if (i % NNODES == NODE) {
        const LL x = minHoleSize(holeCutpoints[i], holeCutpoints[i+1]);
        Put(0, x);
      }
    }
    REP(i, size(discCutpoints)-1) {
      if (i % NNODES == NODE) {
        const LL x = maxDiscSize(discCutpoints[i], discCutpoints[i+1]);
        Put(0, x);
      }
    }
    Send(0);
  }

  // Phase 2: Receive these messages at master, sort, distribute work.
  if (NODE == 0) {
    // Receive.
    REP(node, NNODES) {
      Receive(node);
    }
    vector<Position> holePositions;
    holePositions.reserve(size(holeCutpoints));
    REP(i, size(holeCutpoints)) {
      Position p;
      p.idx = holeCutpoints[i];
      if(i < size(holeCutpoints)-1) {
        Get(i % NNODES, p.size);
      } else {
        p.size = std::numeric_limits<LL>::max();
      }
      holePositions.push_back(p);
    }
    vector<Position> discPositions;
    discPositions.reserve(size(discCutpoints));
    REP(i, size(discCutpoints)) {
      Position p;
      p.idx = discCutpoints[i];
      if(i>0) {
        Get((i-1) % NNODES, p.size);
      } else {
        p.size = 0;
      }
      discPositions.push_back(p);
    }

    // Prefix min/max.
    FORD(i, size(holePositions)-2, 0) {
      holePositions[i].size = min(holePositions[i].size, holePositions[i+1].size);
    }

    FOR(i, 1, size(discPositions)-1) {
      discPositions[i].size = max(discPositions[i].size, discPositions[i-1].size);
    }

    // Dump all positions together and split work.
    vector<Position> allPositions = holePositions;
    for(const Position &p : discPositions) allPositions.push_back(p);
    sort(allPositions.begin(), allPositions.end());

    vector<LL> positionCutpoints = equalSplit(size(allPositions)-1, NNODES);
    REP(node, NNODES) {
      Job job;
      job.positionStart = allPositions[positionCutpoints[node]];
      job.positionEnd = allPositions[positionCutpoints[node+1]];

      auto holeIt = upper_bound(holePositions.begin(), holePositions.end(), job.positionStart);
      if (holeIt != holePositions.begin()) --holeIt;
      job.holeStart = *holeIt;

      holeIt = lower_bound(holePositions.begin(), holePositions.end(), job.positionEnd);
      if (holeIt == holePositions.end()) --holeIt;
      job.holeEnd = *holeIt;

      auto discIt = upper_bound(discPositions.begin(), discPositions.end(), job.positionStart);
      if (discIt != discPositions.begin()) --discIt;
      job.discStart = *discIt;

      discIt = lower_bound(discPositions.begin(), discPositions.end(), job.positionEnd);
      if (discIt == discPositions.end()) --discIt;
      job.discEnd = *discIt;

      SendTo(node, job);
    }
  }

  // Phase 3: Receive jobs and perform them.
  {
    Job job; ReceiveFrom(0, job);

    LL biggestHeightGain = ComputeJob(job);
    SendTo(0, biggestHeightGain);
  }

  // Phase 4: Summarize results and print.
  if (NODE == 0) {
    LL biggestHeightGain = 0;
    REP(node, NNODES) {
      LL x; ReceiveFrom(node, x);
      biggestHeightGain = max(biggestHeightGain, x);
    }
    LL res = numHoles - numDiscs + 1 - biggestHeightGain;
    if(res<0) res = 0;
    cout << res << '\n';
  }
}