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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "message.h"
#include "krazki.h"

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

#define FOR(i,n) for(int i=0; i < int(n); ++i)
#define FO(i,a,b) for(int i=int(a); i<int(b); ++i)
#define OF(i,a,b) for(int i=int(b)-1; i>=int(a); --i)

typedef long long ll;
typedef pair<int,int> pii;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

#define fi first
#define se second

#define ASS assert

#define remin(a,b) a=min(a,b)
#define remax(a,b) a=max(a,b)

#define ALL(c) (c).begin(), (c).end()

#define NL '\n'

#define D if(0)
#define cerr if(0)cerr


int mega_size = 0;

#define div_up(a,b) (((a) + (b)-1) / (b))

int debug_bytes_sent = 0;
int debug_messages_sent = 0;

int num_holes;
int num_discs;

inline pii disc_bounds(int d){
	int idx0 = d*mega_size;
	int idx1 = (d+1)*mega_size;
	remin(idx1, num_discs);
	return {idx0, idx1};
}

inline pii hole_bounds(int h){
	int idx0 = h*mega_size;
	int idx1 = (h+1)*mega_size;
	remin(idx1, num_holes);
	return {idx0, idx1};
}


int wrzuc(int dd, int hh){
	int d0 = disc_bounds(dd).fi;
	int d1 = disc_bounds(dd).se;
	
	int h0 = hole_bounds(hh).fi;
	int h1 = hole_bounds(hh).se;
	
	vector<ll> min_to(h1-h0);
	FOR(i, min_to.size()) min_to[i] = HoleDiameter(1 + h0 + i);
	FO(i, 1, min_to.size()) remin(min_to[i], min_to[i-1]);
	
	int dno = h1-h0;
	FO(d,d0,d1){
		ll c = DiscDiameter(1 + d);
		int a = 0;
		int b = dno;
		while(a < b){
			int m = (a+b)/2;
			if(c <= min_to[m]){
				a = m+1;
			}
			else{
				b = m;
			}
		}
		
		if(b < h1-h0) dno = b-1;
	}
	
	// send result to root
	PutInt(0, dno); debug_bytes_sent += 4;
	Send(0); debug_messages_sent++;
	
	return dno;
}



int main(){
	
	int num_nodes = NumberOfNodes();
	int me = MyNodeId();
	
	
	num_holes = PipeHeight();
	num_discs = NumberOfDiscs();

	{
		int a = div_up(num_holes, num_nodes);
		int b = div_up(num_discs, num_nodes);
		mega_size = max(a,b);
	}
	
	vector<ll> mega_holes(div_up(num_holes, mega_size));
	vector<ll> mega_discs(div_up(num_discs, mega_size));
	
	if(me == 0){
		cerr << "mega_size: " << mega_size << endl;
		cerr << "number of mega_holes: " << mega_holes.size() << endl;
		cerr << "number of mega_discs: " << mega_discs.size() << endl;
	}
	
	// compute mega holes
	ll min_hole = LLONG_MAX;
	{
		int idx0 = me*mega_size;
		int idx1 = (me+1)*mega_size;
		remin(idx1, num_holes);
	
		FO(i, idx0, idx1){
			remin(min_hole, HoleDiameter(1 + i));
		}
		
	}
	
	// compute mega discs
	ll max_disc = -LLONG_MAX;
	{
		int idx0 = me*mega_size;
		int idx1 = (me+1)*mega_size;
		remin(idx1, num_discs);
		
		FO(i, idx0, idx1){
			remax(max_disc, DiscDiameter(1 + i));
		}
		
	}
	
	FOR(i, num_nodes){
		PutLL(i, min_hole); debug_bytes_sent += 8;
		PutLL(i, max_disc); debug_bytes_sent += 8;
		Send(i); debug_messages_sent++;
	}

	
	FOR(i, num_nodes){
		Receive(i);
		
		ll hole = GetLL(i);
		ll disc = GetLL(i);
		
		if(i < (int)mega_holes.size()) mega_holes[i] = hole;
		else D assert(abs(hole) == LLONG_MAX);
		
		if(i < (int)mega_discs.size()) mega_discs[i] = disc;
		else D assert(abs(disc) == LLONG_MAX);
	}
	
	
	// mega_holes and mega_discs in sync now.
	
	D if(me == 0){
		{
			ostringstream os;
			os << "Mega holes:" << " ";
			FOR(i, mega_holes.size()){
				os << mega_holes[i] << " ";
			}
			cerr << os.str() << endl;
		}
		{
			ostringstream os;
			os << "Mega discs:" << " ";
			FOR(i, mega_discs.size()){
				os << mega_discs[i] << " ";
			}
			cerr << os.str() << endl;
		}
	}
	
	
	int murzyn = 1;
	
	int dno_min = num_holes;
	int dno_max = num_holes;
	
	// used only by root
	int dno_real = num_holes;
	
	FOR(d, mega_discs.size()){
		int kolizja = mega_holes.size();
		int mx = (dno_max-1) / mega_size;
		if(dno_max <= 0) mx=0;
		
		int mega_size_this = disc_bounds(d).se - disc_bounds(d).fi;
		dno_real -= mega_size_this;
		
		dno_min -= mega_size_this;
		dno_max -= mega_size_this;
		
		
		FOR(h, min((int)mega_holes.size(), mx+1)){
			if(mega_discs[d] > mega_holes[h]){
				kolizja = h;
				
				// ktoras instancja musi policzyc dokladny wynik
				if(me == murzyn){
					D cerr << "jestem " << me << " wiec licze wrzuc(" << d << "," << h << ")" << endl;
					wrzuc(d,h);
				}
				
				// a root niech go uzyje
				if(me == 0){
					D cerr << "Jestem ROOT i czekam na wynik." << endl;
					Receive(murzyn);
					int dno_real_new = GetInt(murzyn);
					D cerr << "Dzieki, dostalem! Czyli mowisz, ze dno to " << h*mega_size << " + " << dno_real_new << endl;
					remin(dno_real, h*mega_size + dno_real_new);
					D cerr << "Zrobie z tego uzytek i mysle, ze dno to bedzie teraz " << dno_real << endl;
				}
				
				murzyn = (murzyn+1) % num_nodes;
				if(murzyn == 0) ++murzyn;
				
				
				break;
			}
		}
		
		if(me == 0) D cerr << "wrzucam mega-krazek " << d << " i kolidowal na mega-pozycji " << kolizja << endl;
		
		
		
		int dno_min_new = kolizja * mega_size - mega_size;
		int dno_max_new = kolizja * mega_size + mega_size-1;
		
		remin(dno_min, dno_min_new);
		remin(dno_max, dno_max_new);
		
		if(me == 0) D cerr << "dno na pozycji " << dno_real << ", czyli w przedziale [" << dno_min << ", " << dno_max << "]" << endl;
		
		if(me == 0) D assert(dno_min <= dno_real && dno_real <= dno_max);
	}
	
	if(me == 0){
		if(dno_real < -1) dno_real = -1;
		cout << dno_real+1 << NL;
	}
	
	cerr << "messages sent: " << debug_messages_sent << endl;
	cerr << "bytes sent: " << debug_bytes_sent << endl;
	
	
	return 0;
}