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

typedef long long i64;

const int N = 3000000 + 10;
const i64 INF = 1LL << 62;

i64 a[N];

int main() {
  const int n = PipeHeight(), m = NumberOfDiscs(), id = MyNodeId(), tot = NumberOfNodes(), size = (n + tot - 1) / tot;
  const int l = id * size + 1, r = std::min((id + 1) * size, n);
  if (l > n) return 0;
  const int cnt = r - l;
  for (int i = 0; i <= cnt; ++i) a[i] = HoleDiameter(i + l);
  i64 cur = *std::min_element(a, a + cnt + 1), pre = INF;
  if (id) {
    Receive(id - 1);
    pre = GetLL(id - 1);
  }
  if (r < n) {
    PutLL(id + 1, std::min(cur, pre));
    Send(id + 1);
  }
  a[0] = std::min(a[0], pre);
  for (int i = 1; i <= cnt; ++i) a[i] = std::min(a[i - 1], a[i]);
  int p = 1;
  if (r < n) {
    Receive(id + 1);
    p = GetInt(id + 1);
  }
  int q = cnt;
  while (p <= m) {
    i64 t = DiscDiameter(p);
    while (q >= 0 && a[q] < t) --q;
    if (q < 0) break;
    if (p++ == m) {
      printf("%d\n", l + q);
      break;
    }
    --q;
  }
  if (id) {
    PutInt(id - 1, p);
    Send(id - 1);
  } else if (p <= m) {
    puts("0");
  }
  return 0;
}