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
/*
 *  Copyright (C) 2016  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include "krazki.h"
#include "message.h"

#define height PipeHeight
#define discs NumberOfDiscs
#define read_hole HoleDiameter
#define read_disc DiscDiameter

#define node_id MyNodeId
#define nodes NumberOfNodes
#define push PutInt
#define pop GetInt
#define push_long PutLL
#define pop_long GetLL
#define send Send
#define receive Receive

#include <vector>
#include <unordered_map>
#include <cmath>
#include <iostream>
using namespace std;


//#define DEBUG 666
#ifdef DEBUG
	#define DBG(key, value) cerr << key << " " << value << endl;
	#define DBGa(array) for (auto a: array) { cerr << a << " "; } cerr << endl;
#else
	#define DBG(key, value)
	#define DBGa(array)
#endif


int main() {
	int n = height();
	int m = discs();
	int k = nodes();

	// for small input limit the number of used nodes
	int jobs = max(1, n / 2);
	if (k > jobs) {
		k = jobs;
		if (node_id() >= jobs) {
			return 0;
		}
	}

	// select range to process in this node
	int range = n / k;
	int begin = node_id() * range;
	int end = (node_id() + 1) * range;
	if (node_id() == k - 1) {
		end = n;
	}

	// neighbouring nodes
	int next = node_id() + 1;
	int previous = node_id() - 1;

	long long largest_hole = 2 * pow(10, 18) + 1;
	long long smallest_hole = largest_hole;

	// read hole diameters in range(begin, end)
	unordered_map<long long, int> max_level;
	max_level.reserve(end - begin);
	for (int i = begin; i < end; ++i) {
		long long hole = read_hole(i + 1);
		max_level[hole + 1] = i;
		smallest_hole = min(smallest_hole, hole);
	}

	// send smallest_hole to the next node
	if (node_id() == 0) {
		push_long(1, smallest_hole);
		send(1);
	} else {
		// receive from the previous node
		int node = receive(-1);
		largest_hole = pop_long(node);

		// send to the next node
		if (next < k) {
			push_long(next, smallest_hole);
			send(next);
		}
	}

	int gap = 0;
	int level = end;
	int overflow = 0;
	int last_level = end + 1;

	// push the disks through the pipe
	for (int i = 0; i < m; ++i) {
		long long disc = read_disc(i + 1);
		// look at all disc that can stuck in this part of the pipe
		// if last node, don't skipp discs passing through
		if (disc <= largest_hole && (disc > smallest_hole || next == k)) {
			// find stopping level for each disk
			if (max_level.count(disc)) {
				level = min(max_level[disc], last_level - 1);
			} else {
				if (disc <= smallest_hole) {
					level = end;
				} else {
					level = begin;
				}
			}
			// remember gaps between disks
			gap += max(0, last_level - level - 1);
			last_level = level;

			// pipe blocked, can't place more discs
			if (level == begin) {
				overflow = m - i - 1;
			}
		}
	}

	DBG("level", level)
	DBG("gap", gap)
	DBG("overflow", overflow)
	DBG("small", smallest_hole)
	DBG("large", largest_hole)
	DBG("---", "---")

	int new_level = level;

	// start sending from the last node
	if (next == k) {
		push(previous, overflow);
		send(previous);
	} else {
		// receive
		int node = receive(-1);
		int previous_overflow = pop(node);

		DBG("prev", previous_overflow)
		DBG("nlev", new_level)

		// update disks state
		if (overflow > 0) {
			overflow += max(0, previous_overflow - gap);
			DBG("over over", overflow)
		} else {
			new_level -= max(0, previous_overflow - gap);
			DBG("nlev", new_level)
			overflow = max(0, -new_level);
			DBG("over level", overflow)
		}

		// send to previous node
		if (previous >= 0) {
			push(previous, overflow);
			send(previous);
		}
		// print the result in node 0
		else {
			if (overflow > 0) {
				new_level = 0;
			}
			cout << new_level << endl;
		}
	}

	return 0;
}