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
#include <algorithm>
#include <vector>
#include <iostream>
#include "message.h"
#include "krazki.h"

using namespace std;
using ll = long long;

//// mock
//ll HoleDiameter(int i)
//{
//	static int tab[] = { -1, 5 ,6 ,4 ,3 ,6 ,2 ,3 };
//	return tab[i];
//}
//ll DiscDiameter(int i)
//{
//	static int tab[] = { -1, 3, 2, 5 };
//	return tab[i];
//}
//int PipeHeight()
//{
//	//static int tab[] = { -1, 3, 2, 5 };
//	return 7;
//}
//int NumberOfDiscs()
//{
//	//static int tab[] = { -1, 3, 2, 5 };
//	return 3;
//}


inline void solve(int n, int m)
{
	vector<ll> mins(n, 0);
	mins[0] = HoleDiameter(1);
	for (int i = 1; i < n; i++) {
		mins[i] = min(HoleDiameter(i + 1), mins[i - 1]);
	}

	int d = n;
	ll x;
	for (int i = 0; i < m; i++)
	{
		x = DiscDiameter(i + 1);
		while (d > 0 && mins[d - 1] < x) d--;
		d--;
	}

	cout << max(0, d + 1);
}
int main()
{
	int n = PipeHeight(), m = NumberOfDiscs();
	int id = MyNodeId();
	int nodesCount = NumberOfNodes();
	int step = ((float)n / nodesCount + 0.5);

	if(n < 1000000 || nodesCount == 1)
	{
		if(id == 0)
			solve(n, m);
		return 0;
	}

	// distribute
	int from = id*step, to = id == nodesCount - 1 ? n : min((id + 1)*step, n);
	int size = to - from;
	//cout << "from: " << from << " to: " << to << " size: " << size << endl;
	vector<ll> mins(size, 0);
	mins[0] = HoleDiameter(from + 1);
	for (int i = 1; i < size; i++) {
		mins[i] = min(HoleDiameter(from + i + 1), mins[i - 1]);
	}

	// mins to next nodes
	if(id == 0)
	{
		PutLL(id + 1, mins[size - 1]);
		Send(id + 1);
	}

	ll previousMin = numeric_limits<ll>::max();
	if(id > 0)
	{
		Receive(id - 1);
		previousMin = GetLL(id - 1);
		if(id < nodesCount - 1)
		{
			PutLL(id + 1, mins[size - 1]);
			Send(id + 1);
		}
	}
	
	// wait for parent
	int d = n;
	int i = 0;
	if (id < nodesCount - 1)
	{
		//cout << "node " << id << " waiting" << endl;
		Receive(id + 1);
		d = GetInt(id + 1);
		i = GetInt(id + 1);
		//cout << "node " << id << " end waiting. d=" << d << " i=" << i << endl;
	}

	ll x;
	for (; i < m; i++)
	{
		x = DiscDiameter(i + 1);
		//cout << "node " << id << " take disc: " << i + 1 << endl;
		while (d > 0 && d - 1 - from >= 0 && min(mins[d - 1 - from], previousMin) < x)
		{
			d--;
			//cout << "node: " << id << " d: " << d << " min: " << min(mins[d - 1 - from], previousMin) << " d - 1 - from= " << (d - 1 - from) << endl;
		}
		//cout << "node " << id << " exited while " << " d - 1 - from= " << (d - 1 - from) << endl;
		if(d - 1 - from < 0 && id > 0)
		{
			//cout << "node " << id << " finished d: " << d << " i=" << i << endl;
			break;
		}
		d--;
	}

	if (id == 0)
		cout << max(0, d + 1);
		//printf("%d\n", max(0, d + 1));
	else
	{
		PutInt(id - 1, d);
		PutInt(id - 1, i);
		Send(id - 1);
		//cout << "node: " << id << " sending to: " << id -1 << " d= " << d << " i = " << i << endl;
	}

	return 0;
}