// Michał Wiatrowski #include <iostream> #include <vector> #include <algorithm> #include "krazki.h" #include "message.h" using namespace std; int main() { int TOTAL_NODES = NumberOfNodes(); int id = MyNodeId(); int n = PipeHeight(); int m = NumberOfDiscs(); int segment_size = (n + TOTAL_NODES - 1) / TOTAL_NODES; int segment_b = segment_size * id; int segment_e = segment_size * (id + 1); int chunk_size = (m + TOTAL_NODES - 1) / TOTAL_NODES; int chunk_b = chunk_size * id; int chunk_e = chunk_size * (id + 1); vector<long long> pipe(segment_size, -1); vector<pair<int, long long>> reach; vector<pair<long long, long long>> segments(TOTAL_NODES); // Czytanie danych o swoim wycinku rurki { for (int i = 0; i < segment_size; ++i) { if (segment_b + i >= n) break; pipe[i] = HoleDiameter(n - (segment_b + i)); } } // Przetwarzanie wycinka rurki { long long minimum = pipe[segment_size - 1]; for (int i = segment_size - 1; i >= 0; --i) { minimum = std::min(minimum, pipe[i]); pipe[i] = minimum; } } // Propagacja minimów - wysyłanie { long long minimum = pipe[0]; for (int i = 0; i < id; ++i) { PutLL(i, minimum); Send(i); } } // Propagacja minimów - odbieranie { long long real_min = pipe[0]; for (int i = id + 1; i < TOTAL_NODES; ++i) { Receive(i); real_min = std::min(real_min, GetLL(i)); } for (int i = 0; i < segment_size; ++i) pipe[i] = std::min(pipe[i], real_min); } // Wysyłanie całkowitego zakresu { for (int i = 0; i < TOTAL_NODES; ++i) { PutLL(i, pipe.front()); PutLL(i, pipe.back()); Send(i); } } // Odbieranie zakresów poszczególnych jednostek { for (int i = 0; i < TOTAL_NODES; ++i) { Receive(i); segments[i].first = GetLL(i); segments[i].second = GetLL(i); } } // Zbieranie podpowiedzi vector<int> hints(TOTAL_NODES, m); { vector<int> dest(chunk_size, -1); auto search_id = [&](long long r) { int low = 0; int upp = TOTAL_NODES; if (segments[0].second >= r) return 0; while (upp - low > 1) { int mid = (low + upp) / 2; if (segments[mid].second < r) low = mid; else upp = mid; } return (upp < TOTAL_NODES) ? upp : (TOTAL_NODES - 1); }; int max = 0; for (int i = 0; i < chunk_size; ++i) { if (chunk_b + i >= m) break; int dest_id = search_id(DiscDiameter(m - (chunk_b + i))); if (dest_id < max) dest_id = max; else if (max < dest_id) max = dest_id; if (hints[dest_id] == m) hints[dest_id] = chunk_b + i; } } // Wysyłanie podpowiedzi { for (int i = 0; i < TOTAL_NODES; ++i) { PutLL(i, hints[i]); Send(i); } } // Odbieranie podpowiedzi int start_pos = m; { for (int i = 0; i < TOTAL_NODES; ++i) { Receive(i); int s = GetInt(i); if (s != -1) start_pos = std::min(start_pos, s); } } // Przeglądanie właściwych krążków int used_discs; int gaps; { int disc_i = start_pos; int missed = 0; for (int pipe_pos = chunk_b; pipe_pos < chunk_e; ++pipe_pos) { if (pipe_pos >= n) break; if (disc_i >= m) { missed += 1; continue; } long long r = DiscDiameter(m - disc_i); long long& d = pipe[pipe_pos - chunk_b]; if (r > d) missed += 1; else disc_i += 1; } used_discs = (disc_i - start_pos); gaps = missed; } // "Przepychanie" krążków { int start_prev = 0; int used_prev = 0; if (id != 0) { Receive(id - 1); start_prev = GetInt(id - 1); used_prev = GetInt(id - 1); } if (start_pos == m) { start_pos = start_prev + used_prev; used_discs = gaps; } else { int first_unused = start_prev + used_prev; int leftover = start_pos - first_unused; start_pos = first_unused; used_discs += std::min(leftover, gaps); } if (id != TOTAL_NODES - 1) { PutInt(id + 1, start_pos); PutInt(id + 1, used_discs); Send(id + 1); } } // Jeszcze jeden przebieg żeby znaleźć gdzie wylądował // ostatni krążek { if (start_pos + used_discs == m) { int disc_i = start_pos; for (int pipe_pos = chunk_b; pipe_pos < chunk_e; ++pipe_pos) { if (pipe_pos >= n) break; long long r = DiscDiameter(m - disc_i); long long& d = pipe[pipe_pos - chunk_b]; if (r <= d) { if (disc_i == m-1) { // Wysyłanie komunikatu PutInt(0, n - pipe_pos); Send(0); } disc_i += 1; } } } if (id == TOTAL_NODES - 1 && start_pos + used_discs < m) { PutInt(0, 0); Send(0); } } // Wypisywanie wyniku { if (id == 0) { int messanger = Receive(-1); cout << GetInt(messanger) << endl; } } }
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 | // Michał Wiatrowski #include <iostream> #include <vector> #include <algorithm> #include "krazki.h" #include "message.h" using namespace std; int main() { int TOTAL_NODES = NumberOfNodes(); int id = MyNodeId(); int n = PipeHeight(); int m = NumberOfDiscs(); int segment_size = (n + TOTAL_NODES - 1) / TOTAL_NODES; int segment_b = segment_size * id; int segment_e = segment_size * (id + 1); int chunk_size = (m + TOTAL_NODES - 1) / TOTAL_NODES; int chunk_b = chunk_size * id; int chunk_e = chunk_size * (id + 1); vector<long long> pipe(segment_size, -1); vector<pair<int, long long>> reach; vector<pair<long long, long long>> segments(TOTAL_NODES); // Czytanie danych o swoim wycinku rurki { for (int i = 0; i < segment_size; ++i) { if (segment_b + i >= n) break; pipe[i] = HoleDiameter(n - (segment_b + i)); } } // Przetwarzanie wycinka rurki { long long minimum = pipe[segment_size - 1]; for (int i = segment_size - 1; i >= 0; --i) { minimum = std::min(minimum, pipe[i]); pipe[i] = minimum; } } // Propagacja minimów - wysyłanie { long long minimum = pipe[0]; for (int i = 0; i < id; ++i) { PutLL(i, minimum); Send(i); } } // Propagacja minimów - odbieranie { long long real_min = pipe[0]; for (int i = id + 1; i < TOTAL_NODES; ++i) { Receive(i); real_min = std::min(real_min, GetLL(i)); } for (int i = 0; i < segment_size; ++i) pipe[i] = std::min(pipe[i], real_min); } // Wysyłanie całkowitego zakresu { for (int i = 0; i < TOTAL_NODES; ++i) { PutLL(i, pipe.front()); PutLL(i, pipe.back()); Send(i); } } // Odbieranie zakresów poszczególnych jednostek { for (int i = 0; i < TOTAL_NODES; ++i) { Receive(i); segments[i].first = GetLL(i); segments[i].second = GetLL(i); } } // Zbieranie podpowiedzi vector<int> hints(TOTAL_NODES, m); { vector<int> dest(chunk_size, -1); auto search_id = [&](long long r) { int low = 0; int upp = TOTAL_NODES; if (segments[0].second >= r) return 0; while (upp - low > 1) { int mid = (low + upp) / 2; if (segments[mid].second < r) low = mid; else upp = mid; } return (upp < TOTAL_NODES) ? upp : (TOTAL_NODES - 1); }; int max = 0; for (int i = 0; i < chunk_size; ++i) { if (chunk_b + i >= m) break; int dest_id = search_id(DiscDiameter(m - (chunk_b + i))); if (dest_id < max) dest_id = max; else if (max < dest_id) max = dest_id; if (hints[dest_id] == m) hints[dest_id] = chunk_b + i; } } // Wysyłanie podpowiedzi { for (int i = 0; i < TOTAL_NODES; ++i) { PutLL(i, hints[i]); Send(i); } } // Odbieranie podpowiedzi int start_pos = m; { for (int i = 0; i < TOTAL_NODES; ++i) { Receive(i); int s = GetInt(i); if (s != -1) start_pos = std::min(start_pos, s); } } // Przeglądanie właściwych krążków int used_discs; int gaps; { int disc_i = start_pos; int missed = 0; for (int pipe_pos = chunk_b; pipe_pos < chunk_e; ++pipe_pos) { if (pipe_pos >= n) break; if (disc_i >= m) { missed += 1; continue; } long long r = DiscDiameter(m - disc_i); long long& d = pipe[pipe_pos - chunk_b]; if (r > d) missed += 1; else disc_i += 1; } used_discs = (disc_i - start_pos); gaps = missed; } // "Przepychanie" krążków { int start_prev = 0; int used_prev = 0; if (id != 0) { Receive(id - 1); start_prev = GetInt(id - 1); used_prev = GetInt(id - 1); } if (start_pos == m) { start_pos = start_prev + used_prev; used_discs = gaps; } else { int first_unused = start_prev + used_prev; int leftover = start_pos - first_unused; start_pos = first_unused; used_discs += std::min(leftover, gaps); } if (id != TOTAL_NODES - 1) { PutInt(id + 1, start_pos); PutInt(id + 1, used_discs); Send(id + 1); } } // Jeszcze jeden przebieg żeby znaleźć gdzie wylądował // ostatni krążek { if (start_pos + used_discs == m) { int disc_i = start_pos; for (int pipe_pos = chunk_b; pipe_pos < chunk_e; ++pipe_pos) { if (pipe_pos >= n) break; long long r = DiscDiameter(m - disc_i); long long& d = pipe[pipe_pos - chunk_b]; if (r <= d) { if (disc_i == m-1) { // Wysyłanie komunikatu PutInt(0, n - pipe_pos); Send(0); } disc_i += 1; } } } if (id == TOTAL_NODES - 1 && start_pos + used_discs < m) { PutInt(0, 0); Send(0); } } // Wypisywanie wyniku { if (id == 0) { int messanger = Receive(-1); cout << GetInt(messanger) << endl; } } } |