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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <iostream>
#include <cmath>
#include <limits>
#include <vector>
#include <list>
#include <cstdint>

#include <unistd.h>

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

//#define DODEB
#ifdef DODEB

#define DEBUG(A) std::cerr << A;
#define DEBUGNL(A) std::cerr << A << std::endl;
#define SLEEP(A) sleep(A);

#else

#define DEBUG(A)
#define DEBUGNL(A)
#define SLEEP(A)

#endif

////////////////////////////////////////////////////////////////////////////////
// A class for assigning jobs to nodes

class scheduler_t {
protected:
	// Minimum reasonable number of indices per node
	int min_jobs_per_node;

	long long num_jobs;
	int num_used_nodes;
	long long jobs_per_node;

public:
	scheduler_t(long long num_jobs, int num_nodes, int min_jobs_per_node);
	int get_num_used_nodes() const;
	bool is_node_used(int node_id) const;
	std::pair<long long, long long> get_jobs_for_node(int node_id) const;
};

scheduler_t::scheduler_t(long long num_jobs, int num_nodes,
	int min_jobs_per_node) : min_jobs_per_node(std::max(1, min_jobs_per_node)),
		num_jobs(num_jobs) {

	if(num_jobs < min_jobs_per_node) {
		num_used_nodes = 1;
		jobs_per_node = num_jobs;
	} else {
		// At most as many nodes as jobs
		num_used_nodes = std::min(num_jobs, static_cast<long long>(num_nodes));
		// As a result jobs_per_node here is at least 1
		jobs_per_node = num_jobs / num_used_nodes;

		// If the number of jobs per node is smaller than the limit
		if(jobs_per_node < min_jobs_per_node) {
			num_used_nodes = num_jobs / min_jobs_per_node;
			jobs_per_node = num_jobs / num_used_nodes;
		}
	}
}

int scheduler_t::get_num_used_nodes() const {
	return num_used_nodes;
}

bool scheduler_t::is_node_used(int node_id) const {
	return node_id < num_used_nodes;
}

std::pair<long long, long long> scheduler_t::get_jobs_for_node(int node_id) const {
	// Unused node
	if (node_id >= num_used_nodes) {
		return std::make_pair(-1, -1);
	}

	// For the used nodes
	// Distribute the reminder of jobs equaly
	long long rem = num_jobs - jobs_per_node * num_used_nodes;

	long long first;
	long long last;
	if (node_id < rem) {
		first = (jobs_per_node + 1) * node_id;
		last = first + jobs_per_node;
	} else {
		first = (jobs_per_node + 1) * rem + jobs_per_node * (node_id - rem);
		last = first + jobs_per_node - 1;
	}
	
	return std::make_pair(first, last);
}



////////////////////////////////////////////////////////////////////////////////
// A helper storage class

class edge_t {
public:
	int level;
	long long diam;

	edge_t(int level = 0, int diam = 0) : level(level), diam(diam) {
	}
};

////////////////////////////////////////////////////////////////////////////////
// The solver class

class level_counter_t {
public:
	int height;
	int num_discs;
	int num_wholes;
	int local_level;
	scheduler_t sched;
	std::list<edge_t> hole_diams;
	long long begin;
	long long end;
	long long non_processed;

	level_counter_t(int min_jobs_per_node) : height(PipeHeight()),
		num_discs(NumberOfDiscs()), num_wholes(0), local_level(0),
		sched(PipeHeight(), NumberOfNodes(), min_jobs_per_node),
		begin(0), end(0) {
	}

	void prepare_container();
	void exchange_limits();
	void insert_discs();
	void aggregate_results();

protected:
	void update_container(edge_t edge) {
		while(edge.diam < hole_diams.back().diam) {
			edge.level = hole_diams.back().level;
			hole_diams.pop_back();
		}
		hole_diams.push_back(edge);
	}

	std::pair<long long, long long> get_disk_range() const;

	void insert_discs_work(long long end_tmp, int &local_level_tmp,
		int &num_wholes_tmp, long long &non_processed_tmp,
		std::pair<long long, long long> range) const;
};

void level_counter_t::prepare_container() {
	int my_id = MyNodeId();
	if(sched.is_node_used(my_id)) {
		std::pair<long long, long long> range = sched.get_jobs_for_node(my_id);
		begin = range.first;
		end = range.second;
		edge_t edge;

		// Set a guard
		hole_diams.push_back(edge_t(0, 0LL));

		// Update current level of discs
		local_level = begin + 1;

		SLEEP(my_id);
		DEBUGNL(my_id << ")");

		for(long long i = begin; i <= end; ++i) {
			update_container(edge_t(i, HoleDiameter(height - i)));
		}

		#ifdef DODEB
		for(auto edge : hole_diams) {
			DEBUG(edge.level << " " << edge.diam << " | ");
		}
		#endif
		DEBUGNL("\ndisc range: " << get_disk_range().first << " "
			<< get_disk_range().second 
			<< " begin " << begin << " end " << end
			<< "\n**********\n");
	}
}

void level_counter_t::exchange_limits() {
	int my_id = MyNodeId();
	int prev_id = my_id - 1;
	int next_id = my_id + 1;
	if(sched.is_node_used(my_id)) {
		// The first node sends nothing
		if(my_id != 0) {
			PutLL(prev_id, (++hole_diams.begin())->diam);
			Send(prev_id);
		}
		// The last one receives nothing
		if(my_id != sched.get_num_used_nodes() - 1) {
			Receive(next_id);
			long long diam = GetLL(next_id);
			// Update the container if necessary
			if(diam < hole_diams.back().diam) {
				update_container(edge_t(hole_diams.back().level, diam));
			}
		}

		SLEEP(my_id);
		DEBUGNL(my_id << ")");
		/*for(auto edge : hole_diams) {
			DEBUG(edge.level << " " << edge.diam << " | ");
		}*/
		DEBUGNL("");

	}
}

void level_counter_t::insert_discs() {
	insert_discs_work(end, local_level, num_wholes, non_processed,
		get_disk_range());
}

void level_counter_t::aggregate_results() {
	int my_id = MyNodeId();
	int prev_id = my_id - 1;
	int next_id = my_id + 1;
	if(sched.is_node_used(my_id)) {
		std::pair<long long, long long> disk_range = get_disk_range();

		if(my_id == 0) {
			if(sched.is_node_used(1)) {
				int excess = local_level - end - 1;
				PutInt(next_id, (excess > 0) ? excess : 0);
				PutInt(next_id, local_level);
				PutLL(next_id, non_processed);
				Send(next_id);

				DEBUGNL(my_id << ") agg   ll "
				<< local_level << " end " << end << " ex " << excess << " np "
				<< non_processed);

			} else {
				// Some discs are still not processed
				if(non_processed <= NumberOfDiscs()) {
					local_level += NumberOfDiscs() - non_processed + 1;
				}

				printf("%d\n", height - local_level + 1);
			}
		}
		// The others first receive message from prev
		else {
			// Get update data from prev
			Receive(prev_id);
			int excess_prev = GetInt(prev_id);
			int local_level_prev = GetInt(prev_id);
			long long non_processed_prev = GetLL(prev_id);
			int excess = 0;
			long long non_processed_tmp;
			long long last_to_do = disk_range.first - 1;

			// All discs processed
			// non excess is possible
			if(non_processed_prev == NumberOfDiscs() + 1) {
				local_level = local_level_prev;
				non_processed = non_processed_prev;
			}
			// Something to be done
			else {
				// If some discks still need processing
				// (there can be no excess)
				if(non_processed_prev <= last_to_do) {
					// We start from the bottom of this part
					int local_level_tmp = begin;
					int num_wholes_tmp = 0;

					// Inserting the missed discs
					insert_discs_work(end, local_level_tmp, num_wholes_tmp,
						non_processed_tmp,
						std::make_pair(non_processed_prev, last_to_do));

					// Still left over from processing
					if(non_processed_tmp <= last_to_do) {
						non_processed = non_processed_tmp;
					}

					excess = local_level_tmp - begin - 1;
					DEBUGNL(my_id << ") dddddd " << non_processed_tmp << " " << non_processed << " ex " << excess << " ll " << local_level);

				}
				// All the discs that should be done so fare are done
				else {
					excess = excess_prev;
				}

				// Wholes cannot compensate
				if(excess > num_wholes) {
					local_level += excess - num_wholes;
					excess = local_level - end - 1;
				}
			}

			SLEEP(my_id);
			DEBUGNL(my_id << ") agg ex_p " << excess_prev << " non_p_pr "
				<< non_processed_prev << " llp " << local_level_prev << " ll "
				<< local_level << " end " << end << " ex " << excess << " np "
				<< non_processed << " npt " << non_processed_tmp << " ltd " << last_to_do);


			// Finally all but the last send the updated values up
			if(my_id != sched.get_num_used_nodes() - 1) {
				// If there is any excess
				PutInt(next_id, (excess > 0) ? excess : 0);
				PutInt(next_id, local_level);
				PutLL(next_id, non_processed);
				Send(next_id);
			}
			// The last one prints the result
			else {
				// Some discs are still not processed
				if(non_processed <= NumberOfDiscs()) {
					local_level += NumberOfDiscs() - non_processed + 1;
				}
				printf("%d\n", height - local_level + 1);
			}
		}
	}
}


std::pair<long long, long long> level_counter_t::get_disk_range() const {
	int my_id = MyNodeId();
	std::pair<long long, long long> range = std::make_pair(-1LL, -1LL);
	if(sched.is_node_used(my_id)) {
		range = sched.get_jobs_for_node(my_id);
		++range.first;
		++range.second;
		// Not enough discs for this node
		if(range.first > NumberOfDiscs()) {
			return std::make_pair(NumberOfDiscs() + 1, NumberOfDiscs() + 1);
		}
		// Less discs than current range
		else if(range.second > NumberOfDiscs()) {
			range.second = NumberOfDiscs();
		}
	}
	return std::move(range);
}

void level_counter_t::insert_discs_work(long long end_tmp, int &local_level_tmp,
	int &num_wholes_tmp, long long &non_processed_tmp,
	std::pair<long long, long long> range) const {

	int my_id = MyNodeId();

	if(sched.is_node_used(my_id)) {
		if(range.first <= NumberOfDiscs()) {
			long long diam;
			long long past_end = end + 1;
	
			long long i = range.first;
			for(; i <= range.second; ++i) {
				diam = DiscDiameter(i);
				auto it = hole_diams.rbegin();
	
//				DEBUGNL(my_id << ") d " << i << " placing " << diam)
	
				// Done filling the container we cannot process i
				if(diam > it->diam) {
					break;
				}
				// Level already over the top
				if(local_level_tmp == past_end) {
					local_level_tmp++;
				}
				// local_level_tmp is below the top and the disk can go in 
				else {
					for(; local_level_tmp < it->level
						&& it->diam > diam
						&& it != hole_diams.rend(); ++it);
	
					// local_level_tmp is the limiter
					if(local_level_tmp >= it->level) {
						++local_level_tmp;
					}
					// The disc gets stuck above local_level
					else {									
						num_wholes_tmp += it->level - local_level_tmp;
						local_level_tmp = it->level + 1;
					}
				}
				DEBUGNL(my_id << ") place " << i << " at " << local_level_tmp - 1 << " " << diam);
			}
			non_processed_tmp = i;
			DEBUGNL(my_id << ") first not processed " << non_processed_tmp << " " << local_level - end - 1);
		} else {
			non_processed_tmp = NumberOfDiscs() + 1;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////

int main() {
	// Get a solver
	level_counter_t level_counter(20);
	
	level_counter.prepare_container();
	SLEEP(1);
	level_counter.exchange_limits();
	SLEEP(1);
	level_counter.insert_discs();
	SLEEP(1);
	level_counter.aggregate_results();

	//aggregate_results();

	return 0;
}