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
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;
typedef long long int lint;
#define span_n(x, n) x, x+n

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

const int NodesLim = 101;
int NodeId, Nodes;
int RevNodeId;
int N, M;
int NpN;
int LocalMinN, LocalMaxN;
int LocalMinM, LocalMaxM;

lint MinHole[NodesLim];
int MinDiscBlocked[NodesLim];

void Init();
void SolveUsingOneInstance();
void SolveUsingMultipleInstances();

int main() 
{
	Init();
	if(N <= 1000000 || M > N)
		SolveUsingOneInstance();
	else
		SolveUsingMultipleInstances();
	return EXIT_SUCCESS;
}

void Init()
{
	NodeId = MyNodeId();
	Nodes = NumberOfNodes();
	RevNodeId = Nodes - NodeId - 1;
	
	N = PipeHeight();
	M = NumberOfDiscs();
	NpN = N / Nodes;
	
	LocalMaxN = N - RevNodeId*NpN;
	LocalMinN = LocalMaxN - NpN + 1;
		
	LocalMaxM = min(M, (RevNodeId+1)*NpN);
	LocalMinM = min(M, RevNodeId*NpN)+1;
	if(NodeId == 0)
		LocalMinN = 1, LocalMaxM = M;
}

struct isZero
{
	bool operator () (int x) const
	{
		return x == 0;
	}
}is_zero;

void SolveUsingMultipleInstances()
{
	int size = LocalMaxN - LocalMinN + 1;
	lint* holes = new lint[size+1];
	for(int i = 1; i <= size; ++i)
		holes[i] = HoleDiameter(LocalMinN+i-1);
	for(int i = 2; i <= size; ++i)
		holes[i] = min(holes[i], holes[i-1]);
		
	MinHole[NodeId] = holes[size];

	for(int i = NodeId+1; i < Nodes; ++i)
	{
		PutLL(i, holes[size]);
		Send(i);
	}
	for(int i = 0; i < NodeId; ++i)
	{
		int source = Receive(-1);
		MinHole[source] = GetLL(source);
	}
	for(int i = 1; i <= NodeId; ++i)
		MinHole[i] = min(MinHole[i], MinHole[i-1]);
	
	int n = size;
	int m = LocalMinM;
	int lowestBlocked = NodeId;
	int overflow = 0;
	int posFromTop = N;
	int unused = 0;
	int minBlocked = 0;
	for(; m <= LocalMaxM; ++m)
	{
		lint disc = DiscDiameter(m);
		lint* it = lower_bound(span_n(MinHole, NodeId), disc-1, greater<lint>());
		
		int blockedI = it-MinHole;
		if(blockedI < lowestBlocked)
		{
			if(minBlocked = 0)
				minBlocked = m;
			for(int i = blockedI; i <= lowestBlocked; ++i)
				MinDiscBlocked[i] = m;
			lowestBlocked = blockedI;
		}
		else if(blockedI == NodeId && n)
		{
			while(n && disc > holes[n]) --n, ++unused;
			if(n > 0) posFromTop = LocalMinN + --n;
			else overflow = -1;
		}
		else if(blockedI == NodeId && !overflow)
		{
			overflow = -1;
		}
	}
	if(overflow == -1)
		overflow = minBlocked ? LocalMaxM - minBlocked : LocalMaxM - m + 1;
	if(overflow || lowestBlocked < NodeId)
		 posFromTop = LocalMinN;
	
	if(NodeId < Nodes-1)
	{
		int source = NodeId+1;
		Receive(source);
		int prevPosFromTop = GetInt(source);
		int prevOverflow = GetInt(source);
		for(int i = 0; i < source; ++i)
		{
			int minDiscBlock = GetInt(source);
			if(minDiscBlock) MinDiscBlocked[i] = minDiscBlock;
		}
			
		bool isBlocked = MinDiscBlocked[NodeId];
		int blocked = isBlocked 
			? (NodeId == 0 || MinDiscBlocked[NodeId-1] == 0
				? M-MinDiscBlocked[NodeId]+1 
				: (MinDiscBlocked[NodeId-1]) - MinDiscBlocked[NodeId])
			: 0;
		
		bool isSpecial = isBlocked && all_of(span_n(MinDiscBlocked, NodeId), is_zero)
			&& LocalMaxM == M;
		if(isSpecial)
		{
			n = size;
			m = MinDiscBlocked[NodeId];
			unused = 0;
			for(; m <= LocalMaxM && n; ++m)
			{
				lint disc = DiscDiameter(m);
				
				while(n && disc > holes[n]) --n, ++unused;
				if(n > 0) posFromTop = LocalMinN + --n;
				else overflow = LocalMaxM - m + 1;
			}

			posFromTop = LocalMinN + n;
		}
		posFromTop = min(posFromTop, prevPosFromTop);
		
		prevOverflow = max(0, prevOverflow-unused);
		
		if(overflow += max(0, LocalMinN-(posFromTop-prevOverflow)))
			posFromTop = LocalMinN;
		
		posFromTop = max(LocalMinN, posFromTop-prevOverflow);
	}
	
	if(NodeId > 0)
	{
		int target = NodeId-1;
		PutInt(target, posFromTop);
		PutInt(target, overflow);
		for(int i = 0; i < NodeId; ++i)
			PutInt(target, MinDiscBlocked[i]);
		Send(target);
	}
	else
	{
		printf("%d", overflow ? 0 : posFromTop);
	}
		
	delete [] holes;
}

void SolveUsingOneInstance()
{
	if(NodeId != 0)
		return;
	if(M > N)
	{
		puts("0");
		return;
	}
		
	vector<lint> holes(N+1);
	for(int i = 1; i <= N; ++i)
		holes[i] = HoleDiameter(i);
	for(int i = 2; i <= N; ++i)
		holes[i] = min(holes[i], holes[i-1]);
	
	int m = 1;
	int n = N;
	for(; m <= M; ++m)
	{
		lint disc = DiscDiameter(m);
		while(n && disc > holes[n]) --n;
		if(n == 0)
		{
			puts("0");
			return;
		}
		--n;
	}
	printf("%d", n+1);
}