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

using namespace std;

int solve(int begindx, int endindx){
	vector<int> pipes;
	for(int a = begindx; a < endindx; a++){
		pipes.push_back(HoleDiameter(a + 1));
	}

	int minp = pipes[0];
	for(int i = 0; i < pipes.size(); i ++){
		minp = min(minp, pipes[i]);
		pipes[i] = minp;

	}

	long int ret = PipeHeight() - NumberOfDiscs() + 1;
	bool stuck = false;

	int discmax = 0;
	for(int i = 0; i < NumberOfDiscs();  i++){
		int curdisc = DiscDiameter(i+ 1);
		if(discmax < curdisc){
			curdisc = discmax;
		} else {
			continue;
		}
		auto a = lower_bound(pipes.rbegin(), pipes.rend(), DiscDiameter(i + 1));
		long int index = pipes.size() - (a - pipes.rbegin());
		if(a != pipes.rbegin()) {
			stuck = true;
			auto newminlvl = begindx + index + 1 - NumberOfDiscs() + i;

			ret = min(ret, newminlvl); 
		}
		
	}
	if(!stuck){
		return PipeHeight() - NumberOfDiscs() + 1;
	}
	return ret;
}

int main(){
	if(PipeHeight() > 40){
		if(MyNodeId() > 0){
			int step = PipeHeight() / (NumberOfNodes() - 2);
			int lowr = 0;
			int highr = 0;

			lowr = (MyNodeId() - 1) * step;
			highr = min(MyNodeId() * step, PipeHeight());

			int res = solve(lowr, highr);
			PutInt(0, res);
			Send(0);

		} else {
			int result = PipeHeight() - NumberOfDiscs();
			for (int instancja = 1; instancja < NumberOfNodes(); ++instancja) {
				Receive(instancja);
				result = std::min(result, GetInt(instancja));
			}
			std::cout << std::max(0, result) << endl;
		}
	}
	else if(MyNodeId() == 0){
		std::cout << max(0, solve(0, PipeHeight()));
	}
	return EXIT_SUCCESS;
}