#include <stdio.h> #include <stdlib.h> #include <assert.h> #define FOR(i,n) for((i)=0;(i)<(n);++(i)) #define FORI(i,n) for((i)=1;(i)<=(n);++(i)) typedef struct { int nr; int b; int e; int t; } t_process; typedef struct { int heap_size; int (*cmp_func)(t_process*, t_process*); t_process **tab; } t_pr_heap; typedef struct { // 0 - end of process // 1 - other int type; int time; int cpu_nr; int pr_nr; } t_event; typedef struct { int heap_size; t_event **tab; } t_event_heap; typedef struct { int nr; t_process *pr; int b; int e; int max_b; } t_cpu; typedef struct { int heap_size; int min_left; t_cpu **tab; } t_cpu_heap; typedef struct { int top; t_cpu **tab; } t_cpu_stack; #define HEAP_TABSIZE 127 #define HEAP_LEFT(i) (2*(i)) #define HEAP_RIGHT(i) (2*(i)+1) #define HEAP_PARENT(i) ((i)/2) //#define EVENT_HEAP_TABSIZE 134217728 #define EVENT_HEAP_TABSIZE 4000000 #define CPU_STACK_TABSIZE 105 t_pr_heap h1, h2; t_cpu_heap cpu_h; t_event_heap eh; t_cpu_stack free_cpu_stack; int last_update; int h1_cmp(t_process *p1, t_process *p2) { if (p1->b < p2->b) return -1; if (p2->b < p1->b) return 1; return (p1->e - p1->t <= p2->e - p2->t) ? -1 : 1; } int h2_cmp(t_process *p1, t_process *p2) { return (p1->e - p1->t <= p2->e - p2->t) ? -1 : 1; } void h_init(t_pr_heap *h, int (*cmp_func)(t_process*, t_process*) ) { h->heap_size = 0; h->cmp_func = cmp_func; h->tab = (t_process**) malloc(HEAP_TABSIZE * sizeof(t_process*)); } int h_empty(t_pr_heap *h) { return (h->heap_size == 0); } void h_heapify(t_pr_heap *h, int i) { int l, r; int sm; t_process *tmp; l = HEAP_LEFT(i); r = HEAP_RIGHT(i); if (l <= h->heap_size && h->cmp_func( h->tab[l], h->tab[i] ) < 0) { sm = l; } else { sm = i; } if (r <= h->heap_size && h->cmp_func( h->tab[r], h->tab[sm] ) < 0) { sm = r; } if (sm != i) { tmp = h->tab[i]; h->tab[i] = h->tab[sm]; h->tab[sm] = tmp; h_heapify(h, sm); } } void h_build(t_pr_heap *h, int size) { int i; h->heap_size = size; for(i = size/2; i > 0; --i) { h_heapify(h, i); } } t_process* h_extract(t_pr_heap *h) { t_process* result; if (h->heap_size < 1) { return NULL; } result = h->tab[1]; h->tab[1] = h->tab[h->heap_size]; h->heap_size--; h_heapify(h, 1); return result; } void h_insert(t_pr_heap *h, t_process *pr) { int i; h->heap_size++; i = h->heap_size; while(i > 1 && h->cmp_func( h->tab[HEAP_PARENT(i)], pr ) > 0 ) { h->tab[i] = h->tab[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } h->tab[i] = pr; } /* void h_print(t_pr_heap *h) { int i; FORI(i, h->heap_size) { printf("pos %d, nr %d, b %d e %d t %d maxb %d\n", i, h->tab[i]->nr, h->tab[i]->b, h->tab[i]->e, h->tab[i]->t , h->tab[i]->e - h->tab[i]->t + 1); } printf("\n"); } */ int eh_cmp(t_event *e1, t_event *e2) { if (e1->time < e2->time) return -1; if (e2->time < e1->time) return 1; return e1->type - e2->type; } void eh_init(t_event_heap *h) { h->heap_size = 0; h->tab = (t_event**) malloc(EVENT_HEAP_TABSIZE * sizeof(t_event*)); } int eh_empty(t_event_heap *h) { return (h->heap_size == 0); } void eh_heapify(t_event_heap *h, int i) { int l, r; int sm; t_event *tmp; l = HEAP_LEFT(i); r = HEAP_RIGHT(i); if (l <= h->heap_size && eh_cmp( h->tab[l], h->tab[i] ) < 0) { sm = l; } else { sm = i; } if (r <= h->heap_size && eh_cmp( h->tab[r], h->tab[sm] ) < 0) { sm = r; } if (sm != i) { tmp = h->tab[i]; h->tab[i] = h->tab[sm]; h->tab[sm] = tmp; eh_heapify(h, sm); } } void eh_build(t_event_heap *h, int size) { int i; h->heap_size = size; for(i = size/2; i > 0; --i) { eh_heapify(h, i); } } t_event* eh_extract(t_event_heap *h) { t_event *result; if (h->heap_size < 1) { assert(0); return NULL; } result = h->tab[1]; h->tab[1] = h->tab[h->heap_size]; h->heap_size--; eh_heapify(h, 1); return result; } void eh_insert(t_event_heap *h, t_event *e) { int i; h->heap_size++; i = h->heap_size; while(i > 1 && eh_cmp( h->tab[HEAP_PARENT(i)], e ) > 0 ) { h->tab[i] = h->tab[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } h->tab[i] = e; } /* void eh_print(t_event_heap *h) { int i; FORI(i, h->heap_size) { printf("pos %d, time %d, type %d\n", i, h->tab[i]->time, h->tab[i]->type); } printf("\n"); } */ void cpu_heap_init() { cpu_h.heap_size = 0; cpu_h.tab = (t_cpu**) malloc(HEAP_TABSIZE * sizeof(t_cpu*)); } /* int cmp_cpu(t_cpu *c1, t_cpu* c2) { if (NULL == c1->pr) { return -1; } if (NULL == c2->pr) { return 1; } if (c1->pr->e > c2->pr->e) return -1; if (c2->pr->e > c1->pr->e) return 1; return (c1->e <= c2->e) ? -1 : 1; } */ int cmp_cpu(t_cpu *c1, t_cpu* c2) { if (c1->max_b < c2->max_b) return 1; if (c2->max_b < c1->max_b) return -1; return (c1->pr->e <= c2->pr->e) ? 1 : -1; } void cpu_heapify(int i) { int l, r; int sm; t_cpu *tmp; l = HEAP_LEFT(i); r = HEAP_RIGHT(i); if (l <= cpu_h.heap_size && cmp_cpu(cpu_h.tab[l], cpu_h.tab[i]) < 0) { sm = l; } else { sm = i; } if (r <= cpu_h.heap_size && cmp_cpu(cpu_h.tab[r], cpu_h.tab[sm]) < 0) { sm = r; } if (sm != i) { tmp = cpu_h.tab[i]; cpu_h.tab[i] = cpu_h.tab[sm]; cpu_h.tab[sm] = tmp; cpu_heapify(sm); } } void cpu_heap_build(int size) { int i; cpu_h.heap_size = size; for(i = size/2; i > 0; --i) { cpu_heapify(i); } } t_cpu* cpu_heap_ext_max() { t_cpu* result; if (cpu_h.heap_size < 1) { return NULL; } result = cpu_h.tab[1]; cpu_h.tab[1] = cpu_h.tab[cpu_h.heap_size]; cpu_h.heap_size--; cpu_heapify(1); return result; } t_cpu* cpu_heap_ext_pos(int i) { t_cpu* result; assert(cpu_h.heap_size >= i); result = cpu_h.tab[i]; cpu_h.tab[i] = cpu_h.tab[cpu_h.heap_size]; cpu_h.heap_size--; cpu_heapify(i); return result; } t_cpu* cpu_heap_get_max() { return cpu_h.tab[1]; } void cpu_heap_insert(t_cpu *cpu) { int i; cpu_h.heap_size++; i = cpu_h.heap_size; while(i > 1 && cmp_cpu(cpu_h.tab[HEAP_PARENT(i)], cpu) > 0) { cpu_h.tab[i] = cpu_h.tab[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } cpu_h.tab[i] = cpu; } /* void cpu_heap_print() { int i; t_cpu *c; printf("cpu heap size %d\n", cpu_h.heap_size); FORI(i, cpu_h.heap_size) { c = cpu_h.tab[i]; printf("pos %d, nr %d, pr_nr %d, e %d, pr_e %d maxb %d\n", i, c->nr, c->pr == NULL ? 0 : c->pr->nr, c->e, c->pr == NULL ? 0 : c->pr->e, c->max_b); } printf("\n"); } */ void st_init(t_cpu_stack *st) { st->top = 0; st->tab = (t_cpu **) malloc(CPU_STACK_TABSIZE * sizeof(t_cpu*)); } int st_empty(t_cpu_stack *st) { return (st->top == 0 ? 1 : 0); } void st_push(t_cpu_stack *st, t_cpu *c) { st->top++; st->tab[st->top] = c; } t_cpu* st_pop(t_cpu_stack *st) { assert(st->top > 0); st->top--; return st->tab[st->top+1]; } //void cpu_init() //{ // cpu.pr = NULL; //} //int is_cpu_free(int time) //{ // return (cpu.pr == NULL || cpu.e <= time) ? 1 : 0; //} t_event* new_finish_event(int cpu_nr, int pr_nr, int time) { t_event *e; e = (t_event*) malloc (sizeof(t_event)); e->type = 0; e->time = time; e->cpu_nr = cpu_nr; e->pr_nr = pr_nr; return e; } t_event* new_check_event(int time) { t_event *e; e = (t_event*) malloc (sizeof(t_event)); e->type = 1; e->time = time; return e; } int find_cpu(int cpu_nr, int proc_nr, int time) { int i, n; t_cpu **tab = cpu_h.tab; n = cpu_h.heap_size; FORI(i, n) { if (tab[i]->nr == cpu_nr && tab[i]->pr->nr == proc_nr && tab[i]->e <= time) { return i; } } return 0; } void update_cpu(int time) { int i, n, val; if (time - last_update == 0) { return; } n = cpu_h.heap_size; val = time - last_update; FORI(i, n) { cpu_h.tab[i]->max_b += val; } last_update = time; } void cpu_start(t_cpu *cpu, t_process *pr, int time) { // if (cpu->pr != NULL) { // if (cpu->e <= time) { // printf("time %d, cpu %d, process %d ended, real ends %d\n", time, cpu->nr, cpu->pr->nr, cpu->e); // } else { // printf("time %d, cpu %d, stops executing process %d, left %d\n", time, cpu->nr, cpu->pr->nr, cpu->e - time); // } // // } // printf("time %d, cpu %d, start executing proc %d, left (%d), deadline (%d)\n", time, cpu->nr, pr->nr, pr->t, pr->e); cpu->pr = pr; cpu->b = time; cpu->e = time + pr->t; cpu->max_b = pr->e - pr->t + 1; if (cpu->e > pr->e+1) { printf("NIE\n"); exit (0); } } int main() { int i, n, m; int b, end, t; int last_event_time = -1; t_process *pr; t_cpu *cpu; t_pr_heap *ph1, *ph2; t_event_heap *peh; t_cpu_stack *pfree_cpu_stack; ph1 = &h1; ph2 = &h2; peh = &eh; pfree_cpu_stack = &free_cpu_stack; h_init( ph1, h1_cmp ); h_init( ph2, h2_cmp ); eh_init( peh ); cpu_heap_init(); st_init(pfree_cpu_stack); scanf("%d %d\n", &n, &m); FORI(i, n) { scanf("%d %d %d\n", &b, &end, &t); pr = (t_process*) malloc(sizeof(t_process)); pr->nr = i; pr->b = b; pr->e = end-1; pr->t = t; ph1->tab[i] = pr; } h_build(ph1, n); // h_print(ph1); FORI(i, m) { cpu = (t_cpu*) malloc(sizeof(t_cpu)); cpu->nr = i; cpu->pr = NULL; st_push(pfree_cpu_stack, cpu); } while (h_empty(ph1) == 0 || h_empty(ph2) == 0) { int t1, t2, mint, pos; t_event *e; //printf("*** next iter ***\n"); //cpu_heap_print(); if (!h_empty(ph1)) { t1 = ph1->tab[1]->b; } else { t1 = 999999999; } if (!eh_empty(peh)) { t2 = peh->tab[1]->time; } else { t2 = 999999999; } mint = ((t1 <= t2) ? t1 : t2); if (!h_empty(ph1)) { if ((ph1->tab[1]->e - ph1->tab[1]->t + 1) < mint) { printf("NIE\n"); return 0; } } //printf("t1 %d, t2 %d, mint %d\n", t1, t2, mint); if (mint == t2) { if (peh->tab[1]->type == 0) { //printf("event type 0\n"); e = eh_extract(peh); pos = find_cpu(e->cpu_nr, e->pr_nr, e->time); if (pos == 0) { continue; } cpu = cpu_heap_ext_pos(pos); // printf("time %d, cpu %d, proc %d finished\n", t2, cpu->nr, cpu->pr->nr); if (h_empty(ph2)) { st_push( pfree_cpu_stack, cpu ); } else { if (!h_empty(ph2)) { pr = h_extract(ph2); cpu_start( cpu, pr, t2); update_cpu(t2); cpu_heap_insert( cpu ); e = new_finish_event( cpu->nr, pr->nr, cpu->e); eh_insert(peh, e); } } continue; } } if (mint == t1) { pr = h_extract( ph1 ); // printf("pr %d\n", pr->nr); h_insert( ph2, pr ); // h_print(ph1); // h_print(ph2); if (!st_empty( pfree_cpu_stack )) { cpu = st_pop( pfree_cpu_stack ); pr = h_extract(ph2); cpu_start( cpu, pr, t1); update_cpu(t1); cpu_heap_insert( cpu ); e = new_finish_event( cpu->nr, pr->nr, cpu->e); //eh_print(peh); eh_insert(peh, e); //eh_print(peh); //cpu_heap_print(); } else { e = new_check_event(t1); eh_insert(peh, e); //printf("no free cpu\n"); //eh_print(peh); } continue; } e = eh_extract(peh); assert(e->type == 1); //printf("event type 1, time %d\n", e->time); // printf("last_event_time %d\n", last_event_time); if (last_event_time == e->time) { continue; } last_event_time = e->time; update_cpu( e->time ); //h_print(ph2); if (!h_empty(ph2)) { if (!st_empty( pfree_cpu_stack )) { t_event *e1; pr = h_extract(ph2); cpu = st_pop( pfree_cpu_stack ); cpu_start( cpu, pr, e->time); cpu_heap_insert( cpu ); e1 = new_finish_event( cpu->nr, pr->nr, cpu->e); eh_insert(peh, e1); } else { t_event *e1; assert( cpu_h.heap_size > 0); //printf("szukamy mozliwosci wywlasczenia\n"); //cpu_heap_print(); //h_print(ph2); //printf("maxb %d\n", cpu_h.tab[1]->max_b); //printf("maxb %d\n", ph2->tab[1]->e - ph2->tab[1]->t + 1); while ( (!h_empty(ph2)) && ( (cpu_h.tab[1]->max_b > ph2->tab[1]->e - ph2->tab[1]->t + 1) || ((cpu_h.tab[1]->max_b == ph2->tab[1]->e - ph2->tab[1]->t + 1) && (cpu_h.tab[1]->pr->e > ph2->tab[1]->e))) ) { cpu = cpu_heap_ext_max(); pr = h_extract(ph2); //printf("time %d, stop proc %d, told %d, exec-time %d, " // , e->time // , cpu->pr->nr // , cpu->pr->t // , e->time-cpu->b); cpu->pr->t -= e->time - cpu->b; //printf("time left %d\n", cpu->pr->t); h_insert(ph2, cpu->pr); cpu_start( cpu, pr, e->time); cpu_heap_insert( cpu ); e1 = new_finish_event( cpu->nr, pr->nr, cpu->e); eh_insert(peh, e1); } } } if (!h_empty(ph2)) { e->time++; eh_insert(peh, e); } } printf("TAK\n"); 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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define FOR(i,n) for((i)=0;(i)<(n);++(i)) #define FORI(i,n) for((i)=1;(i)<=(n);++(i)) typedef struct { int nr; int b; int e; int t; } t_process; typedef struct { int heap_size; int (*cmp_func)(t_process*, t_process*); t_process **tab; } t_pr_heap; typedef struct { // 0 - end of process // 1 - other int type; int time; int cpu_nr; int pr_nr; } t_event; typedef struct { int heap_size; t_event **tab; } t_event_heap; typedef struct { int nr; t_process *pr; int b; int e; int max_b; } t_cpu; typedef struct { int heap_size; int min_left; t_cpu **tab; } t_cpu_heap; typedef struct { int top; t_cpu **tab; } t_cpu_stack; #define HEAP_TABSIZE 127 #define HEAP_LEFT(i) (2*(i)) #define HEAP_RIGHT(i) (2*(i)+1) #define HEAP_PARENT(i) ((i)/2) //#define EVENT_HEAP_TABSIZE 134217728 #define EVENT_HEAP_TABSIZE 4000000 #define CPU_STACK_TABSIZE 105 t_pr_heap h1, h2; t_cpu_heap cpu_h; t_event_heap eh; t_cpu_stack free_cpu_stack; int last_update; int h1_cmp(t_process *p1, t_process *p2) { if (p1->b < p2->b) return -1; if (p2->b < p1->b) return 1; return (p1->e - p1->t <= p2->e - p2->t) ? -1 : 1; } int h2_cmp(t_process *p1, t_process *p2) { return (p1->e - p1->t <= p2->e - p2->t) ? -1 : 1; } void h_init(t_pr_heap *h, int (*cmp_func)(t_process*, t_process*) ) { h->heap_size = 0; h->cmp_func = cmp_func; h->tab = (t_process**) malloc(HEAP_TABSIZE * sizeof(t_process*)); } int h_empty(t_pr_heap *h) { return (h->heap_size == 0); } void h_heapify(t_pr_heap *h, int i) { int l, r; int sm; t_process *tmp; l = HEAP_LEFT(i); r = HEAP_RIGHT(i); if (l <= h->heap_size && h->cmp_func( h->tab[l], h->tab[i] ) < 0) { sm = l; } else { sm = i; } if (r <= h->heap_size && h->cmp_func( h->tab[r], h->tab[sm] ) < 0) { sm = r; } if (sm != i) { tmp = h->tab[i]; h->tab[i] = h->tab[sm]; h->tab[sm] = tmp; h_heapify(h, sm); } } void h_build(t_pr_heap *h, int size) { int i; h->heap_size = size; for(i = size/2; i > 0; --i) { h_heapify(h, i); } } t_process* h_extract(t_pr_heap *h) { t_process* result; if (h->heap_size < 1) { return NULL; } result = h->tab[1]; h->tab[1] = h->tab[h->heap_size]; h->heap_size--; h_heapify(h, 1); return result; } void h_insert(t_pr_heap *h, t_process *pr) { int i; h->heap_size++; i = h->heap_size; while(i > 1 && h->cmp_func( h->tab[HEAP_PARENT(i)], pr ) > 0 ) { h->tab[i] = h->tab[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } h->tab[i] = pr; } /* void h_print(t_pr_heap *h) { int i; FORI(i, h->heap_size) { printf("pos %d, nr %d, b %d e %d t %d maxb %d\n", i, h->tab[i]->nr, h->tab[i]->b, h->tab[i]->e, h->tab[i]->t , h->tab[i]->e - h->tab[i]->t + 1); } printf("\n"); } */ int eh_cmp(t_event *e1, t_event *e2) { if (e1->time < e2->time) return -1; if (e2->time < e1->time) return 1; return e1->type - e2->type; } void eh_init(t_event_heap *h) { h->heap_size = 0; h->tab = (t_event**) malloc(EVENT_HEAP_TABSIZE * sizeof(t_event*)); } int eh_empty(t_event_heap *h) { return (h->heap_size == 0); } void eh_heapify(t_event_heap *h, int i) { int l, r; int sm; t_event *tmp; l = HEAP_LEFT(i); r = HEAP_RIGHT(i); if (l <= h->heap_size && eh_cmp( h->tab[l], h->tab[i] ) < 0) { sm = l; } else { sm = i; } if (r <= h->heap_size && eh_cmp( h->tab[r], h->tab[sm] ) < 0) { sm = r; } if (sm != i) { tmp = h->tab[i]; h->tab[i] = h->tab[sm]; h->tab[sm] = tmp; eh_heapify(h, sm); } } void eh_build(t_event_heap *h, int size) { int i; h->heap_size = size; for(i = size/2; i > 0; --i) { eh_heapify(h, i); } } t_event* eh_extract(t_event_heap *h) { t_event *result; if (h->heap_size < 1) { assert(0); return NULL; } result = h->tab[1]; h->tab[1] = h->tab[h->heap_size]; h->heap_size--; eh_heapify(h, 1); return result; } void eh_insert(t_event_heap *h, t_event *e) { int i; h->heap_size++; i = h->heap_size; while(i > 1 && eh_cmp( h->tab[HEAP_PARENT(i)], e ) > 0 ) { h->tab[i] = h->tab[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } h->tab[i] = e; } /* void eh_print(t_event_heap *h) { int i; FORI(i, h->heap_size) { printf("pos %d, time %d, type %d\n", i, h->tab[i]->time, h->tab[i]->type); } printf("\n"); } */ void cpu_heap_init() { cpu_h.heap_size = 0; cpu_h.tab = (t_cpu**) malloc(HEAP_TABSIZE * sizeof(t_cpu*)); } /* int cmp_cpu(t_cpu *c1, t_cpu* c2) { if (NULL == c1->pr) { return -1; } if (NULL == c2->pr) { return 1; } if (c1->pr->e > c2->pr->e) return -1; if (c2->pr->e > c1->pr->e) return 1; return (c1->e <= c2->e) ? -1 : 1; } */ int cmp_cpu(t_cpu *c1, t_cpu* c2) { if (c1->max_b < c2->max_b) return 1; if (c2->max_b < c1->max_b) return -1; return (c1->pr->e <= c2->pr->e) ? 1 : -1; } void cpu_heapify(int i) { int l, r; int sm; t_cpu *tmp; l = HEAP_LEFT(i); r = HEAP_RIGHT(i); if (l <= cpu_h.heap_size && cmp_cpu(cpu_h.tab[l], cpu_h.tab[i]) < 0) { sm = l; } else { sm = i; } if (r <= cpu_h.heap_size && cmp_cpu(cpu_h.tab[r], cpu_h.tab[sm]) < 0) { sm = r; } if (sm != i) { tmp = cpu_h.tab[i]; cpu_h.tab[i] = cpu_h.tab[sm]; cpu_h.tab[sm] = tmp; cpu_heapify(sm); } } void cpu_heap_build(int size) { int i; cpu_h.heap_size = size; for(i = size/2; i > 0; --i) { cpu_heapify(i); } } t_cpu* cpu_heap_ext_max() { t_cpu* result; if (cpu_h.heap_size < 1) { return NULL; } result = cpu_h.tab[1]; cpu_h.tab[1] = cpu_h.tab[cpu_h.heap_size]; cpu_h.heap_size--; cpu_heapify(1); return result; } t_cpu* cpu_heap_ext_pos(int i) { t_cpu* result; assert(cpu_h.heap_size >= i); result = cpu_h.tab[i]; cpu_h.tab[i] = cpu_h.tab[cpu_h.heap_size]; cpu_h.heap_size--; cpu_heapify(i); return result; } t_cpu* cpu_heap_get_max() { return cpu_h.tab[1]; } void cpu_heap_insert(t_cpu *cpu) { int i; cpu_h.heap_size++; i = cpu_h.heap_size; while(i > 1 && cmp_cpu(cpu_h.tab[HEAP_PARENT(i)], cpu) > 0) { cpu_h.tab[i] = cpu_h.tab[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } cpu_h.tab[i] = cpu; } /* void cpu_heap_print() { int i; t_cpu *c; printf("cpu heap size %d\n", cpu_h.heap_size); FORI(i, cpu_h.heap_size) { c = cpu_h.tab[i]; printf("pos %d, nr %d, pr_nr %d, e %d, pr_e %d maxb %d\n", i, c->nr, c->pr == NULL ? 0 : c->pr->nr, c->e, c->pr == NULL ? 0 : c->pr->e, c->max_b); } printf("\n"); } */ void st_init(t_cpu_stack *st) { st->top = 0; st->tab = (t_cpu **) malloc(CPU_STACK_TABSIZE * sizeof(t_cpu*)); } int st_empty(t_cpu_stack *st) { return (st->top == 0 ? 1 : 0); } void st_push(t_cpu_stack *st, t_cpu *c) { st->top++; st->tab[st->top] = c; } t_cpu* st_pop(t_cpu_stack *st) { assert(st->top > 0); st->top--; return st->tab[st->top+1]; } //void cpu_init() //{ // cpu.pr = NULL; //} //int is_cpu_free(int time) //{ // return (cpu.pr == NULL || cpu.e <= time) ? 1 : 0; //} t_event* new_finish_event(int cpu_nr, int pr_nr, int time) { t_event *e; e = (t_event*) malloc (sizeof(t_event)); e->type = 0; e->time = time; e->cpu_nr = cpu_nr; e->pr_nr = pr_nr; return e; } t_event* new_check_event(int time) { t_event *e; e = (t_event*) malloc (sizeof(t_event)); e->type = 1; e->time = time; return e; } int find_cpu(int cpu_nr, int proc_nr, int time) { int i, n; t_cpu **tab = cpu_h.tab; n = cpu_h.heap_size; FORI(i, n) { if (tab[i]->nr == cpu_nr && tab[i]->pr->nr == proc_nr && tab[i]->e <= time) { return i; } } return 0; } void update_cpu(int time) { int i, n, val; if (time - last_update == 0) { return; } n = cpu_h.heap_size; val = time - last_update; FORI(i, n) { cpu_h.tab[i]->max_b += val; } last_update = time; } void cpu_start(t_cpu *cpu, t_process *pr, int time) { // if (cpu->pr != NULL) { // if (cpu->e <= time) { // printf("time %d, cpu %d, process %d ended, real ends %d\n", time, cpu->nr, cpu->pr->nr, cpu->e); // } else { // printf("time %d, cpu %d, stops executing process %d, left %d\n", time, cpu->nr, cpu->pr->nr, cpu->e - time); // } // // } // printf("time %d, cpu %d, start executing proc %d, left (%d), deadline (%d)\n", time, cpu->nr, pr->nr, pr->t, pr->e); cpu->pr = pr; cpu->b = time; cpu->e = time + pr->t; cpu->max_b = pr->e - pr->t + 1; if (cpu->e > pr->e+1) { printf("NIE\n"); exit (0); } } int main() { int i, n, m; int b, end, t; int last_event_time = -1; t_process *pr; t_cpu *cpu; t_pr_heap *ph1, *ph2; t_event_heap *peh; t_cpu_stack *pfree_cpu_stack; ph1 = &h1; ph2 = &h2; peh = &eh; pfree_cpu_stack = &free_cpu_stack; h_init( ph1, h1_cmp ); h_init( ph2, h2_cmp ); eh_init( peh ); cpu_heap_init(); st_init(pfree_cpu_stack); scanf("%d %d\n", &n, &m); FORI(i, n) { scanf("%d %d %d\n", &b, &end, &t); pr = (t_process*) malloc(sizeof(t_process)); pr->nr = i; pr->b = b; pr->e = end-1; pr->t = t; ph1->tab[i] = pr; } h_build(ph1, n); // h_print(ph1); FORI(i, m) { cpu = (t_cpu*) malloc(sizeof(t_cpu)); cpu->nr = i; cpu->pr = NULL; st_push(pfree_cpu_stack, cpu); } while (h_empty(ph1) == 0 || h_empty(ph2) == 0) { int t1, t2, mint, pos; t_event *e; //printf("*** next iter ***\n"); //cpu_heap_print(); if (!h_empty(ph1)) { t1 = ph1->tab[1]->b; } else { t1 = 999999999; } if (!eh_empty(peh)) { t2 = peh->tab[1]->time; } else { t2 = 999999999; } mint = ((t1 <= t2) ? t1 : t2); if (!h_empty(ph1)) { if ((ph1->tab[1]->e - ph1->tab[1]->t + 1) < mint) { printf("NIE\n"); return 0; } } //printf("t1 %d, t2 %d, mint %d\n", t1, t2, mint); if (mint == t2) { if (peh->tab[1]->type == 0) { //printf("event type 0\n"); e = eh_extract(peh); pos = find_cpu(e->cpu_nr, e->pr_nr, e->time); if (pos == 0) { continue; } cpu = cpu_heap_ext_pos(pos); // printf("time %d, cpu %d, proc %d finished\n", t2, cpu->nr, cpu->pr->nr); if (h_empty(ph2)) { st_push( pfree_cpu_stack, cpu ); } else { if (!h_empty(ph2)) { pr = h_extract(ph2); cpu_start( cpu, pr, t2); update_cpu(t2); cpu_heap_insert( cpu ); e = new_finish_event( cpu->nr, pr->nr, cpu->e); eh_insert(peh, e); } } continue; } } if (mint == t1) { pr = h_extract( ph1 ); // printf("pr %d\n", pr->nr); h_insert( ph2, pr ); // h_print(ph1); // h_print(ph2); if (!st_empty( pfree_cpu_stack )) { cpu = st_pop( pfree_cpu_stack ); pr = h_extract(ph2); cpu_start( cpu, pr, t1); update_cpu(t1); cpu_heap_insert( cpu ); e = new_finish_event( cpu->nr, pr->nr, cpu->e); //eh_print(peh); eh_insert(peh, e); //eh_print(peh); //cpu_heap_print(); } else { e = new_check_event(t1); eh_insert(peh, e); //printf("no free cpu\n"); //eh_print(peh); } continue; } e = eh_extract(peh); assert(e->type == 1); //printf("event type 1, time %d\n", e->time); // printf("last_event_time %d\n", last_event_time); if (last_event_time == e->time) { continue; } last_event_time = e->time; update_cpu( e->time ); //h_print(ph2); if (!h_empty(ph2)) { if (!st_empty( pfree_cpu_stack )) { t_event *e1; pr = h_extract(ph2); cpu = st_pop( pfree_cpu_stack ); cpu_start( cpu, pr, e->time); cpu_heap_insert( cpu ); e1 = new_finish_event( cpu->nr, pr->nr, cpu->e); eh_insert(peh, e1); } else { t_event *e1; assert( cpu_h.heap_size > 0); //printf("szukamy mozliwosci wywlasczenia\n"); //cpu_heap_print(); //h_print(ph2); //printf("maxb %d\n", cpu_h.tab[1]->max_b); //printf("maxb %d\n", ph2->tab[1]->e - ph2->tab[1]->t + 1); while ( (!h_empty(ph2)) && ( (cpu_h.tab[1]->max_b > ph2->tab[1]->e - ph2->tab[1]->t + 1) || ((cpu_h.tab[1]->max_b == ph2->tab[1]->e - ph2->tab[1]->t + 1) && (cpu_h.tab[1]->pr->e > ph2->tab[1]->e))) ) { cpu = cpu_heap_ext_max(); pr = h_extract(ph2); //printf("time %d, stop proc %d, told %d, exec-time %d, " // , e->time // , cpu->pr->nr // , cpu->pr->t // , e->time-cpu->b); cpu->pr->t -= e->time - cpu->b; //printf("time left %d\n", cpu->pr->t); h_insert(ph2, cpu->pr); cpu_start( cpu, pr, e->time); cpu_heap_insert( cpu ); e1 = new_finish_event( cpu->nr, pr->nr, cpu->e); eh_insert(peh, e1); } } } if (!h_empty(ph2)) { e->time++; eh_insert(peh, e); } } printf("TAK\n"); return 0; } |