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

long long int N,id,h,d;
long long int range_beg, range_end;
long long int disk_beg, disk_end;
long long int *T;
long long int *Q;

main() {
  N = NumberOfNodes();
  id = MyNodeId();
  h = PipeHeight();
  d = NumberOfDiscs();

  if(N > h) {
    N = 1;
    if(id > 0) return 0;
  }
  if (d > h) {
    if(id==0) {
      printf("0\n");
    }
    return 0;
  }

  range_beg = h*id/N + 1;
  range_end = h*(id+1)/N;
  disk_beg = h*id/N + 1;
  disk_end = d*(id+1)/N;

  if(id == N-1) {
    range_end = h;
  }

  T = new long long int[range_end - range_beg + 10];
//  Q = new long long int[disk_end - disk_beg + 10];

  // Pipe reshape
  long long int curr_min = 1000000000LL*1000000000LL;
  for (long long int i=range_beg; i<=range_end; i++) {
    long long int dd = HoleDiameter(i);
    if (dd < curr_min)
      curr_min = dd;
    T[i-range_beg] = curr_min;
  }

  long long int min_dim = 1000000000LL*1000000000LL;
  if(id != 0) {
    Receive(id-1);
    min_dim = GetLL(id-1);
  }

  if (id != N-1) {
    if(min_dim > T[range_end-range_beg]) {
      PutLL(id+1, T[range_end-range_beg]);
    } else {
      PutLL(id+1, min_dim);
    }
    Send(id+1);
  }

  for(int i=range_beg; i<=range_end;i++) {
    if(T[i-range_beg] > min_dim) {
      T[i-range_beg] = min_dim;
    } else {
      break;
    }
  }
/*
  // Disk reshape.
  long long int curr_max = 0;
  for (long long int i=disk_beg; i<=disk_end; i++) {
    long long int dd = DiscDiameter(i);
    if (dd > curr_max)
      curr_max = dd;
    Q[i-disk_beg] = curr_max;
  }

  long long int max_dim = 0;
  if(id != N-1) {
    Receive(id+1);
    max_dim = GetLL(id+1);
  }

  if (id != 0) {
    if(max_dim < Q[range_end]) {
      PutLL(id-1, Q[range_end]);
    } else {
      PutLL(id-1, max_dim);
    }
    Send(id-1);
  }

  for(int i=disk_beg; i<=disk_end; i++) {
    if(Q[i-disk_beg] < max_dim) {
      Q[i-disk_beg] = max_dim;
    } else {
      break;
    }
  }
*/
  // Compute gaps.
  long long int curr_disk = 1;
  if (id != N-1) {
    Receive(id+1);
    curr_disk = GetLL(id+1);
  }
  if (curr_disk == -1) {
    if(id!=0) {
      PutLL(id-1, -1);
      Send(id-1);
    }
    return 0;
  }

  long long int curr_hole = range_end;

  long long int ddim = DiscDiameter(curr_disk);
  while(true) {
    while(curr_hole >= range_beg && T[curr_hole-range_beg] < ddim) {
      curr_hole--;
    }
    if(curr_hole < range_beg) {
      if(id!=0) {
        PutLL(id-1, curr_disk);
        Send(id-1);
        return 0;
      } else {
        printf("0\n");
        return 0;
      }
    } else {
      curr_disk++;
      if (curr_disk > d) {
        printf("%lld\n", curr_hole);
        if(id != 0) {
          PutLL(id-1, -1);
          Send(id-1);
        }
        return 0;
      }
      curr_hole--;
      if(curr_hole < range_beg) {
        if(id!=0) {
          PutLL(id-1, curr_disk);
          Send(id-1);
          return 0;
        } else {
          printf("0\n");
          return 0;
        }
      }
      ddim = DiscDiameter(curr_disk);
    }
  }
/*
  for(int i = disk_beg; i<=disk_end; i++) {
    while(cur_pos <= range_end && T[cur_pos] < Q[i-disk_beg]) {
      cur_pos++;
      my_gaps++;
    }
    if(cur_pos > range_end) {
      my_over = disk_end - i + 1;
    }
  }
*/
  return 0;
}