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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <algorithm>
#include <vector>
#include <limits>
#include <cstdio>

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

int numberOfNodes;
int myNodeId;
int pipeHeight;
int numberOfDiscs;

bool isLastNode(int numberOfNodes, int myNodeId) {
	return myNodeId == numberOfNodes - 1;
}

std::pair<int, int> getRange(int nodeId, int range) {
	int lengthForOneNode = range / numberOfNodes;
	int begin = myNodeId * lengthForOneNode + 1;

	int end;
	if (isLastNode(numberOfNodes, myNodeId)) {
		end = range + 1;
	} else {
		end = (myNodeId + 1) * lengthForOneNode + 1;
	}

	return std::make_pair(begin, end);
}

int getIdOfFirstRelevantSet(long long *maxDiscsDiameters, long long minHoleDiameter, long long entryDiameter) {
	long long minDiscDiameterInRange = 0;
	long long maxDiscDiameterInRange = maxDiscsDiameters[0];
	for (int i = 0; i < numberOfNodes; ++i) {
		if (minHoleDiameter < maxDiscDiameterInRange
				&& entryDiameter <= minDiscDiameterInRange) {
			return i;
		}
		minDiscDiameterInRange = maxDiscDiameterInRange;
		maxDiscDiameterInRange = maxDiscsDiameters[i];
	}
	return -1;
}

std::pair<int, long long> getFirstRelevantDisc(long long *maxDiscsDiameters, long long minHoleDiameter, long long entryDiameter) {
	int setId = getIdOfFirstRelevantSet(maxDiscsDiameters, minHoleDiameter, entryDiameter);
	if (setId == -1) {
		return std::make_pair(-1, -1);
	}
	std::pair<int, int> discsRange = getRange(setId, numberOfDiscs);

	long long discDiameter;
	if (setId > 0) {
		discDiameter = maxDiscsDiameters[setId - 1];
	} else {
		discDiameter = 0;
	}
	for (int i = discsRange.first; i < discsRange.second; ++i) {
		discDiameter = std::max(discDiameter, DiscDiameter(i));
		if (minHoleDiameter < discDiameter && discDiameter <= entryDiameter) {
			return std::make_pair(i, discDiameter);
		}
	}
}

int getNumElementsInSet(int setId) {
	std::pair<int, int> range = getRange(setId, numberOfDiscs);
	return range.second - range.first;
}

int computeDiscsAbove(long long *maxDiscsDiameters, long long entryDiameter, int discId) {
	int beginSetId = 0;
	long long discDiameter = DiscDiameter(discId);
	int discsAbove = 0;
	while (maxDiscsDiameters[beginSetId] < discDiameter) {
		beginSetId++;
	}
	int endSetId = beginSetId;
	while (maxDiscsDiameters[endSetId] <= entryDiameter) {
		endSetId++;
	}

	if (beginSetId == endSetId) {
		while (discDiameter <= entryDiameter) {
			discsAbove++;
			discId++;
			discDiameter = std::max(discDiameter, DiscDiameter(discId));
		}
	} else {
		std::pair<int, int> beginRange = getRange(beginSetId, numberOfDiscs);
		discsAbove += beginRange.second - discId;
		for (int setId = beginSetId + 1; setId < endSetId; ++setId) {
			discsAbove += getNumElementsInSet(setId);
		}
		std::pair<int, int> range = getRange(endSetId, numberOfDiscs);
		discId = range.first;
		while (discDiameter <= entryDiameter) {
			discsAbove++;
			discId++;
			discDiameter = std::max(discDiameter, DiscDiameter(discId));
		}
	}
	return discsAbove;
}

int main() {
//	printf("asdf");
	numberOfNodes = NumberOfNodes();
	myNodeId = MyNodeId();
	pipeHeight = PipeHeight();
	numberOfDiscs = NumberOfDiscs();

	std::pair<int, int> pipeRrange = getRange(myNodeId, pipeHeight);

//	printf("%d: 1", myNodeId);

	std::vector<long long> holesDiameter;
	long long minHoleDiameter = std::numeric_limits<long long>::max();
	for (int i = pipeRrange.first; i < pipeRrange.second; ++i) {
		long long holeDiameter = HoleDiameter(i);
		holesDiameter.push_back(holeDiameter);
		minHoleDiameter = std::min(minHoleDiameter, holeDiameter);
	}

//	printf("%d: 2", myNodeId);

	for (int target = myNodeId; target < numberOfNodes; ++target) {
		PutLL(target, minHoleDiameter);
		Send(target);
	}

//	printf("%d: 3", myNodeId);

	long long entryDiameter = holesDiameter.front();
	for (int source = 0; source < myNodeId; ++source) {
		Receive(source);
		entryDiameter = std::min(entryDiameter, GetLL(source));
	}

//	printf("%d: 4", myNodeId);

	std::pair<int, int> discsRange = getRange(myNodeId, numberOfDiscs);
	long long maxDiscDiameter = 0;
	for (int i = discsRange.first; i < discsRange.second; ++i) {
		maxDiscDiameter = std::max(maxDiscDiameter, DiscDiameter(i));
	}

//	printf("%d: 5", myNodeId);

	for (int target = 0; target < numberOfNodes; ++target) {
		if (target != myNodeId) {
			PutLL(target, maxDiscDiameter);
			Send(target);
		}
	}

//	printf("%d: 1", myNodeId);

	long long maxDiscsDiameters[numberOfNodes];
	maxDiscsDiameters[myNodeId] = maxDiscDiameter;
	for (int i = 0; i < numberOfNodes - 1; ++i) {
		int source = Receive(-1);
		maxDiscsDiameters[source] = GetLL(source);
	}

//	printf("%d: 6", myNodeId);

	for (int i = 1; i < numberOfNodes; ++i) {
		maxDiscsDiameters[i] = std::max(maxDiscsDiameters[i - 1], maxDiscsDiameters[i]);
	}

//	printf("%d: 7", myNodeId);


	int readyToAbsorbe = 0;
	int readyToAbsorbeTmp = 0;
	int discsAbove = 0;
	int lastDiscAbs = -1;
	std::pair<int, long long> disc = getFirstRelevantDisc(maxDiscsDiameters, minHoleDiameter, entryDiameter);
	int discId = disc.first;
	long long discDiameter = disc.second;
	if (discId == -1) {
		readyToAbsorbe = pipeRrange.second - pipeRrange.first;
	} else {
		for (int hole = holesDiameter.size() - 1; hole >= 0; --hole) {
			if (holesDiameter[hole] >= discDiameter) {
				discId++;
				discDiameter = std::max(discDiameter, DiscDiameter(discId));
				lastDiscAbs = pipeRrange.first + hole;
				readyToAbsorbe += readyToAbsorbeTmp;
				readyToAbsorbeTmp = 0;
			} else {
				readyToAbsorbeTmp++;
			}
		}
		if (discDiameter <= entryDiameter) {
			discsAbove = computeDiscsAbove(maxDiscsDiameters, entryDiameter, discId);
			lastDiscAbs = pipeRrange.first - discsAbove;
		}
	}

//	printf("%d: 8", myNodeId);


	PutInt(0, readyToAbsorbe);
	PutInt(0, lastDiscAbs);
	PutInt(0, discsAbove);
	Send(0);

//	printf("%d: 9", myNodeId);

	if (myNodeId == 0) {
//		printf("%d: 10", myNodeId);
		int currentlyDiscsAbove = 0;
		int currentlyLastDisc = 0;
		for (int source = numberOfNodes - 1; source >= 0; --source) {
			Receive(source);
			int readyToAbsorbe = GetInt(source);
			int lastDisc = GetInt(source);
			int discsAbove = GetInt(source);
			if (lastDisc == -1) {
				currentlyDiscsAbove = 0;
			} else {
				if (currentlyDiscsAbove <= readyToAbsorbe) {
					currentlyLastDisc = lastDisc;
				} else {
					int moveUp = currentlyDiscsAbove - readyToAbsorbe;
					currentlyLastDisc = lastDisc - moveUp;
				}

				std::pair<int, int> sourceRange = getRange(source, numberOfDiscs);
				currentlyDiscsAbove = std::max(0, sourceRange.first - currentlyLastDisc);
			}
		}

		if (currentlyLastDisc > 0) {
			printf("%d", currentlyLastDisc);
		} else {
			printf("0");
		}
	}
}