#include <cstdlib> #include <iostream> #include <vector> #include "krazki.h" #include "message.h" using namespace std; int M, me, n, m; int pipes_segment, pipes_start, pipes_end, nodes_needed_for_pipes; int discs_segment, discs_start, discs_end, nodes_needed_for_discs; long long INF = 2 * (1e18); int INT_INF = 2 * (1e9); int MASTER = 0; vector<long long> pipes; vector<long long> pipes_minimums; vector<long long> R; vector<long long> K; vector<long long> my_discs; long long min(long long a, long long b){ return a < b ? a : b; } long long max(long long a, long long b){ return a > b ? a : b; } void init_input(){ M = NumberOfNodes(); me = MyNodeId(); n = PipeHeight(); m = NumberOfDiscs(); if (me == MASTER){ //printf("[%d]There are nodes %d\n", me, M); //printf("[%d]There are %d pipes\n", me, n); //printf("[%d]There are %d discs\n", me, m); //printf("[%d]My Id is %d\n\n\n", me, me); for (int i=0;i<n; i++){ //printf("hole[%d] = %lld\n", i, HoleDiameter(i+1)); } } } void init_pipes_fragments(){ pipes_segment = (n + (M-1))/M; pipes_start = me * pipes_segment; pipes_end = min((me + 1) * pipes_segment, n); nodes_needed_for_pipes = (n+pipes_segment-1)/pipes_segment; //printf("[%d]Start %d - End %d - SEGMENT %d - Nodes needed for pipes %d\n", me, pipes_start, pipes_end, pipes_segment, nodes_needed_for_pipes); } void init_discs_fragments(){ discs_segment = (m + (M-1))/M; discs_start = me * discs_segment; discs_end = min((me + 1) * discs_segment, m); nodes_needed_for_discs = (m+discs_segment-1)/discs_segment; //printf("[%d]Disc Start %d - Disc End %d - SEGMENT %d - Nodes needed for discs %d\n", me, discs_start, discs_end, discs_segment, nodes_needed_for_discs); } void wyznacz_i_propaguj_minima_otworow(){ if (me < nodes_needed_for_pipes){ for (int i=pipes_start;i<pipes_end; i++){ pipes.push_back(HoleDiameter(i+1)); } for (int i=pipes_start;i<pipes_end; i++){ //printf("[%d] pipe[%d] = %lld\n", me, i, pipes[i-pipes_start]); } pipes_minimums.push_back(pipes[0]); for (int i=1;i<pipes.size(); i++){ pipes_minimums.push_back(min(pipes_minimums[i-1], pipes[i])); } for (int i=pipes_start;i<pipes_end; i++){ //printf("[%d] minimum pipe[%d] = %lld\n", me, i, pipes_minimums[i-pipes_start]); } //printf("[%d] Wysylam moje minimum do wszystkich\n", me); for (int node=0; node<M; node++){ PutLL(node, pipes_minimums[pipes_minimums.size()-1]); Send(node); } } } void wczytaj_minima_otworow_od_innych(){ //printf("[%d] Odbieram minima od wszystkich\n", me); R.push_back(INF); for (int node=0; node<nodes_needed_for_pipes; node++){ Receive(node); R.push_back(GetLL(node)); } for (int i=1;i<R.size(); i++){ R[i] = min(R[i-1], R[i]); } R.push_back(-INF); for (int i=0;i<R.size(); i++){ //printf("[%d] R[%d] is %lld\n", me, i, R[i]); } } void normalizuj_krazki(){ if (me < nodes_needed_for_discs){ for (int i=discs_start;i<discs_end; i++){ my_discs.push_back(DiscDiameter(i+1)); } for (int i=discs_start;i<discs_end; i++){ //printf("[%d] disc[%d] is %lld\n", me, i, my_discs[i-discs_start]); } K.push_back(my_discs[0]); for (int i=1;i<my_discs.size(); i++){ K.push_back(max(K[i-1], my_discs[i])); } for (int i=discs_start;i<discs_end; i++){ //printf("[%d] K[%d] is %lld\n", me, i, K[i-discs_start]); } } } int find_fragment_idx(){ long long my_last_disc = my_discs[my_discs.size()-1]; for (int L=1; L<R.size(); L++){ if (R[L-1] >= my_last_disc && my_last_disc > R[L]){ return L; } } return 0; } void znajdz_pozycje_ostatniego_krazka_i_wyslij_moj_przedzial_do_mastera(){ if (me < nodes_needed_for_discs){ int fragment_idx = find_fragment_idx(); //printf("[%d] fragment_idx is %d\n", me, fragment_idx); long long left = -1, right = -1; long long last_disc_stops_at_idx = -INF; int liczba_krazkow = my_discs.size(); if (fragment_idx == R.size()-1){ last_disc_stops_at_idx = n-1; //the last pipe; right = last_disc_stops_at_idx; left = right - liczba_krazkow + 1; } else { //trzeba pobrac zadany fragment //printf("[%d] pobieram fragment dla idx %d\n", me, fragment_idx); int fragment_start = (fragment_idx - 1) * pipes_segment; //odejmujemy straznika z poczatku int fragment_end = min(fragment_idx * pipes_segment + discs_segment, n); //dodajemy (b-a) otworow //printf("[%d] fragment start %d, fragment_end %d \n", me, fragment_start, fragment_end); vector<long long> fragment; int len = fragment_end - fragment_start; for (int i=fragment_start; i<fragment_end; i++){ fragment.push_back(HoleDiameter(i+1)); //printf("fragment[%d] = %lld\n", i, fragment[i-fragment_start]); } fragment.push_back(-INF); //dodaje na koniec straznika //minuje moj fragment int r_idx = fragment_start / pipes_segment; long long r_min = R[r_idx]; //R[0] = INF fragment[0] = min(fragment[0], r_min); for (int i=1; i<fragment.size(); i++){ fragment[i] = min(fragment[i], fragment[i-1]); } //printf("po minowaniu otworow\n"); for (int i=fragment_start; i<fragment_end; i++){ //printf("fragment[%d] = %lld\n", i, fragment[i-fragment_start]); } //szukamy d dla ktorego fragment[d] >= krazek > fragment[d+1] long long last_disc = my_discs[my_discs.size()-1]; //printf("Last disc is %lld\n", last_disc); if (last_disc > fragment[0]){ last_disc_stops_at_idx = -1; } else { last_disc_stops_at_idx = 0; while (last_disc <= fragment[last_disc_stops_at_idx+1]){ last_disc_stops_at_idx++; } } //printf("Last Disc sTops at %d idx\n", fragment_start + last_disc_stops_at_idx); if (last_disc_stops_at_idx != -INF){ int D = fragment_start + last_disc_stops_at_idx; for (int md=my_discs.size()-2; md>=0; md--){ if (my_discs[md] <= fragment[D+1-fragment_start] ){ //printf("disc[%d] = %lld <= fragment[%d+1] = %lld\n", md, my_discs[md], D, fragment[D+1-fragment_start]); //doczepiamy sztucznie na koniec bez zmiany pozostalych //na koncu jest straznik D++; } else { //printf("disc[%d] = %lld > fragment[%d+1] = %lld\n", md, my_discs[md], D, fragment[D+1-fragment_start]); } } right = D; left = right - liczba_krazkow + 1; } } //printf("[%d] Wysylam moj znalezion przedzial (%d, %d) do MASTERA\n", me, left, right); PutLL(MASTER, left); PutLL(MASTER, right); Send(MASTER); } } void scal_przedzialy_i_wypisz_wynik(){ long long left = INF, right = INF; if (me == MASTER){ for (int node = 0; node < nodes_needed_for_discs; node++){ Receive(node); long L = GetLL(node); long R = GetLL(node); if (R >= left){ long LEN = R - L; R = left - 1; L = R - LEN; } left = L; right = R; } if (left >= 0){ std::cout << (left+1) << std::endl; } else { std::cout << 0 << std::endl; } } } int main(){ init_input(); init_pipes_fragments(); init_discs_fragments(); wyznacz_i_propaguj_minima_otworow(); wczytaj_minima_otworow_od_innych(); normalizuj_krazki(); znajdz_pozycje_ostatniego_krazka_i_wyslij_moj_przedzial_do_mastera(); scal_przedzialy_i_wypisz_wynik(); return EXIT_SUCCESS; } //6:22 // //#include "krazki.h" // //int pipe_height, number_of_discs; //int pipe_segment, pipe_start, pipe_end, nodes_needed_for_pipes; //int disc_segment, disc_start, disc_end, nodes_needed_for_discs; //int me, nodes; //int fragment_idx; //int last_disc_stops_at_idx; // //long long last_disc; // //vector<long long> my_pipes; //vector<long long> my_discs; //vector<long long> fragment; //vector<long long> R; // //void init_input(){ // me = getNodeId(); // nodes = getNodeNumber(); // pipe_height = PipeHeight(); // number_of_discs = NumberOfDiscs(); //} // //void init_pipes(){ // pipe_segment = (pipe_height + nodes - 1) / nodes; // pipe_start = me * pipe_segment; // pipe_end = min((me+1)*pipe_segment, pipe_height); // nodes_needed_for_pipes = (pipe_height + pipe_segment - 1) / pipe_segment; //} // //void init_discs(){ // disc_segment = (number_of_discs + nodes - 1) / nodes; // disc_start = me * disc_segment; // disc_end = min((me+1)*disc_segment, number_of_discs); // nodes_needed_for_discs = (number_of_discs + disc_segment - 1) / disc_segment; //} // //void wczytaj_moje_minima(){ // if (me < nodes_needed_for_pipes){ // for (int i=pipe_start; i<pipe_end; i++){ // my_pipes.push_back(HoleDiameter(i+1)); // } // } //} // //void minuj_moje_minima(){ // if (me < nodes_needed_for_pipes){ // int len = pipe_end - pipe_start; // for (int i=1; i<len; i++){ // my_pipes[i] = min(my_pipes[i-1], my_pipes[i]); // } // } //} // //void wyslij_moje_minima_do_wszystkich(){ // if (me < nodes_needed_for_pipes){ // for (int node = 0; node < nodes; node++){ // PutLL(my_pipes[my_pipes.size()-1]); // Send(node); // } // } //} // //void wczytaj_ciag_R(){ // for (int node = 0; node < nodes_needed_for_pipes; node++){ // Receive(node); // R.push_back(GetLL(node)); // } //} // //void minuj_ciag_R(){ // for (int i=1; i<R.size(); i++){ // R[i] = min(R[i-1], R[i]); // } //} // //void wczytaj_moje_krazki(){ // if (me < nodes_needed_for_discs){ // for (int i=disc_start; i<disc_end; i++){ // my_discs.push_back(DiscDiameter(i+1)); // } // } //} // //void maxuj_moje_krazki(){ // if (me < nodes_needed_for_discs){ // for (int i=my_discs.size()-2; i>=0; i--){ // my_discs[i] = max(my_discs[i], my_discs[i+1]); // } // } // last_disc = my_discs[my_discs.size()-1]; //} // //void znajdz_index_fragmentu(){ // // szukamy takiego i ze zachodzi R[i-1] >= last_disc > R[i]; // if (me < nodes_needed_for_discs){ // fragment_idx = 0; // while (fragment_idx < R.size() && R[fragment_idx] >= last_disc){ // fragment_idx++; // } // } //} // //void wczytaj_fragment(){ // int fragment_start = me * fragment_idx; // int fragment_end = min((me + 1) * fragment_idx + my_discs.size(), number_of_discs); // // for (int i=fragment_start; i<fragment_end; i++){ // fragment.push_back(HoleDiameter(i+1)); // } //} // //void minuj_fragment(){ // int fragment_start = me * fragment_idx; // // long long minimum; // // if (fragment_start/pipe_segment == 0){ // minimum = INF; // } else { // minimum = R[fragment_start/pipe_segment-1]; // } // // int len = fragment_end - fragment_start; // fragment[0] = min(fragment[0], minimum); // // for (int i=1; i<len; i++){ // fragment[i] = min(fragment[i-1], fragment[i]); // } //} // //void znajdz_offset(){ // //szukamy takiego i ze fragment[i] >= last_disc > fragment[i+1]; // if (fragment[0] < last_disc){ // last_dist_offset = -1; // } else { // last_disc_offset = 0; // while (fragment[last_disc_offset] >= last_disc){ // last_disc_offset++; // } // } //} // //void znajdz_idx_ostatniego_krazka(){ // if (me < nodes_needed_for_discs){ // if (fragment_idx == R.size()){ // last_disc_stops_at_idx = pipe_height - 1; // } else { // wczytaj_fragment(); // minuj_fragment(); // znajdz_offset(); // last_disc_stops_at_idx = fragment_start + last_disc_offset; // } // } //} // //void generuj_przedzial(){ // if (me < nodes_needed_for_discs){ // if (last_disc_stops_at_id == pipe_height-1){ // my_right = last_disc_stops_at_idx; // my_left = my_right - my_discs.size() + 1; // } else { // int D = last_disc_stops_at_idx - fragment_start; // for(int md = my_discs.size()-2; md>=0; md--){ // if (D < fragment.size()-1 && my_discs[md] <= fragment[D+1]){ // D++; // } // } // my_right = D+fragment_start; // my_left = my_right - my_discs.size() + 1; // } // } //} // //void wyslij_moj_przedzial_do_mastera(){ // if (me < nodes_needed_for_discs){ // PutLL(MASTER, my_left); // PutLL(MASTER, my_right); // Send(MASTER); // } //} // //void scal_przedzialy_i_wypisz_wynik(){ // if (me == MASTER){ // int ans_left = INF, ans_right = INF; // for (int node = nodes_needed_for_discs-1; node >= 0; node--){ // Receive(node); // int node_left = GetLL(node); // int node_right = GetLL(node); // // if (node_right < ans_left){ // ans_left = node_left; // ans_right = node_right; // } else { // int len = node_right - node_left; // ans_right = ans_left - 1; // ans_left = ans_right - len; // } // } // if (ans_left < 0){ // std::cout >> 0 >> endl; // } else { // std::cout >> (ans_left+1) >> endl; // } // } //} // //int main(){ // init_input(); // init_pipes(); // init_discs(); // wczytaj_moje_minima(); // minuj_moje_minima(); // wyslij_moje_minima_do_wszystkich(); // wczytaj_ciag_R(); // minuj_ciag_R(); // wczytaj_moje_krazki(); // minuj_moje_krazki(); // znajdz_index_fragmentu(); // znajdz_idx_ostatniego_krazka(); // generuj_przedzial(); // wyslij_moj_przedzial_do_mastera(); // scal_przedzialy_i_wypisz_wynik(); // 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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | #include <cstdlib> #include <iostream> #include <vector> #include "krazki.h" #include "message.h" using namespace std; int M, me, n, m; int pipes_segment, pipes_start, pipes_end, nodes_needed_for_pipes; int discs_segment, discs_start, discs_end, nodes_needed_for_discs; long long INF = 2 * (1e18); int INT_INF = 2 * (1e9); int MASTER = 0; vector<long long> pipes; vector<long long> pipes_minimums; vector<long long> R; vector<long long> K; vector<long long> my_discs; long long min(long long a, long long b){ return a < b ? a : b; } long long max(long long a, long long b){ return a > b ? a : b; } void init_input(){ M = NumberOfNodes(); me = MyNodeId(); n = PipeHeight(); m = NumberOfDiscs(); if (me == MASTER){ //printf("[%d]There are nodes %d\n", me, M); //printf("[%d]There are %d pipes\n", me, n); //printf("[%d]There are %d discs\n", me, m); //printf("[%d]My Id is %d\n\n\n", me, me); for (int i=0;i<n; i++){ //printf("hole[%d] = %lld\n", i, HoleDiameter(i+1)); } } } void init_pipes_fragments(){ pipes_segment = (n + (M-1))/M; pipes_start = me * pipes_segment; pipes_end = min((me + 1) * pipes_segment, n); nodes_needed_for_pipes = (n+pipes_segment-1)/pipes_segment; //printf("[%d]Start %d - End %d - SEGMENT %d - Nodes needed for pipes %d\n", me, pipes_start, pipes_end, pipes_segment, nodes_needed_for_pipes); } void init_discs_fragments(){ discs_segment = (m + (M-1))/M; discs_start = me * discs_segment; discs_end = min((me + 1) * discs_segment, m); nodes_needed_for_discs = (m+discs_segment-1)/discs_segment; //printf("[%d]Disc Start %d - Disc End %d - SEGMENT %d - Nodes needed for discs %d\n", me, discs_start, discs_end, discs_segment, nodes_needed_for_discs); } void wyznacz_i_propaguj_minima_otworow(){ if (me < nodes_needed_for_pipes){ for (int i=pipes_start;i<pipes_end; i++){ pipes.push_back(HoleDiameter(i+1)); } for (int i=pipes_start;i<pipes_end; i++){ //printf("[%d] pipe[%d] = %lld\n", me, i, pipes[i-pipes_start]); } pipes_minimums.push_back(pipes[0]); for (int i=1;i<pipes.size(); i++){ pipes_minimums.push_back(min(pipes_minimums[i-1], pipes[i])); } for (int i=pipes_start;i<pipes_end; i++){ //printf("[%d] minimum pipe[%d] = %lld\n", me, i, pipes_minimums[i-pipes_start]); } //printf("[%d] Wysylam moje minimum do wszystkich\n", me); for (int node=0; node<M; node++){ PutLL(node, pipes_minimums[pipes_minimums.size()-1]); Send(node); } } } void wczytaj_minima_otworow_od_innych(){ //printf("[%d] Odbieram minima od wszystkich\n", me); R.push_back(INF); for (int node=0; node<nodes_needed_for_pipes; node++){ Receive(node); R.push_back(GetLL(node)); } for (int i=1;i<R.size(); i++){ R[i] = min(R[i-1], R[i]); } R.push_back(-INF); for (int i=0;i<R.size(); i++){ //printf("[%d] R[%d] is %lld\n", me, i, R[i]); } } void normalizuj_krazki(){ if (me < nodes_needed_for_discs){ for (int i=discs_start;i<discs_end; i++){ my_discs.push_back(DiscDiameter(i+1)); } for (int i=discs_start;i<discs_end; i++){ //printf("[%d] disc[%d] is %lld\n", me, i, my_discs[i-discs_start]); } K.push_back(my_discs[0]); for (int i=1;i<my_discs.size(); i++){ K.push_back(max(K[i-1], my_discs[i])); } for (int i=discs_start;i<discs_end; i++){ //printf("[%d] K[%d] is %lld\n", me, i, K[i-discs_start]); } } } int find_fragment_idx(){ long long my_last_disc = my_discs[my_discs.size()-1]; for (int L=1; L<R.size(); L++){ if (R[L-1] >= my_last_disc && my_last_disc > R[L]){ return L; } } return 0; } void znajdz_pozycje_ostatniego_krazka_i_wyslij_moj_przedzial_do_mastera(){ if (me < nodes_needed_for_discs){ int fragment_idx = find_fragment_idx(); //printf("[%d] fragment_idx is %d\n", me, fragment_idx); long long left = -1, right = -1; long long last_disc_stops_at_idx = -INF; int liczba_krazkow = my_discs.size(); if (fragment_idx == R.size()-1){ last_disc_stops_at_idx = n-1; //the last pipe; right = last_disc_stops_at_idx; left = right - liczba_krazkow + 1; } else { //trzeba pobrac zadany fragment //printf("[%d] pobieram fragment dla idx %d\n", me, fragment_idx); int fragment_start = (fragment_idx - 1) * pipes_segment; //odejmujemy straznika z poczatku int fragment_end = min(fragment_idx * pipes_segment + discs_segment, n); //dodajemy (b-a) otworow //printf("[%d] fragment start %d, fragment_end %d \n", me, fragment_start, fragment_end); vector<long long> fragment; int len = fragment_end - fragment_start; for (int i=fragment_start; i<fragment_end; i++){ fragment.push_back(HoleDiameter(i+1)); //printf("fragment[%d] = %lld\n", i, fragment[i-fragment_start]); } fragment.push_back(-INF); //dodaje na koniec straznika //minuje moj fragment int r_idx = fragment_start / pipes_segment; long long r_min = R[r_idx]; //R[0] = INF fragment[0] = min(fragment[0], r_min); for (int i=1; i<fragment.size(); i++){ fragment[i] = min(fragment[i], fragment[i-1]); } //printf("po minowaniu otworow\n"); for (int i=fragment_start; i<fragment_end; i++){ //printf("fragment[%d] = %lld\n", i, fragment[i-fragment_start]); } //szukamy d dla ktorego fragment[d] >= krazek > fragment[d+1] long long last_disc = my_discs[my_discs.size()-1]; //printf("Last disc is %lld\n", last_disc); if (last_disc > fragment[0]){ last_disc_stops_at_idx = -1; } else { last_disc_stops_at_idx = 0; while (last_disc <= fragment[last_disc_stops_at_idx+1]){ last_disc_stops_at_idx++; } } //printf("Last Disc sTops at %d idx\n", fragment_start + last_disc_stops_at_idx); if (last_disc_stops_at_idx != -INF){ int D = fragment_start + last_disc_stops_at_idx; for (int md=my_discs.size()-2; md>=0; md--){ if (my_discs[md] <= fragment[D+1-fragment_start] ){ //printf("disc[%d] = %lld <= fragment[%d+1] = %lld\n", md, my_discs[md], D, fragment[D+1-fragment_start]); //doczepiamy sztucznie na koniec bez zmiany pozostalych //na koncu jest straznik D++; } else { //printf("disc[%d] = %lld > fragment[%d+1] = %lld\n", md, my_discs[md], D, fragment[D+1-fragment_start]); } } right = D; left = right - liczba_krazkow + 1; } } //printf("[%d] Wysylam moj znalezion przedzial (%d, %d) do MASTERA\n", me, left, right); PutLL(MASTER, left); PutLL(MASTER, right); Send(MASTER); } } void scal_przedzialy_i_wypisz_wynik(){ long long left = INF, right = INF; if (me == MASTER){ for (int node = 0; node < nodes_needed_for_discs; node++){ Receive(node); long L = GetLL(node); long R = GetLL(node); if (R >= left){ long LEN = R - L; R = left - 1; L = R - LEN; } left = L; right = R; } if (left >= 0){ std::cout << (left+1) << std::endl; } else { std::cout << 0 << std::endl; } } } int main(){ init_input(); init_pipes_fragments(); init_discs_fragments(); wyznacz_i_propaguj_minima_otworow(); wczytaj_minima_otworow_od_innych(); normalizuj_krazki(); znajdz_pozycje_ostatniego_krazka_i_wyslij_moj_przedzial_do_mastera(); scal_przedzialy_i_wypisz_wynik(); return EXIT_SUCCESS; } //6:22 // //#include "krazki.h" // //int pipe_height, number_of_discs; //int pipe_segment, pipe_start, pipe_end, nodes_needed_for_pipes; //int disc_segment, disc_start, disc_end, nodes_needed_for_discs; //int me, nodes; //int fragment_idx; //int last_disc_stops_at_idx; // //long long last_disc; // //vector<long long> my_pipes; //vector<long long> my_discs; //vector<long long> fragment; //vector<long long> R; // //void init_input(){ // me = getNodeId(); // nodes = getNodeNumber(); // pipe_height = PipeHeight(); // number_of_discs = NumberOfDiscs(); //} // //void init_pipes(){ // pipe_segment = (pipe_height + nodes - 1) / nodes; // pipe_start = me * pipe_segment; // pipe_end = min((me+1)*pipe_segment, pipe_height); // nodes_needed_for_pipes = (pipe_height + pipe_segment - 1) / pipe_segment; //} // //void init_discs(){ // disc_segment = (number_of_discs + nodes - 1) / nodes; // disc_start = me * disc_segment; // disc_end = min((me+1)*disc_segment, number_of_discs); // nodes_needed_for_discs = (number_of_discs + disc_segment - 1) / disc_segment; //} // //void wczytaj_moje_minima(){ // if (me < nodes_needed_for_pipes){ // for (int i=pipe_start; i<pipe_end; i++){ // my_pipes.push_back(HoleDiameter(i+1)); // } // } //} // //void minuj_moje_minima(){ // if (me < nodes_needed_for_pipes){ // int len = pipe_end - pipe_start; // for (int i=1; i<len; i++){ // my_pipes[i] = min(my_pipes[i-1], my_pipes[i]); // } // } //} // //void wyslij_moje_minima_do_wszystkich(){ // if (me < nodes_needed_for_pipes){ // for (int node = 0; node < nodes; node++){ // PutLL(my_pipes[my_pipes.size()-1]); // Send(node); // } // } //} // //void wczytaj_ciag_R(){ // for (int node = 0; node < nodes_needed_for_pipes; node++){ // Receive(node); // R.push_back(GetLL(node)); // } //} // //void minuj_ciag_R(){ // for (int i=1; i<R.size(); i++){ // R[i] = min(R[i-1], R[i]); // } //} // //void wczytaj_moje_krazki(){ // if (me < nodes_needed_for_discs){ // for (int i=disc_start; i<disc_end; i++){ // my_discs.push_back(DiscDiameter(i+1)); // } // } //} // //void maxuj_moje_krazki(){ // if (me < nodes_needed_for_discs){ // for (int i=my_discs.size()-2; i>=0; i--){ // my_discs[i] = max(my_discs[i], my_discs[i+1]); // } // } // last_disc = my_discs[my_discs.size()-1]; //} // //void znajdz_index_fragmentu(){ // // szukamy takiego i ze zachodzi R[i-1] >= last_disc > R[i]; // if (me < nodes_needed_for_discs){ // fragment_idx = 0; // while (fragment_idx < R.size() && R[fragment_idx] >= last_disc){ // fragment_idx++; // } // } //} // //void wczytaj_fragment(){ // int fragment_start = me * fragment_idx; // int fragment_end = min((me + 1) * fragment_idx + my_discs.size(), number_of_discs); // // for (int i=fragment_start; i<fragment_end; i++){ // fragment.push_back(HoleDiameter(i+1)); // } //} // //void minuj_fragment(){ // int fragment_start = me * fragment_idx; // // long long minimum; // // if (fragment_start/pipe_segment == 0){ // minimum = INF; // } else { // minimum = R[fragment_start/pipe_segment-1]; // } // // int len = fragment_end - fragment_start; // fragment[0] = min(fragment[0], minimum); // // for (int i=1; i<len; i++){ // fragment[i] = min(fragment[i-1], fragment[i]); // } //} // //void znajdz_offset(){ // //szukamy takiego i ze fragment[i] >= last_disc > fragment[i+1]; // if (fragment[0] < last_disc){ // last_dist_offset = -1; // } else { // last_disc_offset = 0; // while (fragment[last_disc_offset] >= last_disc){ // last_disc_offset++; // } // } //} // //void znajdz_idx_ostatniego_krazka(){ // if (me < nodes_needed_for_discs){ // if (fragment_idx == R.size()){ // last_disc_stops_at_idx = pipe_height - 1; // } else { // wczytaj_fragment(); // minuj_fragment(); // znajdz_offset(); // last_disc_stops_at_idx = fragment_start + last_disc_offset; // } // } //} // //void generuj_przedzial(){ // if (me < nodes_needed_for_discs){ // if (last_disc_stops_at_id == pipe_height-1){ // my_right = last_disc_stops_at_idx; // my_left = my_right - my_discs.size() + 1; // } else { // int D = last_disc_stops_at_idx - fragment_start; // for(int md = my_discs.size()-2; md>=0; md--){ // if (D < fragment.size()-1 && my_discs[md] <= fragment[D+1]){ // D++; // } // } // my_right = D+fragment_start; // my_left = my_right - my_discs.size() + 1; // } // } //} // //void wyslij_moj_przedzial_do_mastera(){ // if (me < nodes_needed_for_discs){ // PutLL(MASTER, my_left); // PutLL(MASTER, my_right); // Send(MASTER); // } //} // //void scal_przedzialy_i_wypisz_wynik(){ // if (me == MASTER){ // int ans_left = INF, ans_right = INF; // for (int node = nodes_needed_for_discs-1; node >= 0; node--){ // Receive(node); // int node_left = GetLL(node); // int node_right = GetLL(node); // // if (node_right < ans_left){ // ans_left = node_left; // ans_right = node_right; // } else { // int len = node_right - node_left; // ans_right = ans_left - 1; // ans_left = ans_right - len; // } // } // if (ans_left < 0){ // std::cout >> 0 >> endl; // } else { // std::cout >> (ans_left+1) >> endl; // } // } //} // //int main(){ // init_input(); // init_pipes(); // init_discs(); // wczytaj_moje_minima(); // minuj_moje_minima(); // wyslij_moje_minima_do_wszystkich(); // wczytaj_ciag_R(); // minuj_ciag_R(); // wczytaj_moje_krazki(); // minuj_moje_krazki(); // znajdz_index_fragmentu(); // znajdz_idx_ostatniego_krazka(); // generuj_przedzial(); // wyslij_moj_przedzial_do_mastera(); // scal_przedzialy_i_wypisz_wynik(); // return 0; //} // |