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
#include <bits/stdc++.h>

using namespace std;

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


#define MAXN 3000001
typedef long long int ll;
ll MINI[MAXN];

int N,m;
int NO, ID;
ll last_min = 1e9 + 9;
int pocz,kon;
int stak;
int Nod = 100;
void wczytaj()
{
	N = PipeHeight();
	m = NumberOfDiscs();
	NO = NumberOfNodes();
	Nod = NumberOfNodes();
	ID = MyNodeId();
	pocz = ID * max(N / Nod,1) + 1;
	kon = (ID + 1) * max(N / Nod,1) + 1;
	kon = min(kon,N + 1);
	if(ID == NO - 1)
		kon = N + 1;
	if(pocz > N)
		exit(0);
}
void faza1()
{
	for(int q = pocz;q < kon;++q)
	{
		int i = q - pocz;
		if(!i)
			MINI[i] = min(last_min,HoleDiameter(q));
		else
			MINI[i] = min(MINI[i - 1],HoleDiameter(q));
	}
	if(kon <= N)
		PutLL(ID + 1,MINI[kon - pocz - 1]),
		Send(ID + 1);
}
void faza2()
{
	ll ost = DiscDiameter(stak);
	for(int i = kon - 1;i >= pocz;--i)
	{
		if(MINI[i - pocz] >= ost)
		{
			stak++;
			if(stak == m + 1)
			{
				printf("%d\n",i);
				if(ID)
					PutLL(ID - 1,-69),
					Send(ID - 1);
				exit(0);
			}
			ost = DiscDiameter(stak);
		}
	}
	if(!ID)
	 puts("0");
	else
		PutLL(ID - 1,stak),
		Send(ID - 1);
}
int main()
{
	wczytaj();
	if(ID > 0)
	{
		Receive(ID - 1);
		last_min = GetLL(ID - 1);
	}
	faza1();
	if(kon <= N)
	{
		Receive(ID + 1);
		stak = GetLL(ID + 1);
	}
	else stak = 1;
	if(stak == -69)
	{
		if(ID)
			PutLL(ID - 1,-69),
			Send(ID - 1);
		return 0;
	}
	faza2();
	return 0;
}