Advertisement
kuczi55

Untitled

Mar 5th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. const int N=9;
  7.  
  8. void heapify_min(int t[], int i, int rozmiar)
  9. {
  10. int l=2*i;
  11. int p=2*i+1;
  12. int najm=i;
  13. if(l<=rozmiar && t[l]<t[najm]) najm=l;
  14. if(p<=rozmiar && t[p]<t[najm]) najm=p;
  15. if(najm!=i)
  16. {
  17. swap(t[i], t[najm]);
  18. heapify_min(t, najm, rozmiar);
  19. }
  20. }
  21.  
  22. void build_min_heap(int t[])
  23. {
  24. int rozmiar=t[0];
  25. for(int i=rozmiar/2; i>0; i--)
  26. {
  27. heapify_min(t, i, rozmiar);
  28. }
  29. }
  30.  
  31. void heapify_max(int t[], int i, int rozmiar)
  32. {
  33. int l=2*i;
  34. int p=2*i+1;
  35. int najw=i;
  36. if(l<=rozmiar && t[l]>t[najw]) najw=l;
  37. if(p<=rozmiar && t[p]>t[najw]) najw=p;
  38. if(najw!=i)
  39. {
  40. swap(t[i], t[najw]);
  41. heapify_max(t, najw, rozmiar);
  42. }
  43. }
  44.  
  45. void build_max_heap(int t[])
  46. {
  47. int rozmiar=t[0];
  48. for(int i=rozmiar/2; i>0; i--)
  49. {
  50. heapify_max(t, i, rozmiar);
  51. }
  52. }
  53.  
  54. int GetMedian(int heap_max[], int heap_min[])
  55. {
  56. if(heap_max[0]==heap_min[0])
  57. return (heap_max[1]+heap_min[1])/2;
  58. else if(heap_min[0]>heap_max[0]) return heap_min[1];
  59. else return heap_max[1];
  60. }
  61.  
  62. void rebalance(int heap_max[], int heap_min[])
  63. {
  64. if(heap_max[0]>heap_min[0])
  65. {
  66. heap_min[0]++;
  67. swap(heap_max[1], heap_max[heap_max[0]]);
  68. heap_min[heap_min[0]]=heap_max[heap_max[0]];
  69. heap_max[heap_max[0]]=0;
  70. heap_max[0]--;
  71. heapify_max(heap_max, 1, heap_max[0]);
  72. int i=heap_min[0];
  73. while(i/2>0 && heap_min[i]<heap_min[i/2])
  74. {
  75. swap(heap_min[i], heap_min[i/2]);
  76. i/=2;
  77. }
  78. }
  79. else
  80. {
  81. heap_max[0]++;
  82. swap(heap_min[1], heap_min[heap_min[0]]);
  83. heap_max[heap_max[0]]=heap_min[heap_min[0]];
  84. heap_min[heap_min[0]]=0;
  85. heap_min[0]--;
  86. heapify_min(heap_min, 1, heap_min[0]);
  87. int i=heap_max[0];
  88. while(i/2>0 && heap_max[i]>heap_max[i/2])
  89. {
  90. swap(heap_max[i], heap_max[i/2]);
  91. i/=2;
  92. }
  93. }
  94. }
  95.  
  96. void Insert(int k, int heap_max[], int heap_min[])
  97. {
  98. int m=GetMedian(heap_max, heap_min);
  99. if(k>m)
  100. {
  101. heap_min[0]++;
  102. heap_min[heap_min[0]]=k;
  103. int i=heap_min[0];
  104. while(i/2>0 && heap_min[i]<heap_min[i/2])
  105. {
  106. swap(heap_min[i], heap_min[i/2]);
  107. i/=2;
  108. }
  109. }
  110. else
  111. {
  112. heap_max[0]++;
  113. heap_max[heap_max[0]]=k;
  114. int i=heap_max[0];
  115. while(i/2>0 && heap_max[i]>heap_max[i/2])
  116. {
  117. swap(heap_max[i], heap_max[i/2]);
  118. i/=2;
  119. }
  120. }
  121. if(abs(heap_max[0]-heap_min[0])>1) rebalance(heap_max, heap_min);
  122. }
  123.  
  124. int main()
  125. {
  126. int t[]={1, 4, 6, 9, 11, 13, 17, 19, 21};
  127. int heap_max[N]={0};
  128. for(int i=0; i<N/2; i++)
  129. heap_max[i+1]=t[i];
  130. heap_max[0]=N/2;
  131. build_max_heap(heap_max);
  132. int heap_min[N]={0};
  133. for(int i=N/2; i<N; i++)
  134. heap_min[i-N/2+1]=t[i];
  135. heap_min[0]=N/2+1;
  136. build_min_heap(heap_min);
  137. Insert(2, heap_max, heap_min);
  138. //for(int i=0; i<N; i++) cout << heap_max[i] << " ";
  139. cout << GetMedian(heap_max, heap_min);
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement