Advertisement
Void-voiD

Untitled

Dec 17th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Help {
  5. int v, k;
  6. };
  7.  
  8. struct Queue {
  9. struct Help *heap;
  10. int cap, count;
  11. };
  12.  
  13. struct Queue swap(struct Queue base, int a, int b)
  14. {
  15. int v = base.heap[a].v, k = base.heap[a].k;
  16. base.heap[a] = base.heap[b];
  17. base.heap[b].v = v;
  18. base.heap[b].k = k;
  19. return base;
  20. }
  21.  
  22. void heapify(int i, int n, struct Queue base)
  23. {
  24. for (;;) {
  25. int l = 2 * i + 1;
  26. int r = l + 1;
  27. int j = i;
  28. if (l < n && base.heap[i].v < base.heap[j].v) i = l;
  29. if (r < n && base.heap[i].v < base.heap[r].v) i = r;
  30. if (i == j) break;
  31. swap(base, i, j);
  32. }
  33. }
  34.  
  35. struct Queue InitQueue(int n)
  36. {
  37. struct Queue q;
  38. struct Help *d = (struct Help*)malloc(n * sizeof(struct Help));
  39. q.heap = d;
  40. q.cap = n;
  41. q.count = 0;
  42. return q;
  43. }
  44.  
  45. int QueueEmpty(struct Queue q)
  46. {
  47. return (q.count == 0);
  48. }
  49.  
  50. struct Queue Insert(struct Queue q, struct Help x)
  51. {
  52. int i = q.count;
  53. if (i == q.cap) printf("FULL");
  54. q.count = i + 1;
  55. q.heap[i] = x;
  56. while (i > 0 && q.heap[(i - 1) / 2].v > q.heap[i].v) {
  57. q = swap(q, (i - 1) / 2, i);
  58. i = (i - 1) / 2;
  59. }
  60. return q;
  61. }
  62.  
  63. struct Help ExtractMin(struct Queue q)
  64. {
  65. if (q.count == 0) printf("NULL");
  66. return q.heap[0];
  67. }
  68.  
  69. int main()
  70. {
  71. int j, i, k, n = 0, cur = 0, now;
  72. struct Help x;
  73. scanf("%d\n", &k);
  74. int count[k];
  75. for (i = 0; i < k; i++) {
  76. scanf("%d", &count[i]);
  77. n += count[i];
  78. }
  79. scanf("\n");
  80. int all[n];
  81. for (i = 0; i < k; i++) {
  82. for (j = 0; j < count[i]; j++) scanf("%d", &all[cur + j]);
  83. cur += count[i];
  84. if (i != k - 1) scanf("\n");
  85. }
  86. struct Queue q = InitQueue(k);
  87. cur = 0;
  88. for (i = 0; i < k; i++) {
  89. x.v = all[cur];
  90. x.k = i;
  91. q = Insert(q, x);
  92. cur += count[i];
  93. }
  94. int index[k];
  95. for (i = 0; i < k; i++) index[i] = 1;
  96. n -= k;
  97. while (n > 0) {
  98. x = ExtractMin(q);
  99. q.count--;
  100. if (q.count > 0) {
  101. q.heap[0] = q.heap[q.count];
  102. heapify(0, q.count, q);
  103. }
  104. cur = x.k;
  105. printf("%d ", x.v);
  106. if (index[cur] < count[cur]) {
  107. now = 0;
  108. for (i = 0; i < cur; i++) now += count[i];
  109. now += index[cur];
  110. x.v = all[now];
  111. index[cur]++;
  112. q = Insert(q, x);
  113. n--;
  114. }
  115. }
  116. for (i = 0; i < q.count; i++) printf("%d ", q.heap[i].v);
  117. printf("\n");
  118. free(q.heap);
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement