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
#include <iostream>
#include "krazki.h"
#include "message.h"
using namespace std;

int pipeH;
int disks;

int getInstanceDiskSize(int instance)
{
	if (instance == NumberOfNodes() -1) return disks/(NumberOfNodes()-1) + disks%(NumberOfNodes()-1);
	else return disks/(NumberOfNodes()-1);
}

int findH(int from, int size)
{
	int currentH = pipeH+1;
	for (int i=from;i<from+size;i++)
	{
		bool found = false;
		for (int j=1;j<currentH;j++)
		{
			if (DiscDiameter(i) > HoleDiameter(j))
			{
				currentH = j-1;
				j=pipeH+1;
				found=true;
			}
		}
		if (!found) currentH--;
	}
	return currentH;
}

int main()
{
	pipeH = PipeHeight();
	disks = NumberOfDiscs();
	int instanceNumber = MyNodeId();


	if (instanceNumber == 0)
	{
		int currentH = pipeH+1;
		for (int node = 1;node<NumberOfNodes();)
		{
			int r = Receive(node);
			if (r == node)
			{
				int lastDiskPosition = GetInt(r);
				int itsDiskSize = getInstanceDiskSize(r);
				if (lastDiskPosition < currentH)
				{
					currentH = lastDiskPosition;
				}
				else
				{

					currentH -= itsDiskSize;
				}
				node++;
			}
		}

		if (currentH < 1)
		{
			cout << 0;
		}
		else
		{
			cout << currentH;
		}
		cout << endl;
	}
	else
	{
		int diskSize = getInstanceDiskSize(instanceNumber-1);
		int myDiskSize = getInstanceDiskSize(instanceNumber);
		int beginDisk = diskSize*(instanceNumber-1)+1;
		int myH = findH(beginDisk, myDiskSize);
		PutInt(0, myH);
		Send(0);
	}
}