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

const bool pr = false;
#include "message.h"
#if 0
	#include "krazki.cpp"
#else
	#include "krazki.h"
	#define printf(...) ;
#endif

vector<pair<long long int, int>> m;

int main() {
	// policzyc paczke i sprawdzic jej min
	const int N_N = NumberOfNodes();
	const int P_H = PipeHeight();
	const int N_D = NumberOfDiscs();
	const int P_Z = max(P_H, N_D) / max((NumberOfNodes()-3), 1) + 1;
	const int paczek_a = (P_H + P_Z-1)/ P_Z, paczek_b = (N_D+P_Z-1) / P_Z;
	if (paczek_a < 1 || paczek_a >= N_N - 1 || paczek_b < 1 || paczek_b >= N_N - 1)
		return EXIT_SUCCESS;
	const int c = MyNodeId();
	if (P_H < N_D) {
		if (c == 0) {
			printf("0\n");
		}
		return EXIT_SUCCESS;
	}
	if (c == 0) {
		if (pr) printf("P_Z: %d\n", P_Z);
		if(pr)printf("paczek_a: %d,  %d\n", paczek_a, paczek_b);
		for (int i = 1; i <= paczek_a; i++) {
			if (i < 1 || i >= N_N) return EXIT_SUCCESS;
			Receive(i);
			long long int mn = GetLL(i);
			int mnp = GetInt(i);
			if(pr)printf("Odebrano min paczki_a %d => %lld @ %d\n", i, mn, mnp);
			if (!m.size() || m.back().first > mn) {
				m.emplace_back(mn, mnp);
			}
		}
		if(pr)printf("Odebrane paczki a: ");
		if(pr)for (auto x : m)
			printf("%lld @ %d   ", x.first, x.second);
		if(pr)printf("\n");
		for (int i = N_N-paczek_b; i < N_N; i++) {
			if (i < 1 || i >= N_N) return EXIT_SUCCESS;
			Receive(i);
			long long int mx = GetLL(i);
			int mxp = GetInt(i);
			vector<pair<long long int, int>>::reverse_iterator it = lower_bound(m.rbegin(), m.rend(), make_pair(mx, 0));
			if (it == m.rend()) 
				PutLL(i, 1LL<<62);
			else
				PutLL(i, it->first);

			if (it == m.rbegin()){
				PutInt(i, P_H+1);
			} else {
				it--;
				if(pr)printf("Odebrano max paczki_b %d => %lld @ %d\n", i, mx, mxp);
				if(pr)printf("Jej znaleziona paczka to %lld @ %d\n", it->first, it->second);
				PutInt(i, it->second);
			}
			Send(i);
		}
		int mn = 1<<30;
		for (int i = N_N-paczek_b; i < N_N; i++) {
			if (i < 1 || i >= N_N) return EXIT_SUCCESS;
			Receive(i);
			int cw = GetInt(i);
			if(pr)printf("wyn od paczki %d to %d\n", i, cw);
			mn = min(mn, cw);
		}
		#ifdef printf
			#undef printf
			printf("%d\n", mn);
			#define printf(...) ;
		#else
			printf("%d\n", mn);
		#endif

		return EXIT_SUCCESS;
	}

	if (c <= paczek_a) {
		int na = (c-1) * P_Z+1, nb = min(na + P_Z, P_H+1);
		long long int mn = 1LL<<62;
		int mnp = 1;
		for (int i = na; i < nb; i++) {
			if (HoleDiameter(i) < mn) {
				mn = HoleDiameter(i);
				mnp = i;
			}
		}
		PutLL(0, mn);
		PutInt(0, mnp);
		Send(0);
		if(pr)printf("Node %d ma paczke_a %d-%d, min to %d\n", c, na, nb-1, mn);
	}

	if (c >= N_N - paczek_b) {
		vector<long long int> D;
		int na = (c-N_N+paczek_b) * P_Z+1, nb = min(na + P_Z, N_D+1);
		long long int mx = 0;
		int mxp = 0;
		for (int i = na; i < nb; i++) {
			if (DiscDiameter(i) >= mx) {
				mx = DiscDiameter(i);
				mxp = i;
			}
			D.emplace_back(mx);
		}
		PutLL(0, mx);
		PutInt(0, mxp);
		Send(0);
		Receive(0);
		int mnp;
		vector<long long int> V;
		long long int mn = GetLL(0);
		mnp = GetInt(0);
		int najnizszy_pos = mnp;
		for (int i = na; i < nb && najnizszy_pos <= P_H; i++) {
			if (D[0] > HoleDiameter(najnizszy_pos)) {
				break;
			}
			najnizszy_pos++;
		} 
		// najnizszy_pos to miejsce w ktorym sie zatrzyma najmniejszy (pierwszy) krazek z paczki
		najnizszy_pos--; 

		int pocz_paczki = mnp - (mnp - 1) % P_Z;
		pocz_paczki = min(pocz_paczki, max(mnp - nb+na-10, 1));
		najnizszy_pos = min(najnizszy_pos, P_H);
		for (int i = pocz_paczki; i <= najnizszy_pos; i++) {
			if (HoleDiameter(i) < mn) {
				mn = HoleDiameter(i);
			}
			V.emplace_back(mn);
		}
		int pos = V.size()-1;
		if(pr)printf("D.size: %u, V: %u\n", D.size(), V.size());
		for (int i = 0; i < D.size() && pos >= 0; i++) {
			while (D[i] > V[pos] && i < V.size()) {
				pos--;
			}
			if(pr)printf("krazek %d trafi do %d\n", D[i], pos + max(mnp - nb+na-1, 1)); 
			pos--;
		}
		pos += pocz_paczki;
		if(pr)printf("ostatni krazek z paczki sie skonczy na %d, z gory bedize jeszcze %d\n", pos, N_D - nb + 1);
		pos -= N_D - nb;
		PutInt(0, max(pos, 0));
		Send(0);
	}
}