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
#include <bits/stdc++.h>
#include "krazki.h"
#include "message.h"
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
//#define REP(i,n) FOR(i,1,(n)+1)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define LLINF 1000000000000000001LL
//#define VAR(n,v) typeof(v) n=(v)
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)

#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

int MAX_NODES = 100;
static const int MASTER = 0;

void get_range(int n, int node_id, int &u, int &v) {
	u = node_id*(n/MAX_NODES) + min(node_id, n%MAX_NODES);
	v = u + n/MAX_NODES + (node_id < n%MAX_NODES);
}

int main() {
	MAX_NODES = NumberOfNodes();
	int node_id = MyNodeId();
	int n = PipeHeight();
	int u, v;
	get_range(n, node_id, u, v);

	vector<ll> pipe;
	FOR(i,u,v) {
		ll r = HoleDiameter(n-i);
		pipe.pb(r);
	}

	//smooth pipe
	for(int i=pipe.sz-2; i>=0; --i) {
		pipe[i] = min(pipe[i], pipe[i+1]);
	}

	PutLL(MASTER, pipe.sz ? pipe[0] : LLINF);
	Send(MASTER);

	dprintf("node %d) range (%d, %d) ", node_id, u, v);
	dptab(ALL(pipe));

	vector<ll> bottlenecks(MAX_NODES);
	if(MASTER == node_id) {
		FOR(node,0,MAX_NODES) {
			Receive(node);
			bottlenecks[node] = GetLL(node);
		}
		for(int i=MAX_NODES-2; i>=0; --i) {
			bottlenecks[i] = min(bottlenecks[i], bottlenecks[i+1]);
		}
		FOR(node,0,MAX_NODES) {
			PutLL(node, node ? bottlenecks[node-1] : 0);
			PutLL(node, bottlenecks[node]);
			PutLL(node, node < MAX_NODES - 1 ? bottlenecks[node+1] : LLINF);


			//FOR(i,0,bottlenecks.sz) {
			//	PutLL(node, bottlenecks[i]);
			//}
			Send(node);
		}
	}

	Receive(MASTER);
	ll prev = GetLL(MASTER);
	ll min_width = GetLL(MASTER);
	ll next = GetLL(MASTER);
	//FOR(i,0,bottlenecks.sz) {
	//	bottlenecks[i] = GetLL(MASTER);
	//}

	//smooth pipe once again
	//TODO delete?
	FOR(i,0,pipe.sz) {
		pipe[i] = min(pipe[i], next);
	}
	if(pipe.sz) {
		dprintf("node %d) smoothed pipe ", node_id);
		dptab(ALL(pipe));
	}
	if(!pipe.sz) {
		PutInt(MASTER, -1);
		PutInt(MASTER, 0);
		Send(MASTER);
		return 0;
	}

	int m = NumberOfDiscs();
	int max_height = -1; //TODO initialize
	int m_prim = 0;

	FOR(i,0,m) {
		ll r = DiscDiameter(i+1);
		if(r > next) {
			dprintf("node %d)	disc r=%lld too wide\n", node_id, r);
			break; // disc too wide, stops higher
		} else if(!(node_id == MASTER) && (r <= pipe[0] && max_height == -1)) {
			dprintf("node %d)	disc r=%lld too tight\n", node_id, r);
			continue; //disc to tight, goes lower
		} else {
			++m_prim;
			int height_u = 0;
			int height_v = pipe.sz;
			while(height_v - height_u > 1) {
				int height_m = (height_u + height_v) / 2;
				if(pipe[height_m] >= r) height_v = height_m;
				else height_u = height_m;
			}
			if(node_id==MASTER && r<=pipe[0]) height_u = -1;
			max_height = max(max_height + 1, height_u + 1);
			dprintf("node %d)	max_h=%d\n", node_id, max_height);			
		}
	}
	if(max_height != -1) max_height += u;
	PutInt(MASTER, max_height);
	PutInt(MASTER, m_prim);
	Send(MASTER);

	dprintf("node %d)	sending %d (stack %d)\n", node_id, max_height, m_prim);

	if(MASTER == node_id) {
		int global_height = -1;
		FOR(node,0,MAX_NODES) {
			Receive(node);
			max_height = GetInt(node); 
			m_prim = GetInt(node); 
			global_height = max(global_height + m_prim, max_height);
		}
		printf("%d\n", max(0,n-global_height));
	}

	///get, n, m
	//master determine no active threads
	///determine pipe range
	///read pipe range
	///smooth pipe
	///send result
	///master:
	///	wait for all result
	///	reduce
	///	notify all (send ball) (do not forgot about yourself)
	///all:
	///	wait for notification (get ball)
	///propagate
	///for each dick do binary search
	///until you'll recive too wide disc
	///send (number of discs and) position of last disc
	///master:
	///	reduce
	//	print result
	//quit
	return 0;
}