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

#include "krazki.h"
#include "message.h"
#include <algorithm>

#define NUM 400000
using namespace std;
int main() {

  int numberOfNodes = NumberOfNodes();
  int myNodeId = MyNodeId();

  int pipeHeight = PipeHeight();
  int numberOfDiscs = NumberOfDiscs();

  int numberOfPipeParts = (pipeHeight-1)/NUM + 1;
  int numberOfDiscsParts = (numberOfDiscs-1)/NUM + 1;

  int numberOfMyPipeParts = (numberOfPipeParts / numberOfNodes) + ((myNodeId < (numberOfPipeParts % numberOfNodes)));
  int numberOfMyDiscsParts = (numberOfDiscsParts / numberOfNodes) + (myNodeId < (numberOfDiscsParts % numberOfNodes));

  for (int j = 0 ; j < numberOfMyPipeParts; j++)
  {
    long long min = 1000000000100000000LL;
    for(int i = (myNodeId + numberOfNodes * j) * NUM ; i<(myNodeId + numberOfNodes * j + 1)*NUM && i<pipeHeight; i++)
    {
      long long hd = HoleDiameter(i+1);
      if (min>hd)min=hd;
    }
    PutChar(0,'H');
    PutInt(0,myNodeId + numberOfNodes * j);
    PutLL(0,min);
    Send(0);
  }
  for (int j = 0 ; j < numberOfMyDiscsParts; j++)
  {
    long long max = 0LL;
    for(int i = (myNodeId + numberOfNodes * j) * NUM ; i<(myNodeId + numberOfNodes * j + 1)*NUM && i<numberOfDiscs; i++)
    {
      long long dd = DiscDiameter(i+1);
      if (max<dd)max=dd;
    }
    PutChar(0,'D');
    PutInt(0,myNodeId + numberOfNodes * j);
    PutLL(0,max);
    Send(0);
  }

  if(myNodeId!=0) return EXIT_SUCCESS;

  long long tabHD[numberOfPipeParts];
  long long tabDD[numberOfDiscsParts];

  for(int j = 0; j<numberOfPipeParts + numberOfDiscsParts; j++)
  {
    int f=Receive(-1);
    switch(GetChar(f))
    {
      case 'D':
{        int i = GetInt(f);
        long long max = GetLL(f);
        tabDD[i]=max;
        break;
}
      case 'H':
{        int i = GetInt(f);
        long long min = GetLL(f);
        tabHD[i]=min;
        break;
}
    }
  }

  int hd,dd;
  for (hd=0;hd<numberOfPipeParts;hd++)
  {
    for(dd=0;dd<numberOfDiscsParts;dd++)
    {
      if(tabHD[hd]<tabDD[dd])
      {
        break;
      }
    }
      if(dd<numberOfDiscsParts)
      {
        break;
      }
  }

  if(hd==numberOfPipeParts)
  {
#ifdef DEBUG
printf("nie ma preciec\n");
#endif
    printf("%d\n",max(pipeHeight-numberOfDiscs,0));
    return EXIT_SUCCESS;
  }

  int ph=hd*NUM;
  int kh = min((hd+1)*NUM-1,pipeHeight-1);
  int pd=dd*NUM;
  int kd = min((dd+1)*NUM-1,numberOfDiscs-1);

#ifdef DEBUG
printf("ph:%d kh:%d pd:%d kd:%d\n",ph,kh,pd,kd);
#endif
  int numHD = kh-ph+1;
  int numDD = kd-pd+1;

  long long tab2HD[numHD];
  tab2HD[numHD-1] = HoleDiameter(ph+1);
#ifdef DEBUG
printf("tab2HD\n");
for (int i=0;i<numHD;i++)
{
  printf("%lld ",tab2HD[i]);
}
#endif


  for(int j=1;j<numHD;j++)
  {
    long long hd = HoleDiameter(ph+j+1);
    if(hd<tab2HD[numHD-j])tab2HD[numHD-j-1]=hd;
    else tab2HD[numHD-j-1]=tab2HD[numHD-j];
  }

#ifdef DEBUG
printf("tab2HD\n");
for (int i=0;i<numHD;i++)
{
  printf("%lld ",tab2HD[i]);
}
#endif
  long long *maxp=tab2HD;
  int maxj=pd,maxi;
  for(int j=pd;j<=kd;j++)
  {
    long long dd = DiscDiameter(j+1);
    long long *p = lower_bound(tab2HD,tab2HD+numHD,dd);
    if(p>maxp){maxp=p;maxj=j;};
#ifdef DEBUG
printf("\n*p: %lld, j: %d\n",*p,j);
printf("p-tab2HD: %d\n",p-tab2HD);
#endif
  }

  maxj = numberOfDiscs - maxj;
  maxi = maxp-tab2HD;
  maxi = pipeHeight - maxi;
  printf("%d\n",max(maxi-maxj+1,0));
  return EXIT_SUCCESS;
}