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

typedef long long ll;
const int inf = 1000000001;
const ll  infll = (ll) inf * inf;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, b, e)  for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (e); --i)

template <class T, class U> bool remin(T &a, const U &b) { return b < a ? a = b, true : false; }
template <class T, class U> bool remax(T &a, const U &b) { return b > a ? a = b, true : false; }
template <class T> int size(const T &t) { return (int) t.size(); }

// najwiekszy argument, dla ktorego f(arg) zwraca prawde
// funkcja logiczna powinna byc nierosnaca
template <class T, class F>
T bin_search_max(F f, T lo, T up, T eps = 1) {
	T cnt = up - --lo;
	T mid, step;
	while (cnt >= eps) {
		step = cnt / 2;
		mid = up - step;
		if (!f(mid)) {
			up = mid - eps;
			cnt -= step + eps;
		} else {
			cnt = step;
		}
	}
	return up;
}

const int MAXN = 100;
const int n = PipeHeight();
const int m = NumberOfDiscs();
const int N = min(NumberOfNodes(), n);
const int me = MyNodeId();
const bool isMaster = !me;
const bool isLast = me+1 == N;

vector<ll> disc; // cache
vector<ll> holeMinUpTo;
ll partMax[MAXN];
ll currMin[MAXN];

void calcAndBcastTotMaxBlock() {
	const int l = m < N ? 1 : m/N;
	const int e = m - l*me;
	const int b = (isLast ? 0 : max(0, e-l));
	
	ll mx = 0;
	disc.reserve(e <= 0 ? 0 : e - b);
	FORD (i, e, b+1) {
		ll x = DiscDiameter(i);
		disc.push_back(x);
		remax(mx, x);
	}
	assert(disc.size() == disc.capacity());
	
	if (!isMaster) {
		PutLL(0, mx);
		Send(0);
	} else {
		REP (_, N-1) {
			int src = Receive(-1);
			partMax[src] = GetLL(src);
		}
		partMax[0] = mx;
		FORD (i, N-1, 1) {
			remax(partMax[i-1], partMax[i]);
		}
	}
	if (!isMaster) {
		Receive(me-1);
		partMax[0] = GetLL(me-1);
	}
	if (!isLast) {
		PutLL(me+1, partMax[0]);
		Send(me+1);
	}
}

ll checkPart(int q) {
	const int l = m < N ? 1 : m/N;
	const int e = m - l*me;
	q += m-e;
	ll mn = infll;
	REP (i, size(disc)) {
		remin(mn, HoleDiameter(q++));
		if (disc[i] > mn) {
			return 0;
		}
	}
	return mn;
}

void calcMinHole() {
	const int l = n/N;
	const int b = me * l + 1;
	const int e = isLast ? n+1 : b+l;
	
	holeMinUpTo.reserve(e - b);
	ll mn = infll;
	FOR (i, b, e-1) {
		remin(mn, HoleDiameter(i));
		holeMinUpTo.push_back(mn);
	}
}

bool checkUpTo(int q) {
	const int l = n/N;
	const int b = me * l + 1;
	const int e = isLast ? n+1 : b+l;
	
	if (q < b) {
		return true;
	}
	if (q >= e) {
		return partMax[0] <= holeMinUpTo.back();
	}
	return partMax[0] <= holeMinUpTo[q-b];
}

bool ask(int q) {
	if (!isLast) {
		PutInt(1, q);
		Send(1);
	}
	currMin[0] = checkUpTo(q-1) ? checkPart(q) : 0;
	REP (_, N-1) {
		int src = Receive(-1);
		currMin[src] = GetLL(src);
	}
	
	ll mn = infll;
	REP (i, N) {
		// czy dany partmax miesci sie w currMn gorniejszego
		if (partMax[i] > mn) {
			return false;
		}
		remin(mn, currMin[i]);
	}
	return mn;
}

void doTasks() {
	while (true) {
		Receive(me-1);
		int q = GetInt(me-1);
		if (!isLast) {
			PutInt(me+1, q);
			Send(me+1);
		}
		if (q < 0) {
			break;
		}
		ll ch = checkUpTo(q-1) ? checkPart(q) : 0;
		PutLL(0, ch);
		Send(0);
	}
}

void run() {
	if (m > n) {
		if (isMaster) {
			cout << "0\n";
		}
		return;
	}
	if (me >= N) {
		return;
	}
	
	calcAndBcastTotMaxBlock();
	calcMinHole();
	
	if (!isMaster) {
		doTasks();
	} else {
		int ans = bin_search_max(ask, 1, n-m+1);
		cout << ans << '\n';
		if (!isLast) {
			PutInt(1, -1);
			Send(1);
		}
	}
}

int main() {
	run();
}