vadimk772336

Untitled

Nov 2nd, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. enum sort_type
  6. {
  7. heap_max = 0,
  8. heap_min = 1
  9. };
  10.  
  11. struct heap_elements
  12. {
  13. int value;
  14. int own_ID;
  15. };
  16.  
  17. bool operator<(const heap_elements& a, const heap_elements& b)
  18. {
  19. return (a.value < b.value);
  20. }
  21.  
  22. class Heap
  23. {
  24. std::vector<struct heap_elements> h;
  25. int heap_size;
  26.  
  27. public:
  28. Heap();
  29. bool isempty();
  30. heap_elements get_top();
  31. void siftup(int sort_type, std::vector<int>& ID_to_Index);
  32. void siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index);
  33. void swap(int i, int j, std::vector<int>& ID_to_Index);
  34. void add(int sort_type, int ID, int value, std::vector<int>& ID_to_Index);
  35. void delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index);
  36. void out();
  37. };
  38.  
  39. Heap::Heap()
  40. {
  41. std::vector<struct heap_elements> h;
  42. heap_size = 0;
  43. }
  44.  
  45. heap_elements Heap::get_top()
  46. {
  47. return h[0];
  48. }
  49.  
  50. bool Heap::isempty()
  51. {
  52. if (heap_size == 0)
  53. return true;
  54. return false;
  55. }
  56.  
  57. void Heap::swap(int i, int j, std::vector<int>& ID_to_Index)
  58. {
  59. heap_elements buff;
  60. buff = h[i];
  61. h[i] = h[j];
  62. h[j] = buff;
  63.  
  64. ID_to_Index[h[i].own_ID] = i; //На местах в этом массиве лежат айди этой кучи все ок
  65. ID_to_Index[h[j].own_ID] = j;
  66. }
  67.  
  68. void Heap::siftup(int sort_type, std::vector<int>& ID_to_Index)
  69. {
  70. int curr = heap_size - 1;
  71. int parent = (curr - 1) / 2;
  72. while (curr > 0 & parent >= 0)
  73. {
  74. if (sort_type == heap_max & h[parent] < h[curr])
  75. swap(parent, curr, ID_to_Index);
  76.  
  77. if (sort_type == heap_min & h[curr] < h[parent])
  78. swap(parent, curr, ID_to_Index);
  79.  
  80. curr = parent;
  81. parent = (curr - 1) / 2;
  82. }
  83. }
  84.  
  85. void Heap::siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index)
  86. {
  87.  
  88. int parent, max_child;
  89.  
  90. int curr = pos;
  91. int child_l = 2 * curr + 1;
  92. int child_r = 2 * curr + 2;
  93.  
  94. //max_child = (h[child_r] < h[child_l]) ? child_l : child_r;
  95.  
  96. if (h[child_r] < h[child_l])
  97. max_child = child_l;
  98. else
  99. max_child = child_r;
  100.  
  101. while (child_l < heap_size)
  102. {
  103. if (child_l == heap_size - 1)
  104. max_child = child_l;
  105. else if (h[child_r] < h[child_l])
  106. max_child = child_l;
  107. else
  108. max_child = child_r;
  109.  
  110.  
  111. if (sort_type == heap_max & h[curr] < h[max_child])
  112. swap(curr, max_child, ID_to_Index);
  113.  
  114. if (sort_type == heap_min & h[max_child] < h[curr])
  115. swap(curr, max_child, ID_to_Index);
  116.  
  117. curr = max_child;
  118. child_l = 2 * curr + 1;
  119. child_r = 2 * curr + 2;
  120. }
  121. }
  122.  
  123. void Heap::add(int sort_type, int ID, int value, std::vector<int>& ID_to_Index)
  124. {
  125. heap_elements vertex;
  126. vertex.value = value;
  127. vertex.own_ID = ID;
  128.  
  129. h.push_back(vertex);
  130. ID_to_Index.push_back(heap_size); //должно быть общее у двух куч
  131.  
  132. heap_size++;
  133. siftup(sort_type, ID_to_Index);
  134. }
  135.  
  136. void Heap::delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index)
  137. {
  138. int pos = ID_to_Index[ID];
  139. swap(pos, heap_size - 1, ID_to_Index); //Можно не делать свап
  140. h.pop_back();
  141. heap_size--;
  142. ID_to_Index[ID] = -1;
  143. siftdown(sort_type, pos, ID_to_Index);
  144. }
  145.  
  146. void Heap::out(void)
  147. {
  148. cout << "heap: ";
  149. for (int i = 0; i < heap_size - 1; i++)
  150. cout << h[i].value << ", ";
  151.  
  152. cout << h[heap_size - 1].value << ";" << endl;
  153. }
  154.  
  155. void print_vector(std::vector<int>& vector)
  156. {
  157. cout << "vector: ";
  158. for (int i = 0; i < vector.size() - 1; i++)
  159. cout << vector[i] << ", ";
  160. cout << vector[vector.size() - 1] << ";" << endl;
  161. }
  162.  
  163.  
  164. int main()
  165. {
  166.  
  167. Heap heap1;
  168. Heap heap2;
  169. std::vector<int> ID_to_HeapNumber; //При перебрасывании в другую кучу
  170. std::vector<int> ID_to_Index;
  171. int NumberHeap;
  172. int curr_ID = 0;
  173. char c;
  174.  
  175.  
  176. int N, M, K, ID, ID_root1, root2_ID, ID_root2, count = 1;
  177. N = 4;
  178. M = 6;
  179. K = 1;
  180. int R = 0, L = 0;
  181. // int a[N] = { 4, 2, 1, 3, 6, 5, 7 };
  182. int a[N] = { 1, 2, 3, 4 };
  183.  
  184.  
  185. heap1.add(heap_max, a[0], curr_ID, ID_to_Index);
  186. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  187. curr_ID++;
  188. heap1.out();
  189. heap2.out();
  190.  
  191. for (int i = 0; i < M; ++i)
  192. {
  193. cin >> c;
  194.  
  195. if (c == 'R')
  196. {
  197. count++;
  198. R++;
  199. cout << "----------------" << endl;
  200. cout << "R; R = " << R << " count =" << count << endl;
  201.  
  202. if (count < K)
  203. {
  204. cout << "count < K, просто добавляю в кучу1" << endl;
  205.  
  206. heap1.add(heap_max, a[R], curr_ID, ID_to_Index);
  207. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  208. curr_ID++;
  209.  
  210. cout << "answer: "
  211. << "-1" << endl;
  212.  
  213. heap1.out();
  214. heap2.out();
  215. }
  216.  
  217. if (count == K)
  218. {
  219. cout << "count == K, просто добавляю в кучу" << endl;
  220.  
  221. heap1.add(heap_max, a[R], curr_ID, ID_to_Index);
  222. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  223. curr_ID++;
  224.  
  225. cout << "answer: " << heap1.get_top().value << endl;
  226.  
  227. heap1.out();
  228. heap2.out();
  229. }
  230.  
  231. if (count > K)
  232. {
  233. cout << "count > K, Появились лишние - Надо решить куда кидать" << endl;
  234.  
  235. if (a[R] > heap1.get_top().value)
  236. {
  237. cout << "a[R] > корня - во 2 кучу просто" << endl;
  238.  
  239. heap2.add(heap_min, a[R], curr_ID, ID_to_Index);
  240. ID_to_HeapNumber[curr_ID] = 2; //Обновляю ID_to_HeapNumber
  241. curr_ID++;
  242.  
  243. cout << "answer: " << heap1.get_top().value << endl;
  244.  
  245. heap1.out();
  246. heap2.out();
  247. }
  248.  
  249. if (a[R] <= heap1.get_top().value)
  250.  
  251. {
  252. cout << "a[R] <= корня - корень во 2 кучу " << endl;
  253.  
  254. heap_elements root1, root2;
  255.  
  256. // Добавляю корень 1 кучи во 2 кучу не меняя ID
  257. root1 = heap1.get_top();
  258. ID_root1 = root1.own_ID;
  259. heap2.add(heap_min, ID_root1, root1.value, ID_to_Index);
  260. ID_to_HeapNumber[ID_root1] = 2; //Обновляю ID_to_HeapNumber
  261.  
  262. // удаляю корень 1 кучи из 1 кучи
  263. heap2.delete_vertex(heap_min, ID_root1, ID_to_Index);
  264.  
  265. //Добавляю в 1 кучу a[R] меняя айди
  266. heap1.add(heap_max, a[R], curr_ID, ID_to_Index);
  267. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  268. curr_ID++;
  269.  
  270. cout << "answer: " << heap1.get_top().value << endl;
  271.  
  272. heap1.out();
  273. heap2.out();
  274. }
  275. }
  276. }
  277. else
  278. {
  279. L++;
  280. count--;
  281.  
  282. cout << "----------------" << endl;
  283. cout << "L,count = " << L << " " << count << endl;
  284.  
  285. cout << "Удаляем a[L-1]" << endl;
  286.  
  287. ID = L - 1;
  288. NumberHeap = ID_to_HeapNumber[ID];
  289.  
  290. //Если лежит в куче1 то удаляем там, иначе во 2
  291. if (NumberHeap == 1)
  292. heap1.delete_vertex(heap_max, ID, ID_to_Index);
  293. else
  294. heap2.delete_vertex(heap_min, ID, ID_to_Index);
  295.  
  296. heap1.out();
  297. heap2.out();
  298.  
  299. if (count >= K)
  300. {
  301. cout << "count >= K; В 1 куче к-1, во 2 не пусто" << endl;
  302.  
  303. //Добавляем вершину кучи2 в кучу1 не меняя индекс чтобы стало K штук
  304. heap_elements root2 = heap2.get_top();
  305. root2_ID = root2.own_ID;
  306. heap1.add(heap_max, root2_ID, root2.value, ID_to_Index);
  307. ID_to_HeapNumber[root2_ID] = 1; //Обновляю ID_to_HeapNumber
  308.  
  309. //Удаляем вершину кучи2, т.к. теперь она в куче1
  310. heap2.delete_vertex(heap_min, root2_ID, ID_to_Index);
  311. }
  312. else
  313. {
  314. cout << "-1 " << endl;
  315. }
  316. }
  317. }
  318.  
  319. return 0;
  320. }
  321.  
  322.  
Advertisement
Add Comment
Please, Sign In to add comment