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
#include <stdio.h>

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

long long findBottleneckIndex(int pipeHeight, int beginIndex, int endIndex)
{
	long long lastIndex = pipeHeight;
	for (int i = beginIndex; i < endIndex; i++)
	{
		int index = -1;
		while(index + 1 != lastIndex && DiscDiameter(i + 1) <= HoleDiameter(index + 1 + 1)) 
		{
			//printf("leci krazek [%d]=%d przez [%d]=%d\n", i, discs[i], index, pipe[index + 1]);
			index++;
		}
		lastIndex = index;
		if (index <= 0) 
		{
			return index - (beginIndex - endIndex - i - 1) + 1;
		}
		//printf("%d utknal %d\n", i, index);
	}
	return lastIndex + 1;
}

int main()
{
	int numberOfNodes = NumberOfNodes();
	int nodeId = MyNodeId();
	
	int pipeHeight = PipeHeight();
	int numberOfDiscs = NumberOfDiscs();
	int discPerInstance = numberOfDiscs/numberOfNodes;
	if (discPerInstance < 100) 
	{
		discPerInstance = 100;
	}
	int diskTakenSoFar = nodeId * discPerInstance;
	int begin = 0;
	int end = 0;
	if (numberOfDiscs - diskTakenSoFar > 0)
	{
		begin = diskTakenSoFar;
		end = diskTakenSoFar + discPerInstance >= numberOfDiscs ? numberOfDiscs : diskTakenSoFar + discPerInstance;
	}
	
	long long k1 = findBottleneckIndex(pipeHeight, begin, end);
	// findout about nextNode's result and combine it in new result then said previous Node about result
	long long k = k1;
	if (nodeId < numberOfNodes - 1)
	{
		Receive(nodeId + 1);
		long long k2 = GetLL(nodeId + 1);
		k = k1 < k2 ? k1 : k2 - (end - begin) + 1;
	}
	
	if (nodeId > 0) 
	{
		PutLL(nodeId - 1, k);
		Send(nodeId - 1);
	}
	else
	{
		printf("%lld", k);
	}
}