#include "message.h" #include "krazki.h" #include <cstdio> #include <cstdlib> typedef long long int64; int main () { int node_id = MyNodeId(); int nodes = NumberOfNodes(); int pipe = PipeHeight(); int discs = NumberOfDiscs(); if ((discs > pipe) || (DiscDiameter(discs) > HoleDiameter(1))) { if (!node_id) { printf("0\n"); } return 0; } int pipe_nodes = nodes; if (pipe < pipe_nodes) { pipe_nodes = pipe; } int pipe_starts[pipe_nodes]; int pipe_ends[pipe_nodes]; int seg_length = pipe / nodes; int remaining = pipe - seg_length * nodes; int at = 1; for (int i = 0; i < pipe_nodes; ++i) { pipe_starts[i] = at; at += seg_length; if (remaining) { ++at; --remaining; } pipe_ends[i] = at - 1; } int64 min_diam; if (node_id < pipe_nodes - 1) { min_diam = HoleDiameter(pipe_starts[node_id]); for (int i = pipe_starts[node_id] + 1; i <= pipe_ends[node_id]; ++i) { int64 diam = HoleDiameter(i); if (diam < min_diam) { min_diam = diam; } } if (node_id) { PutLL(0, min_diam); Send(0); } } int pipe_bounds[pipe_nodes]; if (node_id == 0) { pipe_bounds[0] = HoleDiameter(1); pipe_bounds[1] = min_diam; int64 min_so_far = min_diam; for (int i = 1; i < pipe_nodes - 1; ++i) { Receive(i); int64 rcv_min = GetLL(i); if (rcv_min < min_so_far) { min_so_far = rcv_min; } pipe_bounds[i + 1] = min_so_far; } if (nodes > 1) { for (int i = 0; i < pipe_nodes; ++i) { PutLL(1, pipe_bounds[i]); } Send(1); } } else { Receive(node_id - 1); for (int i = 0; i < pipe_nodes; ++i) { pipe_bounds[i] = GetLL(node_id - 1); } if (node_id < nodes - 1) { for (int i = 0; i < pipe_nodes; ++i) { PutLL(node_id + 1, pipe_bounds[i]); } Send(node_id + 1); } } int disc_nodes = nodes; if (discs < disc_nodes) { disc_nodes = discs; } int disc_starts[disc_nodes]; int disc_ends[disc_nodes]; seg_length = discs / nodes; remaining = discs - seg_length * nodes; at = 1; for (int i = 0; i < disc_nodes; ++i) { disc_starts[i] = at; at += seg_length; if (remaining) { ++at; --remaining; } disc_ends[i] = at - 1; } int64 max_diam; int64* disc_diams; if (node_id < disc_nodes) { disc_diams = (int64*)malloc((disc_ends[node_id] - disc_starts[node_id] + 4) * sizeof(int64)); max_diam = DiscDiameter(disc_starts[node_id]); disc_diams[0] = max_diam; for (int i = disc_starts[node_id] + 1; i <= disc_ends[node_id]; ++i) { int64 diam = DiscDiameter(i); disc_diams[i - disc_starts[node_id]] = diam; if (diam > max_diam) { max_diam = diam; } } if (node_id && (node_id < disc_nodes - 1)) { PutLL(0, max_diam); Send(0); } } int64 diam_bound = 0; if (node_id == 0) { int64 bound = max_diam; for (int i = 1; i < disc_nodes - 1; ++i) { Receive(i); int64 rcv_max = GetLL(i); PutLL(i, bound); Send(i); if (rcv_max > bound) { bound = rcv_max; } } if (disc_nodes > 1) { PutLL(disc_nodes - 1, bound); Send(disc_nodes - 1); } } else if (node_id < disc_nodes) { Receive(0); diam_bound = GetLL(0); } if (node_id < disc_nodes) { for (int i = 0; i <= disc_ends[node_id] - disc_starts[node_id]; ++i) { if (disc_diams[i] > diam_bound) { diam_bound = disc_diams[i]; } else { disc_diams[i] = diam_bound; } } } int pipe_seg = 0; if (node_id < disc_nodes) { while (1) { if (pipe_seg == pipe_nodes - 1) { break; } if (HoleDiameter(pipe_starts[pipe_seg + 1]) < disc_diams[disc_ends[node_id] - disc_starts[node_id]]) { break; } if (pipe_bounds[pipe_seg + 1] < disc_diams[disc_ends[node_id] - disc_starts[node_id]]) { break; } ++pipe_seg; } int64 bound = pipe_bounds[pipe_seg]; int top_idx = pipe_starts[pipe_seg]; while (1) { int64 diam = HoleDiameter(top_idx); if (bound < diam) { diam = bound; } if (diam < disc_diams[disc_ends[node_id] - disc_starts[node_id]]) { --top_idx; break; } if (diam < bound) { bound = diam; } if (top_idx >= pipe - 1) { break; } ++top_idx; } int64* holes = (int64*)malloc((disc_ends[node_id] - disc_starts[node_id] + 4) * sizeof(int64)); int idx = top_idx; int length = 0; for (int i = 0; i < disc_ends[node_id] - disc_starts[node_id] + 2; ++i) { int64 diam = HoleDiameter(idx); if (bound < diam) { diam = bound; } holes[length] = diam; ++length; if (diam < bound) { bound = diam; } ++idx; if (idx > pipe) { break; } } int height = 0; int disc_idx = 0; while (1) { int64 pipe_diam = holes[0]; if (height < length) { pipe_diam = holes[length - height - 1]; } if (disc_diams[disc_idx] <= pipe_diam) { ++disc_idx; if (disc_idx == disc_ends[node_id] - disc_starts[node_id] + 1) { break; } } ++height; } int top_index = top_idx - height + length - 1; if (node_id) { PutLL(0, top_index); Send(0); } else { for (int i = 1; i < disc_nodes; ++i) { Receive(i); int64 new_top = GetLL(i); int64 min_top = top_index - (disc_ends[i] - disc_starts[i] + 1); if (new_top > min_top) { new_top = min_top; } top_index = new_top; } printf("%d\n", top_index <= 0 ? 0 : top_index); } } return 0; }
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 235 236 | #include "message.h" #include "krazki.h" #include <cstdio> #include <cstdlib> typedef long long int64; int main () { int node_id = MyNodeId(); int nodes = NumberOfNodes(); int pipe = PipeHeight(); int discs = NumberOfDiscs(); if ((discs > pipe) || (DiscDiameter(discs) > HoleDiameter(1))) { if (!node_id) { printf("0\n"); } return 0; } int pipe_nodes = nodes; if (pipe < pipe_nodes) { pipe_nodes = pipe; } int pipe_starts[pipe_nodes]; int pipe_ends[pipe_nodes]; int seg_length = pipe / nodes; int remaining = pipe - seg_length * nodes; int at = 1; for (int i = 0; i < pipe_nodes; ++i) { pipe_starts[i] = at; at += seg_length; if (remaining) { ++at; --remaining; } pipe_ends[i] = at - 1; } int64 min_diam; if (node_id < pipe_nodes - 1) { min_diam = HoleDiameter(pipe_starts[node_id]); for (int i = pipe_starts[node_id] + 1; i <= pipe_ends[node_id]; ++i) { int64 diam = HoleDiameter(i); if (diam < min_diam) { min_diam = diam; } } if (node_id) { PutLL(0, min_diam); Send(0); } } int pipe_bounds[pipe_nodes]; if (node_id == 0) { pipe_bounds[0] = HoleDiameter(1); pipe_bounds[1] = min_diam; int64 min_so_far = min_diam; for (int i = 1; i < pipe_nodes - 1; ++i) { Receive(i); int64 rcv_min = GetLL(i); if (rcv_min < min_so_far) { min_so_far = rcv_min; } pipe_bounds[i + 1] = min_so_far; } if (nodes > 1) { for (int i = 0; i < pipe_nodes; ++i) { PutLL(1, pipe_bounds[i]); } Send(1); } } else { Receive(node_id - 1); for (int i = 0; i < pipe_nodes; ++i) { pipe_bounds[i] = GetLL(node_id - 1); } if (node_id < nodes - 1) { for (int i = 0; i < pipe_nodes; ++i) { PutLL(node_id + 1, pipe_bounds[i]); } Send(node_id + 1); } } int disc_nodes = nodes; if (discs < disc_nodes) { disc_nodes = discs; } int disc_starts[disc_nodes]; int disc_ends[disc_nodes]; seg_length = discs / nodes; remaining = discs - seg_length * nodes; at = 1; for (int i = 0; i < disc_nodes; ++i) { disc_starts[i] = at; at += seg_length; if (remaining) { ++at; --remaining; } disc_ends[i] = at - 1; } int64 max_diam; int64* disc_diams; if (node_id < disc_nodes) { disc_diams = (int64*)malloc((disc_ends[node_id] - disc_starts[node_id] + 4) * sizeof(int64)); max_diam = DiscDiameter(disc_starts[node_id]); disc_diams[0] = max_diam; for (int i = disc_starts[node_id] + 1; i <= disc_ends[node_id]; ++i) { int64 diam = DiscDiameter(i); disc_diams[i - disc_starts[node_id]] = diam; if (diam > max_diam) { max_diam = diam; } } if (node_id && (node_id < disc_nodes - 1)) { PutLL(0, max_diam); Send(0); } } int64 diam_bound = 0; if (node_id == 0) { int64 bound = max_diam; for (int i = 1; i < disc_nodes - 1; ++i) { Receive(i); int64 rcv_max = GetLL(i); PutLL(i, bound); Send(i); if (rcv_max > bound) { bound = rcv_max; } } if (disc_nodes > 1) { PutLL(disc_nodes - 1, bound); Send(disc_nodes - 1); } } else if (node_id < disc_nodes) { Receive(0); diam_bound = GetLL(0); } if (node_id < disc_nodes) { for (int i = 0; i <= disc_ends[node_id] - disc_starts[node_id]; ++i) { if (disc_diams[i] > diam_bound) { diam_bound = disc_diams[i]; } else { disc_diams[i] = diam_bound; } } } int pipe_seg = 0; if (node_id < disc_nodes) { while (1) { if (pipe_seg == pipe_nodes - 1) { break; } if (HoleDiameter(pipe_starts[pipe_seg + 1]) < disc_diams[disc_ends[node_id] - disc_starts[node_id]]) { break; } if (pipe_bounds[pipe_seg + 1] < disc_diams[disc_ends[node_id] - disc_starts[node_id]]) { break; } ++pipe_seg; } int64 bound = pipe_bounds[pipe_seg]; int top_idx = pipe_starts[pipe_seg]; while (1) { int64 diam = HoleDiameter(top_idx); if (bound < diam) { diam = bound; } if (diam < disc_diams[disc_ends[node_id] - disc_starts[node_id]]) { --top_idx; break; } if (diam < bound) { bound = diam; } if (top_idx >= pipe - 1) { break; } ++top_idx; } int64* holes = (int64*)malloc((disc_ends[node_id] - disc_starts[node_id] + 4) * sizeof(int64)); int idx = top_idx; int length = 0; for (int i = 0; i < disc_ends[node_id] - disc_starts[node_id] + 2; ++i) { int64 diam = HoleDiameter(idx); if (bound < diam) { diam = bound; } holes[length] = diam; ++length; if (diam < bound) { bound = diam; } ++idx; if (idx > pipe) { break; } } int height = 0; int disc_idx = 0; while (1) { int64 pipe_diam = holes[0]; if (height < length) { pipe_diam = holes[length - height - 1]; } if (disc_diams[disc_idx] <= pipe_diam) { ++disc_idx; if (disc_idx == disc_ends[node_id] - disc_starts[node_id] + 1) { break; } } ++height; } int top_index = top_idx - height + length - 1; if (node_id) { PutLL(0, top_index); Send(0); } else { for (int i = 1; i < disc_nodes; ++i) { Receive(i); int64 new_top = GetLL(i); int64 min_top = top_index - (disc_ends[i] - disc_starts[i] + 1); if (new_top > min_top) { new_top = min_top; } top_index = new_top; } printf("%d\n", top_index <= 0 ? 0 : top_index); } } return 0; } |