#include <cstdlib> #include <iostream> #include "krazki.h" #include "message.h" int main() { // Tylko zerowy komputer coś liczy. int id = MyNodeId(); int nn = NumberOfNodes(); int nd = NumberOfDiscs(); int ph = PipeHeight(); if (nd > ph) { if (id == 0) std::cout << 0 << std::endl; return EXIT_SUCCESS; } int dh = std::max(100, ph / nn + 1); int lhi = id * dh + 1; int hhi = std::min(ph, (id + 1) * dh); if (hhi < lhi) return EXIT_SUCCESS; long long *pipe = new long long[hhi - lhi + 1]; int c = 0; long long md = HoleDiameter(1) + 1; long long pmd = md; long long di; for (int i = lhi; i <= hhi; i++) { di = HoleDiameter(i); if (md > di) md = di; pipe[c++] = md; } if (lhi > 1) { Receive(id - 1); pmd = GetLL(id - 1); md = std::min(md, pmd); } if (hhi < ph) { PutLL(id + 1, md); Send(id + 1); } for (int i = lhi; i <= hhi; i++) { if (pipe[i-lhi] <= pmd) break; pipe[i-lhi] = pmd; } int i = 1; int r = 0; if (hhi < ph) { Receive(id + 1); i = GetInt(id + 1); r = GetInt(id + 1); } if (r == 0) { if (nd - i + 1 > hhi) { r = 1; } else { --c; while (c >= 0 && i <= nd) { di = DiscDiameter(i); while (c >= 0 && pipe[c] < di) --c; if (c >= 0) { ++i; --c; } } if (i > nd) { r = 2; i = c; } } } else i += hhi - lhi + 1; if (lhi > 1) { PutInt(id - 1, i); PutInt(id - 1, r); Send(id - 1); return EXIT_SUCCESS; } int depth = 0; if (r == 2) depth = i + 2; std::cout << depth << std::endl; return EXIT_SUCCESS; }
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 | #include <cstdlib> #include <iostream> #include "krazki.h" #include "message.h" int main() { // Tylko zerowy komputer coś liczy. int id = MyNodeId(); int nn = NumberOfNodes(); int nd = NumberOfDiscs(); int ph = PipeHeight(); if (nd > ph) { if (id == 0) std::cout << 0 << std::endl; return EXIT_SUCCESS; } int dh = std::max(100, ph / nn + 1); int lhi = id * dh + 1; int hhi = std::min(ph, (id + 1) * dh); if (hhi < lhi) return EXIT_SUCCESS; long long *pipe = new long long[hhi - lhi + 1]; int c = 0; long long md = HoleDiameter(1) + 1; long long pmd = md; long long di; for (int i = lhi; i <= hhi; i++) { di = HoleDiameter(i); if (md > di) md = di; pipe[c++] = md; } if (lhi > 1) { Receive(id - 1); pmd = GetLL(id - 1); md = std::min(md, pmd); } if (hhi < ph) { PutLL(id + 1, md); Send(id + 1); } for (int i = lhi; i <= hhi; i++) { if (pipe[i-lhi] <= pmd) break; pipe[i-lhi] = pmd; } int i = 1; int r = 0; if (hhi < ph) { Receive(id + 1); i = GetInt(id + 1); r = GetInt(id + 1); } if (r == 0) { if (nd - i + 1 > hhi) { r = 1; } else { --c; while (c >= 0 && i <= nd) { di = DiscDiameter(i); while (c >= 0 && pipe[c] < di) --c; if (c >= 0) { ++i; --c; } } if (i > nd) { r = 2; i = c; } } } else i += hhi - lhi + 1; if (lhi > 1) { PutInt(id - 1, i); PutInt(id - 1, r); Send(id - 1); return EXIT_SUCCESS; } int depth = 0; if (r == 2) depth = i + 2; std::cout << depth << std::endl; return EXIT_SUCCESS; } |