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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include "krazki.h"
#include "message.h"

typedef long long int int64;

#define MaxInstances 100

#if defined( MY_MESSAGE )
	#define tls thread_local
#else
	#define tls
#endif

tls int myid;
tls int numInstances;

tls int n;
tls int m;

#define MaxWidth ( ( int64 ) 1 << 60 )

tls int start;
tls int end;
tls int count;
tls std::vector< int64 > widths;

void init()
{
	myid = MyNodeId();
	numInstances = NumberOfNodes();

	n = PipeHeight();

	start = ( int ) ( ( int64 ) n * myid / numInstances );
	end = ( int ) ( ( int64 ) n * ( myid + 1 ) / numInstances );
	count = end - start;

	m = NumberOfDiscs();
}

void calc_widths()
{
	widths.resize( count );
	for ( int i = 0; i < count; ++i )
	{
		int64 w = HoleDiameter( start + i + 1 );
		if ( i && w > widths[ i - 1 ] )
		{
			w = widths[ i - 1 ];
		}
		widths[ i ] = w;
	}
}

void receive_top_and_send_bottom()
{
	int64 topWidth = MaxWidth;
	if ( myid )
	{
		Receive( myid - 1 );
		topWidth = GetLL( myid - 1 );
	}

	int64 bottomWidth = topWidth;
	if ( !widths.empty() && bottomWidth > widths.back() )
	{
		bottomWidth = widths.back();
	}

	if ( myid < numInstances - 1 )
	{
		PutLL( myid + 1, bottomWidth );
		Send( myid + 1 );
	}

	for ( int i = 0; i < count; ++i )
	{
		if ( widths[ i ] > topWidth )
		{
			widths[ i ] = topWidth;
		}
	}
}

struct region_t
{
	int start;
	int count_initial;
	int count_processed;

	region_t()
		: start( -1 )
		, count_initial( 0 )
		, count_processed( 0 )
	{}
};

region_t create_initial_region()
{
	region_t initialRegion;
	initialRegion.start = start - ( n - m );
	int initialRegionEnd = end - ( n - m );
	if ( initialRegion.start < 0 )
	{
		initialRegion.start = 0;
	}
	if ( initialRegionEnd < initialRegion.start )
	{
		initialRegionEnd = initialRegion.start;
	}
	initialRegion.count_initial = initialRegionEnd - initialRegion.start;

	return initialRegion;
}

void Send( const int target, const region_t& region )
{
	PutInt( target, region.start );
	PutInt( target, region.count_initial );
	PutInt( target, region.count_processed );
	Send( target );
}

void Receive( const int source, region_t& region )
{
	Receive( source );
	region.start = GetInt( source );
	region.count_initial = GetInt( source );
	region.count_processed = GetInt( source );
}

int find_height_for_disc( const int count, const int64 value )
{
	auto begin = widths.begin();
	auto end = begin + count;
	auto it = std::upper_bound( begin, end, value, [](const int64 lhs, const int64 rhs) -> bool { return lhs > rhs; } );
	if ( it != begin )
	{
		--it;
	}
	else
	{
		it = end;
	}
	return it == end ? -1 : ( int ) ( it - begin );
}

int handle_discs()
{
	int space_left;
	int needed_space;

	int stored = 0;
	int low = count;

	region_t to_process = create_initial_region();

	int numIterationsLeft = numInstances - myid;
	while ( numIterationsLeft )
	{
		// store initial

		int new_stored = 0;
		int new_low = count;

		int i = 0;
		while ( i < to_process.count_initial )
		{
			const int64 disc = DiscDiameter( m - ( to_process.start + to_process.count_initial - i ) + 1 );

			const int disc_height = find_height_for_disc( new_low, disc );
			if ( disc_height == -1 )
			{
				if ( !myid )
				{
					low = -1;
					return low;
				}

				region_t to_send;
				to_send.start = to_process.start;
				to_send.count_initial = to_process.count_initial - i;
				to_send.count_processed = to_process.count_processed + stored;
				Send( myid - 1, to_send );

				stored = new_stored;

				goto next_iteration;
			}
			else
			{
				++new_stored;
				new_low = disc_height;
				++i;
			}
		}

		// manage space left

		space_left = new_low;
		needed_space = to_process.count_processed + stored;
		if ( space_left < needed_space )
		{
			if ( !myid )
			{
				low = -1;
				return low;
			}

			const int missing_space = needed_space - space_left;

			region_t to_send;
			to_send.start = to_process.start + to_process.count_initial + space_left;
			to_send.count_initial = 0;
			to_send.count_processed = missing_space;
			Send( myid - 1, to_send );

			stored = to_process.count_initial + space_left;
			low = 0;

			goto next_iteration;
		}
		else
		{
			stored += to_process.count_initial + to_process.count_processed;
			if ( low > new_low - needed_space )
			{
				low = new_low - needed_space;
			}
		}

		// send empty region

		if ( myid )
		{
			Send( myid - 1, region_t() );
		}

next_iteration:
		// receive next region to process

		--numIterationsLeft;

		if ( numIterationsLeft )
		{
			if ( myid < numInstances - 1 )
			{
				Receive( myid + 1, to_process );
			}
		}
	}

	PutInt( 0, low == count ? -2 : start + low );
	Send( 0 );

	if ( !myid )
	{
		low = n;
		for ( int i = numInstances - 1; i >= 0; --i )
		{
			Receive( i );
			const int recv_low = GetInt( i );
			if ( recv_low != -2 && low > recv_low )
			{
				low = recv_low;
			}
		}
	}

	return low;
}

void print_result( const int low )
{
	if ( !myid )
	{
		std::cout << ( low + 1 );
	}
}

#ifdef MY_MESSAGE
int my_main( int _id )
#else
int main()
#endif
{
#ifdef MY_MESSAGE
	{
		extern thread_local int id;
		id = _id;
	}
#endif

	init();
	if ( m > n )
	{
		if ( !myid )
		{
			std::cout << 0;
		}
		return 0;
	}
	calc_widths();
	receive_top_and_send_bottom();
	const int low = handle_discs();
	print_result( low );
	return 0;
}