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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <algorithm>
#include <iostream>
#include <cassert>
#include <vector>
#include <cstdio>
#include <array>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>

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

namespace {
	using namespace std;

	typedef vector<int> vint;
	typedef long long unsigned ulo;
	typedef long long lint;

	void __attribute__((unused)) spacje(int k) {
		int magic;
		static stack<int*> stos;
		while (!stos.empty() && stos.top() <= &magic) stos.pop();
		stos.push(&magic);
		for (int n = k * stos.size(); n--;) fprintf(stderr, " ");
	}

    #define BEND(x) begin(x), end(x)

	#define BOTH(x) for (int x = 0; x < 2; ++x)

	#define UPlt(xx, yy) (xx) = min(xx, yy)
	#define UPgt(xx, yy) (xx) = max(xx, yy)

	struct in_t {
		template<class T>
		operator T () {
			static bool __attribute__((unused)) dummy = ios_base::sync_with_stdio(false);
			T x;
			cin >> x;
			return x;
		}
	};
	in_t __attribute__((unused)) in;

	class Print {
		ostream & os;
		bool nfirst;
	 public:
		Print(ostream & os = cout) : os(os), nfirst(false) {}
		~Print() { os << endl; }
		template<class T> Print & operator , (T const& e) {
			if (nfirst) os << " ";
            os << e; nfirst = true; return *this;
        }
	};

	#define print Print{cout},
	#define prerr Print{cerr},

	template <class T>
	struct Range {
		struct Intit {
			T x; Intit(T x) : x(x) {}
			T & operator * () { return x; };
			bool operator != (Intit other) { return x < other.x; }
			void operator ++ () { ++x; }
		};

		T b, e;
		explicit Range(T n) : b(0), e(n) {}
		Range(T b, T e) : b(b), e(e) {}
		Intit begin() { return b; }
		Intit end() { return e; }
	};

	template <class T>
	Range<T> range(T n) { return Range<T>(n); }

	template <class T>
	Range<T> range(T b, T e) { return Range<T>(b, e); }

	template <class T>
	Range<T> inclusive(T b, T e) { return Range<T>(b, e + 1); }

	#define _ __attribute__((unused)) _
};

/* #define dprintf(...) if (1) spacje(2), fprintf(stderr, __VA_ARGS__) */
#define dprintf(...) {}

static int myid;
static int nproc;

static int ibegin(int i, int n) {
    return i * (n / nproc) + 1;
}

static int iend(int i, int n) {
    return (i == nproc - 1 ? n : (i+1) * (n / nproc)) + 1;
}

Range<int> irange(int i, int n) {
    return Range<int>(ibegin(i, n), iend(i, n));
}

int main() {

    nproc = NumberOfNodes();
    myid = MyNodeId();

    int const n_holes = PipeHeight();
    int const n_discs = NumberOfDiscs();

    if (n_holes < nproc) {
        nproc = n_holes;
        if (n_discs > n_holes) {
            if (myid == 0) print 0;
            return 0;
        }

        if (myid >= nproc) {
            return 0;
        }
    }

    lint my_chunk_width = 0;
    vector<lint> my_chunk;
    for (int i : irange(myid, n_discs)) {
        dprintf("grabbing disk %d\n", i);
        // reorder, bo myślałem, bo zła numeracja dysków
        lint x = DiscDiameter(n_discs - i + 1);
        my_chunk.push_back(x);
        UPgt(my_chunk_width, x);
    }

    lint my_bottom_w = 1ll << 60;
    for (int i : irange(myid, n_holes)) {
        dprintf("grabbing hole %d\n", i);
        lint x = HoleDiameter(i);
        UPlt(my_bottom_w, x);
    }

    // propagate bottom_ws

    vector<lint> bottom_ws(nproc);

    for (int i : range(nproc)) {
        if (i == myid) continue;
        PutLL(i, my_bottom_w);
        Send(i);
    }

    for (int i : range(nproc)) {
        if (i == myid) {
            bottom_ws[i] = my_bottom_w;
        } else {
            Receive(i);
            bottom_ws[i] = GetLL(i);
        }
    }

    {
        lint curr_w = 1ll << 60;
        for (int i : range(nproc)) {
            UPlt(curr_w, bottom_ws[i]);
            bottom_ws[i] = curr_w;
        }
    }

    //
    
    int here_node = 0;
    dprintf("here: %d\n", here_node);
    for (int i : range(nproc)) {
        if (bottom_ws[i] >= my_chunk_width) {
            here_node = min(nproc - 1, i + 1);
        }
    }
    int here_start = ibegin(here_node, n_holes);
    int here_end = iend(here_node, n_holes) + my_chunk.size();
    UPlt(here_end, PipeHeight() + 1);
    dprintf("%d -- %d\n", here_start, here_end);

    lint tmpwidth = 1ll << 60;
    vector<lint> buffer;
    for (int i : range(here_start, here_end)) {
        UPlt(tmpwidth, HoleDiameter(i));
        buffer.push_back(tmpwidth);
    }

    int depth = here_end - 1;
    int idisk = my_chunk.size() - 1;
    while (depth >= 0 && idisk >= 0) {
        lint disk_width = my_chunk[idisk];
        dprintf("buf at %d = %lld\n", depth, buffer[depth - here_start]);
        if (depth < here_start || disk_width <= buffer[depth - here_start]) {
            dprintf(
                "local %d of %lld fits at %d\n",
                idisk,
                disk_width,
                depth
            );
            // fits
            --idisk;
        }
        --depth;
    }

    if (nproc > 1 && myid == nproc - 1) {
        PutInt(nproc - 2, depth);
        Send(nproc - 2);
    } else {
        int their_depth = PipeHeight();
        if (nproc > 1) {
            Receive(myid + 1);
            their_depth = GetInt(myid + 1);
        }
        UPlt(depth, their_depth - (int)my_chunk.size());
        if (myid == 0) {
            if (depth < 0) print 0;
            else print (depth + 1);
        } else {
            PutInt(myid - 1, depth);
            Send(myid - 1);
        }
    }

	return 0;
}