Advertisement
rudolf222222

Untitled

Oct 1st, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct HeapNode {
  4. int value;
  5. int index;
  6. int next_index;
  7. };
  8. struct Array {
  9. int amount;
  10. int index;
  11. int* arr;
  12. };
  13. struct Heap {
  14. HeapNode* head;
  15. int hp_size;
  16. };
  17. void MinHeapify(Heap* heap, int index);
  18. void Swap(Heap* heap, int index, int minimum) {
  19. HeapNode temp = heap->head[index];
  20. heap->head[index] = heap->head[minimum];
  21. heap->head[minimum] = temp;
  22. }
  23. int LeftChild(int index) { return 2 * index + 1; }
  24. int RightChild(int index) { return 2 * index + 2; }
  25. void ReplaceMin(Heap* heap, HeapNode small) {
  26. heap->head[0] = small;
  27. MinHeapify(heap, 0);
  28. }
  29. HeapNode ExtractMin(Heap* heap) { return heap->head[0]; }
  30. void MinHeapify(Heap* heap, int index) {
  31. int left_index = 2 * index + 1;
  32. int right_index = 2 * index + 2;
  33. int minimum = index;
  34. if (left_index < heap->hp_size &&
  35. heap->head[left_index].value < heap->head[minimum].value) {
  36. minimum = left_index;
  37. }
  38. if (right_index < heap->hp_size &&
  39. heap->head[right_index].value < heap->head[minimum].value) {
  40. minimum = right_index;
  41. }
  42. if (minimum != index) {
  43. Swap(heap, index, minimum);
  44. MinHeapify(heap, minimum);
  45. }
  46. }
  47. Heap* HeapCreate(HeapNode* arr, int size) {
  48. Heap* heap = new Heap();
  49. heap->head = arr;
  50. heap->hp_size = size;
  51. int mid_index = (heap->hp_size - 1) / 2;
  52. while (mid_index >= 0) {
  53. MinHeapify(heap, mid_index);
  54. --mid_index;
  55. }
  56. return heap;
  57. }
  58. int* Merge(Array* arrays, int array_num, int size) {
  59. int* res = new int[size * 2];
  60. HeapNode* head = new HeapNode[array_num];
  61. for (int i = 0; i < array_num; ++i) {
  62. head[i].value = arrays[i].arr[0];
  63. head[i].index = i;
  64. head[i].next_index = 1;
  65. }
  66. Heap* heap = HeapCreate(head, array_num);
  67. for (int count = 0; count < size; count++) {
  68. HeapNode root = ExtractMin(heap);
  69. res[count] = root.value;
  70. if (root.next_index < arrays[root.index].amount) {
  71. root.value = arrays[root.index].arr[root.next_index];
  72. root.next_index++;
  73. } else {
  74. root.value = __INT_MAX__;
  75. }
  76. ReplaceMin(heap, root);
  77. }
  78. delete heap;
  79. return res;
  80. }
  81. int main() {
  82. int array_num, size = 0;
  83. std::cin >> array_num;
  84. Array* arrays = new Array[array_num];
  85. for (int i = 0; i < array_num; i++) {
  86. std::cin >> arrays[i].amount;
  87. arrays[i].arr = new int[arrays[i].amount];
  88. for (int k = 0; k < arrays[i].amount; k++) {
  89. std::cin >> arrays[i].arr[k];
  90. size++;
  91. // std::cout << arrays[i].arr[k] << " ";
  92. }
  93. }
  94. int* res = Merge(arrays, array_num, size);
  95. for (int i = 0; i < size; i++) {
  96. std::cout << res[i] << " ";
  97. }
  98. std::cout << "\n";
  99. delete[] res;
  100. delete[] arrays;
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement