Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <algorithm>
  9. #include <set>
  10. #include <queue>
  11. #include <iomanip>
  12.  
  13. using namespace std;
  14.  
  15.  
  16.  
  17. class Heap
  18. {
  19. public:
  20. Heap() = default;
  21. ~Heap() = default;
  22.  
  23. Heap(const std::vector<int>& v) : items(v)
  24. {
  25. Heapify();
  26. }
  27.  
  28. int Peek();
  29. void Insert(int x);
  30.  
  31. bool IsEmpty() { return items.size() != 0; }
  32. int Size() { return items.size(); }
  33.  
  34. protected:
  35. int parent_idx(int n) { return (int)ceil((n - 2) / 2); }
  36. int left_idx(int n) { return 2 * n + 1; }
  37. int right_idx(int n) { return 2 * n + 2; }
  38.  
  39. void Heapify();
  40. virtual void SiftUp(int n) = 0;
  41. virtual void SiftDown(int n) = 0;
  42.  
  43. std::vector<int> items;
  44. };
  45.  
  46. class MinHeap : public Heap
  47. {
  48. public:
  49. MinHeap() = default;
  50. ~MinHeap() = default;
  51.  
  52. int ExtractMin();
  53. protected:
  54. virtual void SiftUp(int n);
  55. virtual void SiftDown(int n);
  56. };
  57.  
  58. class MaxHeap : public Heap
  59. {
  60. public:
  61. MaxHeap() = default;
  62. ~MaxHeap() = default;
  63.  
  64. int ExtractMax();
  65. protected:
  66. virtual void SiftUp(int n);
  67. virtual void SiftDown(int n);
  68. };
  69.  
  70. //-------------------------------------------------------------------------------------
  71.  
  72. int Heap::Peek()
  73. {
  74. return items[0];
  75. }
  76.  
  77. void Heap::Insert(int x)
  78. {
  79. items.push_back(x);
  80. SiftUp(items.size() - 1);
  81. }
  82.  
  83. void Heap::Heapify()
  84. {
  85. int heap_size = items.size();
  86.  
  87. for (int i = heap_size / 2; i >= 0; --i)
  88. SiftDown(i);
  89. }
  90.  
  91. //-------------------------------------------------------------------------------------
  92.  
  93. int MinHeap::ExtractMin()
  94. {
  95. int res = items[0];
  96.  
  97. items[0] = items[items.size() - 1];
  98. items.pop_back();
  99. SiftDown(0);
  100.  
  101. return res;
  102. }
  103.  
  104. void MinHeap::SiftUp(int n)
  105. {
  106. int parent = parent_idx(n);
  107. if (n < 1 || parent < 0 || n > items.size()-1)
  108. return;
  109.  
  110. if (items[parent] > items[n])
  111. {
  112. swap(items[parent], items[n]);
  113. SiftUp(parent);
  114. }
  115. }
  116.  
  117. void MinHeap::SiftDown(int n)
  118. {
  119. int left = left_idx(n);
  120. int right = right_idx(n);
  121. int heap_size = items.size();
  122. int argmin;
  123.  
  124. if (left < heap_size-1 && items[left] < items[n])
  125. argmin = left;
  126. else
  127. argmin = n;
  128.  
  129. if (right < heap_size-1 && items[right] < items[argmin])
  130. argmin = right;
  131.  
  132. if (argmin != n)
  133. {
  134. swap(items[n], items[argmin]);
  135. SiftDown(argmin);
  136. }
  137. }
  138.  
  139. //-------------------------------------------------------------------------------------
  140.  
  141. int MaxHeap::ExtractMax()
  142. {
  143. int res = items[0];
  144.  
  145. items[0] = items[items.size() - 1];
  146. items.pop_back();
  147. SiftDown(0);
  148.  
  149. return res;
  150. }
  151.  
  152. void MaxHeap::SiftUp(int n)
  153. {
  154. int parent = parent_idx(n);
  155. if (n < 1 || parent < 0 || n > items.size()-1)
  156. return;
  157.  
  158. if (items[parent] < items[n])
  159. {
  160. swap(items[parent], items[n]);
  161. SiftUp(parent);
  162. }
  163. }
  164.  
  165. void MaxHeap::SiftDown(int n)
  166. {
  167. int left = left_idx(n);
  168. int right = right_idx(n);
  169. int heap_size = items.size();
  170. int argmax;
  171.  
  172. if (left < heap_size-1 && items[left] > items[n])
  173. argmax = left;
  174. else
  175. argmax = n;
  176.  
  177. if (right < heap_size-1 && items[right] > items[argmax])
  178. argmax = right;
  179.  
  180. if (argmax != n)
  181. {
  182. swap(items[n], items[argmax]);
  183. SiftDown(argmax);
  184. }
  185. }
  186.  
  187. //-------------------------------------------------------------------------------------
  188.  
  189. void AddNumber(int x, MinHeap& mnh, MaxHeap& mxh)
  190. {
  191. if (x < mxh.Peek())
  192. mxh.Insert(x);
  193. else
  194. mnh.Insert(x);
  195. }
  196.  
  197. void Rebalance(MinHeap& mnh, MaxHeap& mxh)
  198. {
  199. if (mnh.Size() - mxh.Size() > 1)
  200. while (mnh.Size() - mxh.Size() > 1)
  201. mxh.Insert(mnh.ExtractMin());
  202.  
  203. else if (mxh.Size() - mnh.Size() > 1)
  204. while (mxh.Size() - mnh.Size() > 1)
  205. mnh.Insert(mxh.ExtractMax());
  206. }
  207.  
  208. double GetMediane(MinHeap& mnh, MaxHeap& mxh)
  209. {
  210. double res;
  211.  
  212. if (mnh.Size() == mxh.Size())
  213. res = ((double)mnh.Peek() + (double)mxh.Peek()) / 2.0;
  214.  
  215. else if (mnh.Size() > mxh.Size())
  216. res = double(mnh.Peek());
  217. else
  218. res = double(mxh.Peek());
  219.  
  220. return res;
  221. }
  222.  
  223. int main()
  224. {
  225. MinHeap mnh;
  226. MaxHeap mxh;
  227.  
  228. int n;
  229. cin >> n;
  230. vector<int> a(n);
  231.  
  232. cout << fixed << setprecision(1);
  233.  
  234. if (n >= 1)
  235. {
  236. cin >> a[0];
  237. cout << double(a[0]) << endl;
  238. }
  239. if (n >= 2)
  240. {
  241. cin >> a[1];
  242. cout << (double)(((double)a[1] + (double)a[0]) /2.0) << endl;
  243. }
  244. if (n >= 3)
  245. {
  246. mnh.Insert(max(a[0], a[1]));
  247. mxh.Insert(min(a[0], a[1]));
  248.  
  249. for (int i = 2; i < n; ++i)
  250. {
  251. cin >> a[i];
  252.  
  253. AddNumber(a[i], mnh, mxh);
  254. Rebalance(mnh, mxh);
  255. double r = GetMediane(mnh, mxh);
  256.  
  257. cout << r << endl;
  258. }
  259. }
  260.  
  261. return 0;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement