#include <cstdlib> #include <cstdio> #include <vector> #include <algorithm> #include "krazki.h" #include "message.h" typedef int smallnum; typedef long long bignum; struct node_result { int first_ring_position; int last_ring_position; int airgap; }; typedef std::vector<smallnum> smallvec; typedef std::vector<bignum> bigvec; void run_master_node(int number_of_nodes) { int number_of_workers = number_of_nodes - 1; int pipe_height = PipeHeight(); std::vector<node_result> results(number_of_workers); for (int i = 0; i < number_of_workers; i++) { int instance_number = Receive(-1); results[instance_number-1].airgap = GetInt(instance_number); results[instance_number-1].first_ring_position = GetInt(instance_number); results[instance_number-1].last_ring_position = GetInt(instance_number); } int height_so_far = 0; for (int i = 0; i < number_of_workers; i++) { if (results[i].last_ring_position == 0) {continue;} if (results[i].last_ring_position > pipe_height) { height_so_far = pipe_height + 1; break; } if (results[i].first_ring_position >= height_so_far) { height_so_far = results[i].last_ring_position; } else { height_so_far = std::max(results[i].last_ring_position, height_so_far + results[i].last_ring_position - results[i].first_ring_position - results[i].airgap); } if (height_so_far > pipe_height) { height_so_far = pipe_height + 1; break; } } printf("%d\n", pipe_height - height_so_far + 1); } void run_worker_node(int node_id, int number_of_nodes) { // Just to avoid overflow bignum number_of_discs = NumberOfDiscs(); bignum pipe_height = PipeHeight(); bignum worker_id = node_id - 1; bignum number_of_workers = number_of_nodes - 1; // Choose start and end disc range for this guy int start_disc = int(worker_id * number_of_discs / number_of_workers); int end_disc = int((worker_id + 1) * number_of_discs / number_of_workers); if (end_disc == start_disc) { PutInt(0, 0); PutInt(0, 0); PutInt(0, 0); Send(0); return; } // Step 1. Load pipes bigvec diameters; smallvec pipe_starts; smallvec pipe_ends; diameters.reserve(pipe_height); pipe_starts.reserve(pipe_height); pipe_ends.reserve(pipe_height); bignum running_min = 0; for (smallnum i = 0; i < pipe_height; i++) { bignum diameter = HoleDiameter(i+1); if (diameters.empty()) { running_min = diameter; diameters.push_back(diameter); pipe_ends.push_back(pipe_height); pipe_starts.push_back(pipe_height-1); } else { if (diameter >= running_min) { pipe_starts[pipe_starts.size()-1]--; } else { running_min = diameter; diameters.push_back(diameter); pipe_ends.push_back(pipe_height-i); pipe_starts.push_back(pipe_height-i-1); } } } // Step 2. Load rings bignum last_ring_size = -1; smallnum last_ring_position = 0; smallnum first_ring_position = -1; smallnum airgap = 0; for (smallnum i = start_disc; i < end_disc; i++) { bignum disk_size = DiscDiameter(i+1); if (last_ring_size >= disk_size) { last_ring_position += 1; } else { bigvec::reverse_iterator it = std::lower_bound(diameters.rbegin(), diameters.rend(), disk_size); smallnum idx = diameters.rend() - it - 1; if (idx >= diameters.size()) { last_ring_position = pipe_height + 1; break; } smallnum possible_start = pipe_starts[idx]; if (last_ring_position >= possible_start) { last_ring_position += 1; } else { if (last_ring_position > 0) { airgap += (possible_start - last_ring_position); } last_ring_position = possible_start + 1; } if (first_ring_position == -1) { first_ring_position = last_ring_position - 1; } if (disk_size > last_ring_size) { last_ring_size = disk_size; } } } PutInt(0, airgap); PutInt(0, first_ring_position); PutInt(0, last_ring_position); Send(0); } int main() { int node_id = MyNodeId(); if (node_id == 0) { run_master_node(NumberOfNodes()); return EXIT_SUCCESS; } else { run_worker_node(node_id, NumberOfNodes()); 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 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 | #include <cstdlib> #include <cstdio> #include <vector> #include <algorithm> #include "krazki.h" #include "message.h" typedef int smallnum; typedef long long bignum; struct node_result { int first_ring_position; int last_ring_position; int airgap; }; typedef std::vector<smallnum> smallvec; typedef std::vector<bignum> bigvec; void run_master_node(int number_of_nodes) { int number_of_workers = number_of_nodes - 1; int pipe_height = PipeHeight(); std::vector<node_result> results(number_of_workers); for (int i = 0; i < number_of_workers; i++) { int instance_number = Receive(-1); results[instance_number-1].airgap = GetInt(instance_number); results[instance_number-1].first_ring_position = GetInt(instance_number); results[instance_number-1].last_ring_position = GetInt(instance_number); } int height_so_far = 0; for (int i = 0; i < number_of_workers; i++) { if (results[i].last_ring_position == 0) {continue;} if (results[i].last_ring_position > pipe_height) { height_so_far = pipe_height + 1; break; } if (results[i].first_ring_position >= height_so_far) { height_so_far = results[i].last_ring_position; } else { height_so_far = std::max(results[i].last_ring_position, height_so_far + results[i].last_ring_position - results[i].first_ring_position - results[i].airgap); } if (height_so_far > pipe_height) { height_so_far = pipe_height + 1; break; } } printf("%d\n", pipe_height - height_so_far + 1); } void run_worker_node(int node_id, int number_of_nodes) { // Just to avoid overflow bignum number_of_discs = NumberOfDiscs(); bignum pipe_height = PipeHeight(); bignum worker_id = node_id - 1; bignum number_of_workers = number_of_nodes - 1; // Choose start and end disc range for this guy int start_disc = int(worker_id * number_of_discs / number_of_workers); int end_disc = int((worker_id + 1) * number_of_discs / number_of_workers); if (end_disc == start_disc) { PutInt(0, 0); PutInt(0, 0); PutInt(0, 0); Send(0); return; } // Step 1. Load pipes bigvec diameters; smallvec pipe_starts; smallvec pipe_ends; diameters.reserve(pipe_height); pipe_starts.reserve(pipe_height); pipe_ends.reserve(pipe_height); bignum running_min = 0; for (smallnum i = 0; i < pipe_height; i++) { bignum diameter = HoleDiameter(i+1); if (diameters.empty()) { running_min = diameter; diameters.push_back(diameter); pipe_ends.push_back(pipe_height); pipe_starts.push_back(pipe_height-1); } else { if (diameter >= running_min) { pipe_starts[pipe_starts.size()-1]--; } else { running_min = diameter; diameters.push_back(diameter); pipe_ends.push_back(pipe_height-i); pipe_starts.push_back(pipe_height-i-1); } } } // Step 2. Load rings bignum last_ring_size = -1; smallnum last_ring_position = 0; smallnum first_ring_position = -1; smallnum airgap = 0; for (smallnum i = start_disc; i < end_disc; i++) { bignum disk_size = DiscDiameter(i+1); if (last_ring_size >= disk_size) { last_ring_position += 1; } else { bigvec::reverse_iterator it = std::lower_bound(diameters.rbegin(), diameters.rend(), disk_size); smallnum idx = diameters.rend() - it - 1; if (idx >= diameters.size()) { last_ring_position = pipe_height + 1; break; } smallnum possible_start = pipe_starts[idx]; if (last_ring_position >= possible_start) { last_ring_position += 1; } else { if (last_ring_position > 0) { airgap += (possible_start - last_ring_position); } last_ring_position = possible_start + 1; } if (first_ring_position == -1) { first_ring_position = last_ring_position - 1; } if (disk_size > last_ring_size) { last_ring_size = disk_size; } } } PutInt(0, airgap); PutInt(0, first_ring_position); PutInt(0, last_ring_position); Send(0); } int main() { int node_id = MyNodeId(); if (node_id == 0) { run_master_node(NumberOfNodes()); return EXIT_SUCCESS; } else { run_worker_node(node_id, NumberOfNodes()); return EXIT_SUCCESS; } } |