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<bits/stdc++.h>
#define FOR(i,s,e) for(int i=(s);i<=(e);i++)
#define FORD(i,s,e) for(int i=(s);i>=(e);i--)
#define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define ALL(k) (k).begin(),(k).end()
#define e1 first 
#define e2 second
#define mp make_pair
#define pb push_back
#define eb emplace_back 
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const bool print=false;
const int MAXI=111;

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

LL t[MAXI];
LL t1[MAXI];
PLL sendings[MAXI];


int main() {
	int nnodes=NumberOfNodes();
	int mynode=MyNodeId();
	int n=PipeHeight();
	int dd=(n+nnodes-1)/nnodes;
	int n1=min(dd*mynode+1,n+1),n2=min(dd*(mynode+1),n);
	if(print)printf("INST %d:: %d %d\n",mynode,n1,n2);
	LL minim=(LL)1e18+696969;
	LL maxim=0;
	FOR(i,n1,n2){
		LL a=HoleDiameter(n-i+1);
		minim=min(minim,a);
		maxim=a;
	}
	FOR(i,0,nnodes-1)
		PutLL(i,minim),PutLL(i,maxim),Send(i);
	FOR(i,0,nnodes-1){
		Receive(i);
		t1[i+1]=GetLL(i);
		t[i+1]=GetLL(i);
	}
	t1[nnodes]=(LL)1e18+6969;
	FORD(i,nnodes-1,0){
		t1[i]=min(t1[i],t1[i+1]);
		t[i]=min(t[i],t1[i+1]);
	}
	FOR(i,1,nnodes) if(t[i]<t[i-1]) t[i]=t[i-1];
	if(print){
		printf("%d:: ",mynode);
		FOR(i,0,nnodes) printf("%lld ",t[i]);
		puts("");
		printf("%d:: ",mynode);
		//FOR(i,0,nnodes) printf("%lld ",t1[i]);
		puts("");
	}
	//OKOKOK
	
	int m=NumberOfDiscs();
	int dm=(m+nnodes-1)/nnodes;
	int m1=min(dm*mynode+1,m+1),m2=min(dm*(mynode+1),m);
	
	int wynup=0;//najwyzsza pozycja na ktorej jest ostatni z przedzialu
	int wyndol=0;
	FOR(i,m1,m2){
		LL a=DiscDiameter(i);
		int nr=lower_bound(t,t+nnodes+1,a)-t-1;
		if(nr>=nnodes) nr=nnodes-1;
		//if(nr<0) nr=0;//{puts("X");nr=0;}
		int mxm=min(dd*nr+1,n)+m2-i;//na ktorej pozycji najwczesniej znajdzie sie ostatni dysk z przedzialu
		wynup=max(wynup,mxm);
		if(print)printf("A %d, %d %lld; %d %d\n",mynode,i,a,nr,mxm);
	}
	if(mynode!=0){
		Receive(mynode-1);
		int mxdol=GetInt(mynode-1);//pozycja pod najnizszym dyskiem z przedzialu
		wyndol=mxdol;
		wynup=max(wynup,mxdol+m2-m1+1);
	}
	if(mynode!=nnodes-1){
		PutInt(mynode+1,wynup);
		Send(mynode+1);
	}
	if(print)printf("I %d:: %d %d, %d %d\n",mynode,m1,m2,wyndol,wynup);
	int ob=-1;
	FOR(i,0,nnodes-1) sendings[i]=mp(-1,-1);
	FOR(i,m1,m2){
		wyndol++;
		LL a=DiscDiameter(i);
		int nr=lower_bound(t,t+nnodes+1,a)-t-1;
		if(nr<0) nr=0;
		if(nr>=nnodes) nr=nnodes-1;
		if(print)printf("\t%d:: %d %lld;; %d %d; %d\n",mynode,i,a,nr,ob,wyndol);
		if(dd*nr+1>=wyndol){//jezeli nowy nr > nr z wyndola
			if(print)printf("\tA %d %d %d %d\n",nr,ob,dd*nr+1,wyndol);
			if(ob!=-1) {
				//PutInt(ob,i-1);
				sendings[ob].e2=i-1;
				//Send(ob);
			}
			ob=nr;
			wyndol=dd*nr+1;
			//PutInt(ob,i);
			sendings[ob].e1=i;
		}
		else if(wyndol>dd*(ob+1)){
			if(print)printf("\tB %d %d %d %d\n",nr,ob,dd*ob+1,wyndol);
			if(ob!=-1) {
				//PutInt(ob,i-1);
				//Send(ob);
				sendings[ob].e2=i-1;
			}
			//ob=-1;
			ob=nr;sendings[ob].e1=i;
		}
	}
	if(ob!=-1) sendings[ob].e2=m2;//{PutInt(ob,m2);Send(ob);}
	if(print){
		printf("SEND %d:: ",mynode);
		FOR(i,0,nnodes-1) printf("%d %lld %lld;; ",i,sendings[i].e1,sendings[i].e2);
		puts("");
	}
	
	FOR(i,0,nnodes-1){
		PutInt(i,sendings[i].e1),PutInt(i,sendings[i].e2);Send(i);
	}
	
	vector<LL> V;
	FOR(i,n1,n2){
		LL a=HoleDiameter(n-i+1);
		V.pb(a);
	}
	reverse(ALL(V));
	LL pop=(LL)1e18+6969;
	for(auto &it:V){
		if(it>pop) it=pop;
		pop=it;
	}
	reverse(ALL(V));
	if(print){
		printf("%d %d %d:: ",mynode,n1,n2);for(auto it:V) printf("%lld ",it);puts("");
	}
	int wyn=0;
	FOR(i0,0,nnodes-1){
		Receive(i0);
		int lw=GetInt(i0),pw=GetInt(i0);
		if(lw==-1) continue;
		FOR(i,lw,pw){
			LL a=DiscDiameter(i);
			int pos=lower_bound(ALL(V),a)-V.begin()+n1;
			int ww=(m-i)+pos;
			wyn=max(ww,wyn);
			if(print)printf("\t%d %d: %lld %d;; %d %d\n",i0,i,a,pos,ww,wyn);
		}
	}
	//printf("%d %d\n",mynode,wyn); 
	PutInt(0,wyn);Send(0);
	if(mynode==0){
		FOR(i,0,nnodes-1){
			Receive(i);
			wyn=max(wyn,GetInt(i));
		}
		wyn=n-wyn+1;
		if(wyn<0) wyn=0;
		printf("%d\n",wyn);
	}
	
	
	return EXIT_SUCCESS;
 }