#include <cstdlib> #include <iostream> //#include <cassert> #include "krazki.h" #include "message.h" #define MIN(a,b) (((a) > (b)) ? (b) : (a)) int nodes; int pipe_start; int pipe_end; int n_disc; int n_pipes; int n_pipes_per_node; int n_disc_per_node; int id; typedef struct { int pipe_nr; int disc_nr; } t_pipe; t_pipe *pipe; int lowest_disc_nr; int val; int top_disc; int val_from_lower_node; int disc_nr_from_lower_node; int move_disc(int st, int end) { int i, j, lastpos; long long pipe_diam; long long disc_diam; lastpos = n_pipes_per_node; lowest_disc_nr = 0; top_disc = 0; for(i = st; i <= end; ++i) { j = 0; disc_diam = DiscDiameter( i ); while(1) { pipe_diam = HoleDiameter( pipe[j].pipe_nr ); if (pipe[j].disc_nr != 0 || disc_diam > pipe_diam) { break; } j++; if (j == n_pipes_per_node) { break; } } if (j < n_pipes_per_node) { if (j > 0) { j--; pipe[j].disc_nr = i; lastpos = j; top_disc = i; if (lowest_disc_nr == 0) { lowest_disc_nr = i; } } else { if (j == 0) { return end - i + 1; } } } } return -lastpos; } int move_disc_last(int st, int end) { int i, j, lastpos; long long pipe_diam; long long disc_diam; lastpos = n_pipes_per_node; top_disc = 0; for(i = st; i <= end; ++i) { j = 0; disc_diam = DiscDiameter( i ); while(1) { pipe_diam = HoleDiameter( pipe[j].pipe_nr ); if (pipe[j].disc_nr != 0 || disc_diam > pipe_diam) { break; } j++; if (j == n_pipes_per_node) { break; } } if (j <= n_pipes_per_node) { if (j > 0) { j--; pipe[j].disc_nr = i; lastpos = j; top_disc = i; } else { //assert (j == 0); return end - i + 1; } } } return -lastpos; } int main() { int last_disc = 0; nodes = NumberOfNodes(); n_disc = NumberOfDiscs(); n_pipes = PipeHeight(); id = MyNodeId(); if (n_disc > n_pipes) { if (id != 0) { return 0; } std::cout << "0\n"; return 0; } pipe = NULL; while (1) { n_pipes_per_node = n_pipes / nodes; if (n_disc - last_disc > n_pipes) { if (id != 0) { return 0; } else { std::cout << "0\n"; return 0; } } if (n_pipes_per_node <= 2) { if (id != 0) { return 0; } pipe_start = 1; pipe_end = n_pipes; n_pipes_per_node = n_pipes; pipe = new t_pipe [ n_pipes ]; for(int i = 0; i < n_pipes_per_node; ++i) { pipe[i].pipe_nr = pipe_start+i; pipe[i].disc_nr = 0; } n_disc_per_node = n_disc - last_disc; val = move_disc_last(last_disc + 1, n_disc ); if (val >= 0) { std::cout << "0\n"; return 0; } else { std::cout << -val+1 << "\n"; return 0; } } pipe_start = id * n_pipes_per_node+1; pipe_end = (id+1) * n_pipes_per_node; if (id == nodes-1) { pipe_end = n_pipes; n_pipes_per_node = pipe_end - pipe_start + 1; } //std::cerr << "node:" << id << " pstart " << pipe_start << " pend " << pipe_end << "\n"; if (pipe == NULL) { pipe = new t_pipe [ n_pipes_per_node ]; } for(int i = 0; i < n_pipes_per_node; ++i) { pipe[i].pipe_nr = pipe_start+i; pipe[i].disc_nr = 0; } //for(int i = 0; i < n_pipes_per_node; ++i) { // std::cerr << "node: " << id << " pipe pos " << i << " nr " << pipe[i].pipe_nr << "\n"; //} n_disc_per_node = MIN(n_pipes / nodes, n_disc - last_disc); if (id < nodes-1) { val = move_disc(last_disc + 1, last_disc + n_disc_per_node ); } else { val = move_disc_last(last_disc + 1, last_disc + n_disc_per_node ); } last_disc += n_disc_per_node; //std::cerr << "node: " << id << " val " << val << "\n"; /*for(int i = 0; i < n_pipes_per_node; ++i) { std::cerr << "node: " << id << " pipe_nr: " << pipe[i].pipe_nr << " pipe_diam: " << HoleDiameter( pipe[i].pipe_nr ) << " disc_nr: " << pipe[i].disc_nr << " disc_diam " << (pipe[i].disc_nr == 0 ? 0 : DiscDiameter( pipe[i].disc_nr )) << " lowest disc " << lowest_disc_nr << " top disc " << top_disc << "\n"; }*/ if (id < nodes-1) { Receive(id+1); val_from_lower_node = GetInt(id+1); // std::cerr << "node: " << id // << " received val " << val_from_lower_node // << "\n"; if (val_from_lower_node > 0) { Receive(id+1); disc_nr_from_lower_node = GetInt(id+1); // std::cerr << "node: " << id // << " received disc_nr " << disc_nr_from_lower_node // << "\n"; } if (val <= 0) { if (val_from_lower_node <= 1) { if (val == -n_pipes_per_node) { val += val_from_lower_node; } } else { if (disc_nr_from_lower_node + val_from_lower_node - 1 >= lowest_disc_nr) { val_from_lower_node = lowest_disc_nr - disc_nr_from_lower_node; } if (val_from_lower_node > 1) { if (val < 0) { if (-val > n_pipes_per_node - (n_disc_per_node - disc_nr_from_lower_node + 1)) { val = -n_pipes_per_node + n_disc_per_node - disc_nr_from_lower_node + 1; } //if (1-val > val_from_lower_node) { // val = 1 - val_from_lower_node; //} } } } } } if (id > 0) { PutInt(id-1, val); Send(id-1); if (val > 0) { PutInt(id-1, top_disc+1); Send(id-1); } } //else { // std::cerr << "node: 0 val " << val << "\n"; //} if (last_disc >= n_disc) { if (id == 0) { if (val > 0) { std::cout << "0\n"; } else { std::cout << -val+1 << "\n"; } } return 0; } if (id == 0) { if (val >= 0) { std::cout << "0\n"; for(int i = 1; i < nodes; ++i) { PutInt(i, 0); Send(i); } return 0; } else { val = -val; for(int i = 1; i < nodes; ++i) { PutInt(i, val); Send(i); } } } else { Receive(0); val = GetInt(0); if (val == 0) { return 0; } } n_pipes = val; } 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | #include <cstdlib> #include <iostream> //#include <cassert> #include "krazki.h" #include "message.h" #define MIN(a,b) (((a) > (b)) ? (b) : (a)) int nodes; int pipe_start; int pipe_end; int n_disc; int n_pipes; int n_pipes_per_node; int n_disc_per_node; int id; typedef struct { int pipe_nr; int disc_nr; } t_pipe; t_pipe *pipe; int lowest_disc_nr; int val; int top_disc; int val_from_lower_node; int disc_nr_from_lower_node; int move_disc(int st, int end) { int i, j, lastpos; long long pipe_diam; long long disc_diam; lastpos = n_pipes_per_node; lowest_disc_nr = 0; top_disc = 0; for(i = st; i <= end; ++i) { j = 0; disc_diam = DiscDiameter( i ); while(1) { pipe_diam = HoleDiameter( pipe[j].pipe_nr ); if (pipe[j].disc_nr != 0 || disc_diam > pipe_diam) { break; } j++; if (j == n_pipes_per_node) { break; } } if (j < n_pipes_per_node) { if (j > 0) { j--; pipe[j].disc_nr = i; lastpos = j; top_disc = i; if (lowest_disc_nr == 0) { lowest_disc_nr = i; } } else { if (j == 0) { return end - i + 1; } } } } return -lastpos; } int move_disc_last(int st, int end) { int i, j, lastpos; long long pipe_diam; long long disc_diam; lastpos = n_pipes_per_node; top_disc = 0; for(i = st; i <= end; ++i) { j = 0; disc_diam = DiscDiameter( i ); while(1) { pipe_diam = HoleDiameter( pipe[j].pipe_nr ); if (pipe[j].disc_nr != 0 || disc_diam > pipe_diam) { break; } j++; if (j == n_pipes_per_node) { break; } } if (j <= n_pipes_per_node) { if (j > 0) { j--; pipe[j].disc_nr = i; lastpos = j; top_disc = i; } else { //assert (j == 0); return end - i + 1; } } } return -lastpos; } int main() { int last_disc = 0; nodes = NumberOfNodes(); n_disc = NumberOfDiscs(); n_pipes = PipeHeight(); id = MyNodeId(); if (n_disc > n_pipes) { if (id != 0) { return 0; } std::cout << "0\n"; return 0; } pipe = NULL; while (1) { n_pipes_per_node = n_pipes / nodes; if (n_disc - last_disc > n_pipes) { if (id != 0) { return 0; } else { std::cout << "0\n"; return 0; } } if (n_pipes_per_node <= 2) { if (id != 0) { return 0; } pipe_start = 1; pipe_end = n_pipes; n_pipes_per_node = n_pipes; pipe = new t_pipe [ n_pipes ]; for(int i = 0; i < n_pipes_per_node; ++i) { pipe[i].pipe_nr = pipe_start+i; pipe[i].disc_nr = 0; } n_disc_per_node = n_disc - last_disc; val = move_disc_last(last_disc + 1, n_disc ); if (val >= 0) { std::cout << "0\n"; return 0; } else { std::cout << -val+1 << "\n"; return 0; } } pipe_start = id * n_pipes_per_node+1; pipe_end = (id+1) * n_pipes_per_node; if (id == nodes-1) { pipe_end = n_pipes; n_pipes_per_node = pipe_end - pipe_start + 1; } //std::cerr << "node:" << id << " pstart " << pipe_start << " pend " << pipe_end << "\n"; if (pipe == NULL) { pipe = new t_pipe [ n_pipes_per_node ]; } for(int i = 0; i < n_pipes_per_node; ++i) { pipe[i].pipe_nr = pipe_start+i; pipe[i].disc_nr = 0; } //for(int i = 0; i < n_pipes_per_node; ++i) { // std::cerr << "node: " << id << " pipe pos " << i << " nr " << pipe[i].pipe_nr << "\n"; //} n_disc_per_node = MIN(n_pipes / nodes, n_disc - last_disc); if (id < nodes-1) { val = move_disc(last_disc + 1, last_disc + n_disc_per_node ); } else { val = move_disc_last(last_disc + 1, last_disc + n_disc_per_node ); } last_disc += n_disc_per_node; //std::cerr << "node: " << id << " val " << val << "\n"; /*for(int i = 0; i < n_pipes_per_node; ++i) { std::cerr << "node: " << id << " pipe_nr: " << pipe[i].pipe_nr << " pipe_diam: " << HoleDiameter( pipe[i].pipe_nr ) << " disc_nr: " << pipe[i].disc_nr << " disc_diam " << (pipe[i].disc_nr == 0 ? 0 : DiscDiameter( pipe[i].disc_nr )) << " lowest disc " << lowest_disc_nr << " top disc " << top_disc << "\n"; }*/ if (id < nodes-1) { Receive(id+1); val_from_lower_node = GetInt(id+1); // std::cerr << "node: " << id // << " received val " << val_from_lower_node // << "\n"; if (val_from_lower_node > 0) { Receive(id+1); disc_nr_from_lower_node = GetInt(id+1); // std::cerr << "node: " << id // << " received disc_nr " << disc_nr_from_lower_node // << "\n"; } if (val <= 0) { if (val_from_lower_node <= 1) { if (val == -n_pipes_per_node) { val += val_from_lower_node; } } else { if (disc_nr_from_lower_node + val_from_lower_node - 1 >= lowest_disc_nr) { val_from_lower_node = lowest_disc_nr - disc_nr_from_lower_node; } if (val_from_lower_node > 1) { if (val < 0) { if (-val > n_pipes_per_node - (n_disc_per_node - disc_nr_from_lower_node + 1)) { val = -n_pipes_per_node + n_disc_per_node - disc_nr_from_lower_node + 1; } //if (1-val > val_from_lower_node) { // val = 1 - val_from_lower_node; //} } } } } } if (id > 0) { PutInt(id-1, val); Send(id-1); if (val > 0) { PutInt(id-1, top_disc+1); Send(id-1); } } //else { // std::cerr << "node: 0 val " << val << "\n"; //} if (last_disc >= n_disc) { if (id == 0) { if (val > 0) { std::cout << "0\n"; } else { std::cout << -val+1 << "\n"; } } return 0; } if (id == 0) { if (val >= 0) { std::cout << "0\n"; for(int i = 1; i < nodes; ++i) { PutInt(i, 0); Send(i); } return 0; } else { val = -val; for(int i = 1; i < nodes; ++i) { PutInt(i, val); Send(i); } } } else { Receive(0); val = GetInt(0); if (val == 0) { return 0; } } n_pipes = val; } return 0; } |