#include "krazki.h" #include "message.h" #include <iostream> int main(int argc, char** argv) { int number_of_nodes = NumberOfNodes(); int my_node = MyNodeId(); int pipe_height = PipeHeight(); int number_of_discs = NumberOfDiscs(); if (pipe_height < pipe_height) { if (my_node == 0) { std::cout << 0 << std::endl; } } else { int pipe_height_per_node = pipe_height/number_of_nodes; int nodes_with_additional_pipe = pipe_height - number_of_nodes*pipe_height_per_node; int number_of_active_nodes = pipe_height_per_node ? number_of_nodes : nodes_with_additional_pipe; long long *min_hole_diameters = new long long [number_of_active_nodes]; int start_index; int end_index; if (my_node < nodes_with_additional_pipe) { start_index = (pipe_height_per_node + 1) * my_node; end_index = start_index + pipe_height_per_node + 1; } else { start_index = pipe_height_per_node * my_node + nodes_with_additional_pipe; end_index = start_index + pipe_height_per_node; } if (start_index > pipe_height) { return 0; } if (end_index > pipe_height) { end_index = pipe_height; } int max_index = end_index-start_index; long long min_hole_diameter = HoleDiameter(start_index + 1); for (int index = 1; index < max_index; index++) { long long hole_diameter = HoleDiameter(start_index + index + 1); if (hole_diameter < min_hole_diameter) { min_hole_diameter = hole_diameter; } } if (my_node == 0) { if (number_of_active_nodes > 1) { int target = 1; PutLL(target, min_hole_diameter); Send(target); } } else { int source = my_node - 1; Receive(source); long long min_hole_diameter_previous_pipe = GetLL(source); if (min_hole_diameter_previous_pipe < min_hole_diameter) { min_hole_diameter = min_hole_diameter_previous_pipe; } if (my_node < number_of_active_nodes - 1) { int target = my_node + 1; PutLL(target, min_hole_diameter); Send(target); } } min_hole_diameters[my_node] = min_hole_diameter; for (int index = 1; index < number_of_active_nodes; index++) { if (index != my_node) { PutLL(index, min_hole_diameter); Send(index); } } for (int index = 0; index < number_of_active_nodes; index++) { if (index != my_node) { int source = Receive(-1); long long source_min_hole_diameter = GetLL(source); min_hole_diameters[source] = source_min_hole_diameter; } } if (0 != my_node) { PutLL(0, min_hole_diameter); Send(0); } int discs_per_node = number_of_discs/number_of_nodes; int nodes_with_additional_disc = number_of_discs - number_of_nodes*discs_per_node; int nodes_with_discs = discs_per_node ? number_of_nodes : nodes_with_additional_disc; int disc_start_index; int disc_end_index; if (my_node < nodes_with_additional_disc) { disc_start_index = (discs_per_node + 1) * my_node; disc_end_index = disc_start_index + discs_per_node + 1; } else { disc_start_index = discs_per_node * my_node + nodes_with_additional_disc; disc_end_index = disc_start_index + discs_per_node; } if (disc_start_index > number_of_discs) { delete [] min_hole_diameters; return 0; } if (disc_end_index > number_of_discs) { disc_end_index = number_of_discs; } long long *normalized_disc_diameter = new long long[disc_end_index - disc_start_index]; for (int index = disc_start_index + 1; index <= disc_end_index; index++) { int table_index = index - disc_start_index - 1; if (table_index == 0) { normalized_disc_diameter[table_index] = DiscDiameter(index); } else { if (normalized_disc_diameter[table_index - 1] > DiscDiameter(index)) { normalized_disc_diameter[table_index] = normalized_disc_diameter[table_index - 1]; } else { normalized_disc_diameter[table_index] = DiscDiameter(index); } } } long long last_diameter = normalized_disc_diameter[disc_end_index - disc_start_index - 1]; int pipe_index = 0; int last_pipe_index = pipe_height_per_node; if (nodes_with_additional_pipe > 0) { last_pipe_index++; } while (pipe_index < number_of_active_nodes && min_hole_diameters[pipe_index] >= last_diameter && HoleDiameter(last_pipe_index) >= last_diameter) { pipe_index++; last_pipe_index += pipe_height_per_node; if (pipe_index < nodes_with_additional_pipe) { last_pipe_index++; } } if (pipe_index == number_of_active_nodes) { pipe_index--; } int min_period_index = 0; int max_period_index = 0; if (pipe_index != 0 || last_diameter <= HoleDiameter(1)) { if (pipe_index < nodes_with_additional_pipe) { start_index = (pipe_height_per_node + 1) * pipe_index; end_index = start_index + pipe_height_per_node + 1; } else { start_index = pipe_height_per_node * pipe_index + nodes_with_additional_pipe; end_index = start_index + pipe_height_per_node; } if (start_index > pipe_height) { delete [] normalized_disc_diameter; delete [] min_hole_diameters; return 0; } if (end_index > pipe_height) { end_index = pipe_height; } int min_period_index = start_index; while (min_period_index < end_index && HoleDiameter(min_period_index + 1) >= last_diameter) { min_period_index++; } max_period_index = min_period_index; for (int index = disc_end_index - 2; index >= disc_start_index; index--) { if(max_period_index < pipe_height && HoleDiameter(max_period_index + 1) <= normalized_disc_diameter[index]) { max_period_index++; } else { min_period_index--; } } if (my_node != 0) { int source = 0; Receive(source); PutInt(source, min_period_index); PutInt(source, max_period_index); Send(source); } else { for (int index = 1; index < nodes_with_discs; index++) { Send(index); Receive(index); int target_min_period_index = GetInt(index); int target_max_period_index = GetInt(index); if (min_period_index > target_max_period_index) { min_period_index = target_min_period_index; } else { min_period_index -= target_max_period_index - target_min_period_index + 1; } } if (min_period_index <= 0) { std::cout << 0 <<std::endl; } else { std::cout << min_period_index <<std::endl; } } } delete [] normalized_disc_diameter; delete [] min_hole_diameters; } 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 | #include "krazki.h" #include "message.h" #include <iostream> int main(int argc, char** argv) { int number_of_nodes = NumberOfNodes(); int my_node = MyNodeId(); int pipe_height = PipeHeight(); int number_of_discs = NumberOfDiscs(); if (pipe_height < pipe_height) { if (my_node == 0) { std::cout << 0 << std::endl; } } else { int pipe_height_per_node = pipe_height/number_of_nodes; int nodes_with_additional_pipe = pipe_height - number_of_nodes*pipe_height_per_node; int number_of_active_nodes = pipe_height_per_node ? number_of_nodes : nodes_with_additional_pipe; long long *min_hole_diameters = new long long [number_of_active_nodes]; int start_index; int end_index; if (my_node < nodes_with_additional_pipe) { start_index = (pipe_height_per_node + 1) * my_node; end_index = start_index + pipe_height_per_node + 1; } else { start_index = pipe_height_per_node * my_node + nodes_with_additional_pipe; end_index = start_index + pipe_height_per_node; } if (start_index > pipe_height) { return 0; } if (end_index > pipe_height) { end_index = pipe_height; } int max_index = end_index-start_index; long long min_hole_diameter = HoleDiameter(start_index + 1); for (int index = 1; index < max_index; index++) { long long hole_diameter = HoleDiameter(start_index + index + 1); if (hole_diameter < min_hole_diameter) { min_hole_diameter = hole_diameter; } } if (my_node == 0) { if (number_of_active_nodes > 1) { int target = 1; PutLL(target, min_hole_diameter); Send(target); } } else { int source = my_node - 1; Receive(source); long long min_hole_diameter_previous_pipe = GetLL(source); if (min_hole_diameter_previous_pipe < min_hole_diameter) { min_hole_diameter = min_hole_diameter_previous_pipe; } if (my_node < number_of_active_nodes - 1) { int target = my_node + 1; PutLL(target, min_hole_diameter); Send(target); } } min_hole_diameters[my_node] = min_hole_diameter; for (int index = 1; index < number_of_active_nodes; index++) { if (index != my_node) { PutLL(index, min_hole_diameter); Send(index); } } for (int index = 0; index < number_of_active_nodes; index++) { if (index != my_node) { int source = Receive(-1); long long source_min_hole_diameter = GetLL(source); min_hole_diameters[source] = source_min_hole_diameter; } } if (0 != my_node) { PutLL(0, min_hole_diameter); Send(0); } int discs_per_node = number_of_discs/number_of_nodes; int nodes_with_additional_disc = number_of_discs - number_of_nodes*discs_per_node; int nodes_with_discs = discs_per_node ? number_of_nodes : nodes_with_additional_disc; int disc_start_index; int disc_end_index; if (my_node < nodes_with_additional_disc) { disc_start_index = (discs_per_node + 1) * my_node; disc_end_index = disc_start_index + discs_per_node + 1; } else { disc_start_index = discs_per_node * my_node + nodes_with_additional_disc; disc_end_index = disc_start_index + discs_per_node; } if (disc_start_index > number_of_discs) { delete [] min_hole_diameters; return 0; } if (disc_end_index > number_of_discs) { disc_end_index = number_of_discs; } long long *normalized_disc_diameter = new long long[disc_end_index - disc_start_index]; for (int index = disc_start_index + 1; index <= disc_end_index; index++) { int table_index = index - disc_start_index - 1; if (table_index == 0) { normalized_disc_diameter[table_index] = DiscDiameter(index); } else { if (normalized_disc_diameter[table_index - 1] > DiscDiameter(index)) { normalized_disc_diameter[table_index] = normalized_disc_diameter[table_index - 1]; } else { normalized_disc_diameter[table_index] = DiscDiameter(index); } } } long long last_diameter = normalized_disc_diameter[disc_end_index - disc_start_index - 1]; int pipe_index = 0; int last_pipe_index = pipe_height_per_node; if (nodes_with_additional_pipe > 0) { last_pipe_index++; } while (pipe_index < number_of_active_nodes && min_hole_diameters[pipe_index] >= last_diameter && HoleDiameter(last_pipe_index) >= last_diameter) { pipe_index++; last_pipe_index += pipe_height_per_node; if (pipe_index < nodes_with_additional_pipe) { last_pipe_index++; } } if (pipe_index == number_of_active_nodes) { pipe_index--; } int min_period_index = 0; int max_period_index = 0; if (pipe_index != 0 || last_diameter <= HoleDiameter(1)) { if (pipe_index < nodes_with_additional_pipe) { start_index = (pipe_height_per_node + 1) * pipe_index; end_index = start_index + pipe_height_per_node + 1; } else { start_index = pipe_height_per_node * pipe_index + nodes_with_additional_pipe; end_index = start_index + pipe_height_per_node; } if (start_index > pipe_height) { delete [] normalized_disc_diameter; delete [] min_hole_diameters; return 0; } if (end_index > pipe_height) { end_index = pipe_height; } int min_period_index = start_index; while (min_period_index < end_index && HoleDiameter(min_period_index + 1) >= last_diameter) { min_period_index++; } max_period_index = min_period_index; for (int index = disc_end_index - 2; index >= disc_start_index; index--) { if(max_period_index < pipe_height && HoleDiameter(max_period_index + 1) <= normalized_disc_diameter[index]) { max_period_index++; } else { min_period_index--; } } if (my_node != 0) { int source = 0; Receive(source); PutInt(source, min_period_index); PutInt(source, max_period_index); Send(source); } else { for (int index = 1; index < nodes_with_discs; index++) { Send(index); Receive(index); int target_min_period_index = GetInt(index); int target_max_period_index = GetInt(index); if (min_period_index > target_max_period_index) { min_period_index = target_min_period_index; } else { min_period_index -= target_max_period_index - target_min_period_index + 1; } } if (min_period_index <= 0) { std::cout << 0 <<std::endl; } else { std::cout << min_period_index <<std::endl; } } } delete [] normalized_disc_diameter; delete [] min_hole_diameters; } return 0; } |