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

#include <bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define ALL(G) (G).begin(),(G).end()

//#define DEBUG
#ifdef DEBUG
#define DEB printf
#else
#define DEB(...)
#endif

typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> vi;

int ja,discs,height,nodes,d;

const int MAXN=3000000;
const int NODES=123;
LL H[MAXN];
LL M[NODES];
LL D[MAXN];
LL inf=1LL<<60;

const int GORA=NODES; //2do: change?

int last_bs;

int b_s(LL r){
	 int p=0,k=nodes;
	 while(p+1<k){
		  int m=(p+k)/2;
		  if(M[m]>=r) k=m;
		  else p=m;
	 }
	 //	 printf("b_s dla %lld to %d (%lld)\n",r,p,M[p]);
	 last_bs=p;
	 return p;
}

int dol(LL r, int nr){
	 return d*b_s(r)+discs-nr; //2do: min z height -1?
}
bool czy[NODES];
int main(){
	 ja=MyNodeId();
	 discs=NumberOfDiscs();
	 height=PipeHeight();
	 nodes=NumberOfNodes();
	 //znajdz min w bloku dziur
	 d=height/nodes+1;
	 int pocz=d*ja,kon=min(height,(ja+1)*d);
	 int tu=max(kon-pocz,0);
	 fru(i,tu) H[i]=HoleDiameter(height-i-pocz);
	 LL mmin=inf;
	 fru(i,tu) mmin=min(mmin,H[i]);
	 fru(i,nodes){
		  PutLL(i,mmin);
		  Send(i);
	 }
	 fru(i,nodes){
		  Receive(i);
		  M[i]=GetLL(i);
	 }
	 LL w=inf;
	 for(int i=nodes-1;i>=0;--i){
		  w=min(w,M[i]);
		  M[i]=w;
	 }
	 if(ja==0){
		  fru(i,nodes) DEB("%lld ",M[i]); DEB("\n");
	 }
	 int d_z=discs/nodes+1;
	 int p_z=d_z*ja,k_z=min(discs,d_z*(ja+1));
	 int kand=0;
	 fru(i,k_z-p_z){
		  D[i]=DiscDiameter(i+1+p_z);
		  int e=dol(D[i],i+p_z);
		  kand=max(kand,e);
		  DEB("kand dla %lld (%d) to %d\n",D[i],i+p_z+1,e);
	 }
	 DEB("kand z %d to %d\n",ja,kand);
	 fru(i,nodes){
		  PutInt(i,kand);
		  Send(i);
	 }
	 fru(i,nodes){
		  Receive(i);
		  int e=GetInt(i);
		  kand=max(kand,e);
		  if(ja==0) DEB("%d przyslal %d\n",i,e);
	 }
	 if(kand>height+1){ //2do: +1?
		  if(ja==0) puts("0");
		  return EXIT_SUCCESS;
	 }
	 fru(i,k_z-p_z){
		  int e=dol(D[i],i+p_z);
		  if(e+d>=kand){//2do: = ?
				czy[last_bs]=1;
		  }
		  DEB("kand dla %lld (%d) to %d\n",D[i],i+p_z+1,e);
	 }
	 int ile=0;
	 fru(i,nodes) ile+=czy[i];
	 assert(ile<GORA);
	 fru(i,nodes){
		  PutInt(i,czy[i]);
		  Send(i);
	 }
	 M[nodes]=inf;
	 LL s=M[ja+1];
	 if(0) {
		  fru(i,nodes) printf("%lld ",M[i]); puts("");
		  printf("s = %lld\n",s);
	 }
	 for(int i=kon-1;i>=pocz;--i){
		  s=min(s,H[i-pocz]);
		  H[i-pocz]=s;
	 }
	 int ret=0;
	 int tutaj=0;
	 fru(w,nodes){
		  Receive(w);
		  int e=GetInt(w);
		  if(e==0) continue;
		  ++tutaj;
		  int p_z=d_z*w,k_z=min(discs,d_z*(w+1));
//		  if(ja==0) fru(i,20) printf("%lld ",H[i]); puts("");
		  LL last=0;
		  for(int i=p_z;i<k_z;++i){
				LL r=DiscDiameter(i+1);
				if(b_s(r)!=ja) continue;
				if(r<=last) continue;
				last=r;
				int pp=0,kk=tu,x;
				if(H[0]>=r) x=0;
				else{
					 while(pp+1<kk){
						  int mm=(pp+kk)/2;
						  if(H[mm]>=r) kk=mm;
						  else pp=mm;
					 }
					 x=kk;
				}
				int wyn=ja*d+x+discs-i;
if(0)				if(wyn>ret) {
					 fprintf(stderr,"(%d): %lld na poz %d ma wynik %d bo bs zwrocil %d czyli %lld\n",ja,r,i+1,wyn,x,H[x]);
//					 fprintf(stderr,"
				}
				ret=max(ret,wyn);
		  }
	 }
	 assert(tutaj<GORA);
	 PutInt(0,ret);
	 Send(0);
	 if(ja==0){
		  fru(i,nodes) {
				Receive(i);
				ret=max(ret,GetInt(i));
		  }
		  printf("%d\n",max(0,height-ret+1));
	 }
	 return EXIT_SUCCESS;
}