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
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <limits>

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


/***************************
 * Zmienne globalne
 ***************************/


//static std::map<long long, std::set<long long>> gPositions;  // diameter -> set<positions>

std::map<int, long long> gMinDiameters;  // nodeId -> minDiameter
long long gMinDiameter = std::numeric_limits<long long>::max();


/***************************
 * Prototypy funkcji
 ***************************/


bool IAmLast(int nodeId, int numberOfNodes);

int countCurrentPipeStart(int pipeHeight, int nodeId, int numberOfNodes);
int countCurrentPipeLength(int pipeHeight, int nodeId, int numberOfNodes);

void processPipe(int start, int height, std::vector<long long> &gDiameters, std::map<long long, int> &gDiametersMap, std::set<long long> &gDiametersSet);
void printPipe();

void sendMinDiameter(int nodeId);
void getMinDiameters(int nodeId, int numberOfNodes);
void printMinDiameters();

int processDiscs(int numberOfDiscs, int discNumber, int height, int nodeId, int numberOfNodes, int pipeHeight, std::vector<long long> &gDiameters, std::map<long long, int> &gDiameterrsMap, std::set<long long> &gDiametersSet);

bool smallerDiameterExist(long long diameter, std::set<long long> &gDiametersSet);
void decraseDiameter(long long diameter, std::map<long long, int> &gDiameterrsMap, std::set<long long> &gDiametersSet);


/***************************
 * Main
 ***************************/


int main()
{
	std::vector<long long> gDiameters;
	std::map<long long, int> gDiametersMap;  // diameter -> count
	std::set<long long> gDiametersSet;

	int numberOfNodes = NumberOfNodes();
	int myNodeId = MyNodeId();

	int pipeHeight = PipeHeight();
	int numberOfDiscs = NumberOfDiscs();

	//
	if (pipeHeight < numberOfNodes)
	{
		if (myNodeId != 0) { return 0; }
		else { numberOfNodes = 1; }
	}

	//
	int currentPipeStart = countCurrentPipeStart(pipeHeight, myNodeId, numberOfNodes);
	int currentPipeHeight = countCurrentPipeLength(pipeHeight, myNodeId, numberOfNodes);

	// DEBUG
//	std::cerr << "current start: " << currentPipeStart << ", current height: " << currentPipeHeight << std::endl;

	//
	processPipe(currentPipeStart, currentPipeHeight, gDiameters, gDiametersMap, gDiametersSet);
//	printPipe();

	// DEBUG
//	for (const auto &i : gDiameters)
//		std::cerr << i << " ";
//	std::cerr << std::endl;

	//
	sendMinDiameter(myNodeId);
	getMinDiameters(myNodeId, numberOfNodes);
//	printMinDiameters();

	//
	int discNumber = 1;

	if (myNodeId != 0)
	{
		Receive(myNodeId - 1);
		discNumber = GetInt(myNodeId - 1);

		// DEBUG
//		std::cerr << "Got: " << discNumber << std::endl;

		if (discNumber == 0)
		{
			if (IAmLast(myNodeId, numberOfNodes) == false) { PutInt(myNodeId + 1, 0); Send(myNodeId + 1); }
			return 0;
		}
	}

	//
	discNumber = processDiscs(numberOfDiscs, discNumber, currentPipeHeight, myNodeId, numberOfNodes, pipeHeight, gDiameters, gDiametersMap, gDiametersSet);

	if (IAmLast(myNodeId, numberOfNodes) == false)
	{
		PutInt(myNodeId + 1, discNumber);
		Send(myNodeId + 1);
	}
	else
	{
		if (discNumber != 0)
		{
			std::cout << 0 << std::endl;
		}

		std::cerr << "Result: " << discNumber << std::endl;
	}

	std::cerr << "DONE" << std::endl;
	return 0;
}


/*****************************
 * Funkcje
 *****************************/


bool IAmLast(int nodeId, int numberOfNodes)
{
	if (nodeId == numberOfNodes - 1) { return true; }
	else { return false; }
}


int countCurrentPipeStart(int pipeHeight, int nodeId, int numberOfNodes)
{
	return (numberOfNodes - nodeId - 1) * (pipeHeight / numberOfNodes) + 1;
}


int countCurrentPipeLength(int pipeHeight, int nodeId, int numberOfNodes)
{
	if (IAmLast(nodeId, numberOfNodes) == true)
	{
		return pipeHeight - nodeId * (pipeHeight / numberOfNodes);
	}

	return pipeHeight / numberOfNodes;
}


void processPipe(int start, int height, std::vector<long long> &gDiameters, std::map<long long, int> &gDiametersMap, std::set<long long> &gDiametersSet)
{
	gDiameters.resize(height);

	start = start + height - 1;

	for (int i = 0; i < height; ++i, --start)
	{
		long long diameter = HoleDiameter(start);

//		gPositions[diameter].insert(start);
		gDiameters[i] = diameter;
		gDiametersSet.insert(diameter);
		gDiametersMap[diameter]++;

		gMinDiameter = std::min(diameter, gMinDiameter);
	}
}


void printPipe()
{
//	std::cerr << "Pipe: " << std::endl;

//	for (const auto &i : gPositions)
//	{
//		std::cerr << i.first << ": ";

//		for (const auto &j : i.second)
//		{
//			std::cerr << j << " ";
//		}

//		std::cerr << std::endl;
//	}

//	std::cerr << "min diameter: " << gMinDiameter << std::endl;
}


void sendMinDiameter(int nodeId)
{
	while (--nodeId >= 0)
	{
		// DEBUG
//		std::cerr << "send: " << nodeId << std::endl;

		PutLL(nodeId, gMinDiameter);
		Send(nodeId);
	}
}


void getMinDiameters(int nodeId, int numberOfNodes)
{
	for (int i = nodeId + 1; i < numberOfNodes; ++i)
	{
		// DEBUG
//		std::cerr << "get: " << i << std::endl;

		Receive(i);
		gMinDiameters[i] = GetLL(i);
	}

	// DEBUG
//	std::cerr << "get: DONE" << std::endl;
}


void printMinDiameters()
{
	std::cerr << "Min Diameters: " << std::endl;

	for (const auto &i : gMinDiameters)
	{
		std::cerr << i.first << " " << i.second << std::endl;
	}
}


int processDiscs(int numberOfDiscs, int discNumber, int height, int nodeId, int numberOfNodes, int pipeHeight, std::vector<long long> &gDiameters, std::map<long long, int> &gDiameterrsMap, std::set<long long> &gDiametersSet)
{
	int position = 0;

	//
	long long minDiameter = std::numeric_limits<long long>::max();

	for (const auto &i : gMinDiameters)
	{
		minDiameter = std::min(i.second, minDiameter);
	}

	//
	for (int i = discNumber; i < numberOfDiscs + 1; ++i)
	{
		long long diameter = DiscDiameter(i);

		// DEBUG
//		std::cerr << "process disc: " << i << " " << diameter << std::endl;

		if (diameter > minDiameter || position > height) {
			// DEBUG
			std::cerr << "RETURN: " << diameter << " " << minDiameter << " " << position << std::endl;

			return i;
		}

		//
		long long temp_diameter = 0;

		do
		{
			position++;
			temp_diameter = gDiameters[position - 1];
			decraseDiameter(gDiameters[position - 1], gDiameterrsMap, gDiametersSet);

			// DEBUG
//			std::cerr << "while: " << position << " " << temp_diameter << std::endl;

		} while (smallerDiameterExist(diameter, gDiametersSet) || temp_diameter < diameter);

		if (position > height) {
			return i;
		}

		// DEBUG
//		std::cerr << "current pos: " << position << std::endl;
	}

	if (position >= height)
	{
		position = 0;
	}
	else
	{
		position = pipeHeight - (nodeId * (pipeHeight / numberOfNodes) + position) + 1;
	}

	std::cout << position << std::endl;
	return 0;
}


bool smallerDiameterExist(long long diameter, std::set<long long> &gDiametersSet)
{
//	std::cerr << "Smaller diameter input: " << diameter << std::endl;

	if (gDiametersSet.empty()) {
		// DEBUG
//		std::cerr << "Smaller diameter exists: EMPTY" << std::endl;
		return false;
	}

	if (diameter > *(gDiametersSet.begin()))
	{
		// DEBUG
//		std::cerr << "Smaller diameter exists: TRUE" << std::endl;

		return true;
	}
	else
	{
		// DEBUG
//		std::cerr << "Smaller diameter exists: TRUE" << std::endl;

		return false;
	}
}


void decraseDiameter(long long diameter, std::map<long long, int> &gDiametersMap, std::set<long long> &gDiametersSet)
{
	// DEBUG
//	std::cerr << "removing diameter: " << diameter << std::endl;

	if (--gDiametersMap[diameter] == 0) {
		//DEBUG
//		std::cerr << "erasing diameter: " << diameter << std::endl;

		gDiametersSet.erase(diameter);
	}
}