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
#include <cstdlib>
#include <iostream>
#include <vector>
#include <set>

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

#define ll long long

const ll INF = 1LL << 62;

using namespace std;

int bs(vector<ll>& v, ll x, int from, int to) {
  int f = from;
  int t = to;
  while(t - f >= 2) {
    int mid = (f+t)/2;
    if(v[mid] < x) {
      t = mid;
    } else {
      f = mid;
    }
  }
  int k=-1;
  for(;;) {
    int g = f+k;
    if(g >= from && g < to) {
      if(v[g] < x) {
        return g-1;
      }
    } else if(g == to) {
      return g;
    }
    k++;
  }
}

void nonincr(vector<ll>& h) {
  ll max = INF;
  for(vector<ll>::iterator it = h.begin();it != h.end();it++) {
    if(*it < max) {
      max = *it;
    } else {
      *it = max;
    }
  }
}

int main() {
  int hc = PipeHeight();
  int dc = NumberOfDiscs();
  int n = NumberOfNodes();
  if(hc < dc) {
    if(MyNodeId() == 0) {
      cout<<0<<endl;
    }
    return 0;
  }
  int maxc = max(hc,dc);
  int me = MyNodeId();
  if (me != 0) {
    return 0;
  }
  vector<ll> h, d;
  for(int i=0;i<hc;i++) {
    h.push_back(HoleDiameter(i+1));
  }
  h.push_back(0);
  nonincr(h);

  for(int i=0;i<dc;i++) {
    d.push_back(DiscDiameter(i+1));
  }

  int from = 0;
  int to = h.size()-1;
  int i = 0;
  while(i < dc && to > 0) {
    if(d[i] <= h[from]) {
      to = bs(h,d[i],from,to+1);
      h[to] = 0;
    } else {
      break;
    }
    i++;
  }
  cout<<(i<dc?0:(to+1))<<endl;
  return 0;
}