#include <cstdio> #include "message.h" #include "krazki.h" const long long INF = 0x7FFFFFFFFFFFFFFFuLL; int nodes, me, height, discs; long long HoleDiam(int i) { ++i; return (i == height) ? 0 : HoleDiameter(i); } long long DiscDiam(int i) { ++i; return DiscDiameter(i); } int main (void) { nodes = NumberOfNodes(); me = MyNodeId(); height = PipeHeight(); discs = NumberOfDiscs(); ++height; // HoleDiam returns 0 at bottom // Cannot handle too many nodes. // Every pipe slice must be non-empty. if (nodes > height) nodes = height; if (me >= nodes) return 0; // pipe cleaner int pipe_ofs[nodes + 1]; for (int i = 0; i <= nodes; ++i) { pipe_ofs[i] = height * i / nodes; } int p_lo = pipe_ofs[me]; int p_hi = pipe_ofs[me + 1]; int p_cnt = p_hi - p_lo; long long PD[p_cnt]; long long min_d = INF; for (int i = p_lo; i < p_hi; ++i) { long long d = HoleDiam(i); PD[i - p_lo] = d; if (d < min_d) min_d = d; } for (int i = 0; i < nodes; ++i) { PutLL(i, min_d); Send(i); } long long PminD[nodes]; for (int i = 0; i < nodes; ++i) { int j = Receive(i); PminD[j] = GetLL(j); } min_d = INF; for (int i = 0; i < nodes; ++i) { if (PminD[i] < min_d) min_d = PminD[i]; PminD[i] = min_d; } min_d = me ? PminD[me - 1] : INF; for (int i = 0; i < p_cnt; ++i) { if (PD[i] > min_d) PD[i] = min_d; min_d = PD[i]; } // disc cleaner int disc_ofs[nodes + 1]; for (int i = 0; i <= nodes; ++i) { disc_ofs[i] = discs * i / nodes; } int d_lo = disc_ofs[me]; int d_hi = disc_ofs[me + 1]; int d_cnt = d_hi - d_lo; long long DD[p_cnt]; long long max_d = 0; for (int i = d_lo; i < d_hi; ++i) { long long d = DiscDiam(i); DD[i - d_lo] = d; if (d > max_d) max_d = d; } for (int i = 0; i < nodes; ++i) { PutLL(i, max_d); Send(i); } long long DmaxD[nodes]; for (int i = 0; i < nodes; ++i) { int j = Receive(i); DmaxD[j] = GetLL(j); } max_d = 0; for (int i = 0; i < nodes; ++i) { if (DmaxD[i] > max_d) max_d = DmaxD[i]; DmaxD[i] = max_d; } max_d = me ? DmaxD[me - 1] : 0; for (int i = 0; i < d_cnt; ++i) { if (DD[i] < max_d) DD[i] = max_d; max_d = DD[i]; } // cleaning done // find... long long found; int place; for (int i = 0; i < nodes; ++i) { // binsearch in DD[0 .. d_cnt); long long find = PminD[i] + 1; int lo = 0; int hi = d_cnt; while (lo < hi) { int mid = (lo + hi) / 2; if (DD[mid] < find) lo = mid + 1; else hi = mid; }; int where = d_lo + lo; if (where >= d_hi) where = 0x7FFFFFFF; // Node i cares about this slice PutInt(i, where); Send(i); if (me != i) continue; int located = 0x7FFFFFFF; for (int j = 0; j < nodes; ++j) { Receive(j); int v = GetInt(j); if (v < located) located = v; } found = find; place = located; } if (me + 1 < nodes) { PutInt(me + 1, place); Send(me + 1); } int place_end; if (!me) { place_end = 0x7FFFFFFF; } else { Receive(me - 1); place_end = GetInt(me - 1); } if (place > discs) place = discs; if (place_end > discs) place_end = discs; int used = 0; int spare = 0; int p_where = p_hi - 1; while (place < place_end) { long long discdiam = DiscDiam(place); while ((p_where >= p_lo) && (discdiam > PD[p_where - p_lo])) { --p_where; ++spare; } if (p_where >= p_lo) { --p_where; ++used; ++place; } else break; } PutInt(0, used); PutInt(0, spare); PutInt(0, place_end - place); PutInt(0, p_where + 1 - p_lo); Send(0); if (me) return 0; int USED[nodes]; int SPARE[nodes]; int LEFT[nodes]; int DEPTH[nodes]; for (int i = 0; i < nodes; ++i) { Receive(i); USED[i] = GetInt(i); SPARE[i] = GetInt(i); LEFT[i] = GetInt(i); DEPTH[i] = GetInt(i); } int deep = 0; for (int i = 0; i < nodes; ++i) { int j = nodes - 1 - i; // spare cancels out -deep if (deep < 0) { deep += SPARE[j]; if (deep > 0) deep = 0; } // +deep useless if stuff was used if (USED[j]) if (deep > 0) deep = 0; deep += DEPTH[j]; // +deep useless if stuff was left if (LEFT[j]) if (deep > 0) deep = 0; deep -= LEFT[j]; } if (deep < 0) printf("0\n"); else printf("%d\n", deep + 1); }
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 <cstdio> #include "message.h" #include "krazki.h" const long long INF = 0x7FFFFFFFFFFFFFFFuLL; int nodes, me, height, discs; long long HoleDiam(int i) { ++i; return (i == height) ? 0 : HoleDiameter(i); } long long DiscDiam(int i) { ++i; return DiscDiameter(i); } int main (void) { nodes = NumberOfNodes(); me = MyNodeId(); height = PipeHeight(); discs = NumberOfDiscs(); ++height; // HoleDiam returns 0 at bottom // Cannot handle too many nodes. // Every pipe slice must be non-empty. if (nodes > height) nodes = height; if (me >= nodes) return 0; // pipe cleaner int pipe_ofs[nodes + 1]; for (int i = 0; i <= nodes; ++i) { pipe_ofs[i] = height * i / nodes; } int p_lo = pipe_ofs[me]; int p_hi = pipe_ofs[me + 1]; int p_cnt = p_hi - p_lo; long long PD[p_cnt]; long long min_d = INF; for (int i = p_lo; i < p_hi; ++i) { long long d = HoleDiam(i); PD[i - p_lo] = d; if (d < min_d) min_d = d; } for (int i = 0; i < nodes; ++i) { PutLL(i, min_d); Send(i); } long long PminD[nodes]; for (int i = 0; i < nodes; ++i) { int j = Receive(i); PminD[j] = GetLL(j); } min_d = INF; for (int i = 0; i < nodes; ++i) { if (PminD[i] < min_d) min_d = PminD[i]; PminD[i] = min_d; } min_d = me ? PminD[me - 1] : INF; for (int i = 0; i < p_cnt; ++i) { if (PD[i] > min_d) PD[i] = min_d; min_d = PD[i]; } // disc cleaner int disc_ofs[nodes + 1]; for (int i = 0; i <= nodes; ++i) { disc_ofs[i] = discs * i / nodes; } int d_lo = disc_ofs[me]; int d_hi = disc_ofs[me + 1]; int d_cnt = d_hi - d_lo; long long DD[p_cnt]; long long max_d = 0; for (int i = d_lo; i < d_hi; ++i) { long long d = DiscDiam(i); DD[i - d_lo] = d; if (d > max_d) max_d = d; } for (int i = 0; i < nodes; ++i) { PutLL(i, max_d); Send(i); } long long DmaxD[nodes]; for (int i = 0; i < nodes; ++i) { int j = Receive(i); DmaxD[j] = GetLL(j); } max_d = 0; for (int i = 0; i < nodes; ++i) { if (DmaxD[i] > max_d) max_d = DmaxD[i]; DmaxD[i] = max_d; } max_d = me ? DmaxD[me - 1] : 0; for (int i = 0; i < d_cnt; ++i) { if (DD[i] < max_d) DD[i] = max_d; max_d = DD[i]; } // cleaning done // find... long long found; int place; for (int i = 0; i < nodes; ++i) { // binsearch in DD[0 .. d_cnt); long long find = PminD[i] + 1; int lo = 0; int hi = d_cnt; while (lo < hi) { int mid = (lo + hi) / 2; if (DD[mid] < find) lo = mid + 1; else hi = mid; }; int where = d_lo + lo; if (where >= d_hi) where = 0x7FFFFFFF; // Node i cares about this slice PutInt(i, where); Send(i); if (me != i) continue; int located = 0x7FFFFFFF; for (int j = 0; j < nodes; ++j) { Receive(j); int v = GetInt(j); if (v < located) located = v; } found = find; place = located; } if (me + 1 < nodes) { PutInt(me + 1, place); Send(me + 1); } int place_end; if (!me) { place_end = 0x7FFFFFFF; } else { Receive(me - 1); place_end = GetInt(me - 1); } if (place > discs) place = discs; if (place_end > discs) place_end = discs; int used = 0; int spare = 0; int p_where = p_hi - 1; while (place < place_end) { long long discdiam = DiscDiam(place); while ((p_where >= p_lo) && (discdiam > PD[p_where - p_lo])) { --p_where; ++spare; } if (p_where >= p_lo) { --p_where; ++used; ++place; } else break; } PutInt(0, used); PutInt(0, spare); PutInt(0, place_end - place); PutInt(0, p_where + 1 - p_lo); Send(0); if (me) return 0; int USED[nodes]; int SPARE[nodes]; int LEFT[nodes]; int DEPTH[nodes]; for (int i = 0; i < nodes; ++i) { Receive(i); USED[i] = GetInt(i); SPARE[i] = GetInt(i); LEFT[i] = GetInt(i); DEPTH[i] = GetInt(i); } int deep = 0; for (int i = 0; i < nodes; ++i) { int j = nodes - 1 - i; // spare cancels out -deep if (deep < 0) { deep += SPARE[j]; if (deep > 0) deep = 0; } // +deep useless if stuff was used if (USED[j]) if (deep > 0) deep = 0; deep += DEPTH[j]; // +deep useless if stuff was left if (LEFT[j]) if (deep > 0) deep = 0; deep -= LEFT[j]; } if (deep < 0) printf("0\n"); else printf("%d\n", deep + 1); } |