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
#include <bits/stdc++.h>
#include "krazki.h"
#include "message.h"
using namespace std;

int nodes;
int mynode;

int n, m;

int n1;
int pocz[107];
int kon[107];

long long inf=(long long)1000000007*1000000007;

//t1 - dziury malejonce
//t2 - dyski rosnonce
//ma byc t1[i]<t2[j]

vector <long long> t1, t2;

long long m1[107];
long long m2[107];

int wyn;

inline long long dziura(int v)
{
	if (v>n)
		return 0;
	return HoleDiameter(v);
}

inline long long dysk(int v)
{
	if (v>m)
		return 0;
	return DiscDiameter(v);
}

int main()
{
	nodes=NumberOfNodes();
	mynode=MyNodeId();
	
	n=PipeHeight();
	m=NumberOfDiscs();
	
	if (m>n)
	{
		if (!mynode)
			printf("0\n");
		return 0;
	}
	wyn=n-m+1;
	n1=(n+nodes-1)/nodes;
	for (int i=0; i<nodes; i++)
	{
		pocz[i]=i*n1+1;
		kon[i]=i*n1+n1;
	}
	t1.resize(n1);
	t2.resize(n1);
	m1[mynode]=inf;
	m2[mynode]=0;
	for (int i=pocz[mynode]; i<=kon[mynode]; i++)
	{
		t1[i-pocz[mynode]]=dziura(i);
		t2[i-pocz[mynode]]=dysk(i);
	}
	for (int i=0; i<n1; i++)
	{
		m1[mynode]=min(m1[mynode], t1[i]);
		m2[mynode]=max(m2[mynode], t2[i]);
	}
	for (int i=0; i<nodes; i++)
	{
		PutLL(i, m1[mynode]);
		PutLL(i, m2[mynode]);
		Send(i);
	}
	for (int i=0; i<nodes; i++)
	{
		Receive(i);
		m1[i]=GetLL(i);
		m2[i]=GetLL(i);
	}
	for (int i=1; i<nodes; i++)
	{
		m1[i]=min(m1[i], m1[i-1]);
		m2[i]=max(m2[i], m2[i-1]);
	}
	if (mynode)
		t2[0]=max(t2[0], m2[mynode-1]);
	for (int i=1; i<n1; i++)
		t2[i]=max(t2[i-1], t2[i]);
	for (int i=0; i<nodes; i++)
	{
		if (m1[i]<m2[mynode])
		{
			t1.resize(2*n1);
			int s=pocz[i];
			int k=s+2*n1-1;
			for (int j=s; j<=k; j++)
				t1[j-s]=dziura(j);
			if (i)
				t1[0]=min(t1[0], m1[i-1]);
			for (int j=1; j<2*n1; j++)
				t1[j]=min(t1[j], t1[j-1]);
			int it=n1-1;
			
			for (int j=0; j<2*n1 && it>=0; j++)
			{
				while(it>=0 && t1[j]<t2[it])
				{
					wyn=min(wyn, j+s+it+pocz[mynode]-(m+1));
					it--;
				}
			}
			break;
		}
	}
	PutInt(0, wyn);
	Send(0);
	if (mynode)
		return 0;
	for (int i=0; i<nodes; i++)
	{
		Receive(i);
		wyn=min(wyn, GetInt(i));
	}
	wyn=max(wyn, 0);
	printf("%d\n", wyn);
	return 0;
}