Advertisement
And1

merge

Dec 27th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct arr {
  5.     int *a, min, k, cap;
  6. };
  7.  
  8. struct queue {
  9.     struct arr **heap;
  10.     int cap, count;
  11. };
  12.  
  13. struct queue initPriorityQueue (int);
  14. void insert(struct queue *, struct arr *);
  15. void swapQ(struct queue *, int, int);
  16. void deleteMin (struct queue *);
  17. void increaseKey (struct queue *);
  18. void heapify(struct queue *, int);
  19.  
  20. int main(int argc, char *argv[]) {
  21.     int k;
  22.     scanf("%d", &k);
  23.     int *arr = (int *)malloc(sizeof(int) * k);
  24.     struct queue q = initPriorityQueue(k);
  25.     for (int i = 0; i < k; i++) {
  26.         scanf("%d", &arr[i]);
  27.     }
  28.     for (int i = 0; i < k; i++) {
  29.         struct arr *elem = (struct arr *)malloc(sizeof(struct arr));
  30.         elem->a = (int *)malloc(sizeof(int) * arr[i]);
  31.         for (int j = 0; j < arr[i]; j++) {
  32.             scanf("%d", &elem->a[j]);
  33.         }
  34.         elem->min = 0;
  35.         elem->cap = arr[i];
  36.         elem->k = elem->a[elem->min];
  37.         insert (&q, elem);
  38.         free(elem->a);
  39.         free(elem);
  40.     }
  41.     while (q.count > 0) {
  42.         printf("%d ", q.heap[0]->k);
  43.         if (q.heap[0]->min == q.heap[0]->cap - 1){
  44.             deleteMin(&q);
  45.         } else {
  46.             increaseKey(&q);
  47.         }
  48.     }
  49.     free(q.heap);
  50.     free(arr);
  51.     return 0;
  52. }
  53.  
  54. struct queue initPriorityQueue(int n){
  55.     struct queue q;
  56.     q.heap = (struct arr **)malloc(sizeof(struct arr *) * n);
  57.     q.cap = n;
  58.     q.count = 0;
  59.     return q;
  60. }
  61.  
  62. void insert (struct queue *q, struct arr *elem){
  63.     int i = q->count;
  64.     q->count++;
  65.     q->heap[i] = elem;
  66.     for (;i > 0 && q->heap[(i - 1) / 2]->k > q->heap[i]->k; i = (i - 1) / 2) {
  67.         swapQ(q, i, (i - 1) / 2);
  68.     }
  69. }
  70.  
  71. void swapQ (struct queue *q, int i, int j){
  72.     struct arr *t = q->heap[i];
  73.     q->heap[i] = q->heap[j];
  74.     q->heap[j] = t;
  75. }
  76.  
  77. void increaseKey (struct queue *q){
  78.     int i = q->heap[0]->min;
  79.     q->heap[0]->min++;
  80.     q->heap[0]->k = q->heap[0]->a[i + 1];
  81.     heapify(q, 0);
  82. }
  83.  
  84. void heapify(struct queue *q, int i){
  85.     int l = 2 * i + 1;
  86.     int r = 2 * i + 2;
  87.     int largest;
  88.     if (l < q->count && q->heap[l]->k < q->heap[i]->k){
  89.         largest = l;
  90.     }else largest = i;
  91.     if (r < q->count && q->heap[r]->k < q->heap[largest]->k){
  92.         largest = r;
  93.     }
  94.     if (largest != i){
  95.         swapQ(q, i, largest);
  96.         heapify(q, largest);
  97.     }
  98. }
  99.  
  100. void deleteMin (struct queue *q){
  101.     q->count--;
  102.     if (q->count > 0){
  103.         q->heap[0] = q->heap[q->count];
  104.         heapify(q, 0);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement