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
#include <cstdlib>

#include <bits/stdc++.h>

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

using namespace std;

void prezes() {
	int n = PipeHeight(), m = NumberOfDiscs();
	int nodes = NumberOfNodes();

	if (m < nodes*10) {
		PutInt(1, 0);
		Send(1);
		PutInt(1, m);
		Send(1);

		Receive(1);
		long long wyn = GetInt(1);
		
		printf("%d", wyn+1);

		for (int i = 2; i < nodes; i++) {
			PutInt(i, -1);
			Send(i);
		}

		return;
	}

	nodes--;

	int pod = m / nodes;
	int res = (m % nodes) + pod;
	int start = 0;
	for (int i = 1; i <= nodes-1; i++) {
		PutInt(i, start);
		Send(i);
		start += pod;

		PutInt(i, pod);
		Send(i);
	}

	PutInt(nodes, start);
	Send(nodes);
	PutInt(nodes, m-start);
	Send(nodes);

	int wyn[nodes];

	for (int i = 1; i <= nodes; i++) {
		Receive(i);
		wyn[i-1] = GetInt(i);
	}

	int gleb = wyn[0];
	for (int i = 0; i < nodes; i++) {
		if (wyn[i] == -1) {
			printf("0");
			return;
		}

		if (gleb-pod < wyn[i])
			gleb -= pod;
			if (gleb < -1) {
				printf("0");
				return;
			}
		else
			gleb = wyn[i];

		if (i == nodes-2)
			pod = res;
	}

	printf("%d", gleb+1);
}

int main() {
	if (MyNodeId() == 0) {
		prezes();

		return EXIT_SUCCESS;
	}

	Receive(0);	
	int start = GetInt(0);

	if (start == -1)
		return EXIT_SUCCESS;

	Receive(0);
	int n = PipeHeight(), m = GetInt(0);
	//printf("%d %d\n", start, m); 
	long long* r = new long long[n], *k = new long long[n];

	for (int i = 0; i < n; i++)
		r[i] = HoleDiameter(i+1);
	for (int i = 0; i < m; i++)
		k[i] = DiscDiameter(i+1+start);

	long long maks[n];
	maks[0] = r[0];
	for (int i = 1; i < n; i++)
		maks[i] = min(r[i], maks[i-1]);

	//for (int i = 0; i < n; i++)
	//	printf("%d ", maks[i]);
	//printf("\n");

	int gleb = n;
	for (int i = 0; i < m; i++) {
		long long* cgleb = upper_bound(maks, maks+gleb, k[i], greater<long long>());

		//printf("%d\n", cgleb-maks);
		if (cgleb == maks && *cgleb < k[i]) {
			gleb = -1;
			break;
		}

		cgleb--;
		if (*cgleb < k[i]) {
			gleb = -1;
			break;
		}


		long long vgleb = cgleb - maks;
		gleb = (int) vgleb;
	}

	PutInt(0, gleb);
	Send(0);
	//printf("%d %d\n", MyNodeId(), gleb+1);

	return EXIT_SUCCESS;
}