vadimk772336

ласт

Nov 3rd, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. #include <fstream>
  5.  
  6. enum sort_type
  7. {
  8. heap_max = 0,
  9. heap_min = 1
  10. };
  11.  
  12. struct heap_elements
  13. {
  14. int value;
  15. int own_ID;
  16. };
  17.  
  18. bool operator<(const heap_elements& a, const heap_elements& b)
  19. {
  20. return (a.value < b.value);
  21. }
  22.  
  23. class Heap
  24. {
  25. std::vector<struct heap_elements> h;
  26. int heap_size;
  27.  
  28. public:
  29. Heap();
  30. bool isempty();
  31. heap_elements get_top();
  32. void siftup(int sort_type, int pos, std::vector<int>& ID_to_Index);
  33. void siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index);
  34. void swap(int i, int j, std::vector<int>& ID_to_Index);
  35. void add(int sort_type, int ID, int value, std::vector<int>& ID_to_Index);
  36. void delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index);
  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, int pos, std::vector<int>& ID_to_Index)
  69. {
  70. int curr = pos;
  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.  
  86. void Heap::siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index)
  87. {
  88.  
  89. int parent, max_child, min_child;
  90.  
  91. int curr = pos;
  92. int child_l = 2 * curr + 1;
  93. int child_r = 2 * curr + 2;
  94.  
  95. while (child_l < heap_size)
  96. {
  97. if (sort_type == heap_max)
  98. {
  99. if (child_l == heap_size - 1)
  100. max_child = child_l;
  101. else if (h[child_r] < h[child_l])
  102. max_child = child_l;
  103. else
  104. max_child = child_r;
  105.  
  106. if (h[curr] < h[max_child])
  107. swap(curr, max_child, ID_to_Index);
  108.  
  109. curr = max_child;
  110. child_l = 2 * curr + 1;
  111. child_r = 2 * curr + 2;
  112. }
  113.  
  114. if (sort_type == heap_min)
  115. {
  116.  
  117. if (child_l == heap_size - 1)
  118. min_child = child_l;
  119. else if (h[child_r] < h[child_l])
  120. min_child = child_r;
  121. else
  122. min_child = child_l;
  123.  
  124.  
  125. if (h[min_child] < h[curr]) {
  126. swap(curr, min_child, ID_to_Index);
  127. }
  128. curr = min_child;
  129. child_l = 2 * curr + 1;
  130. child_r = 2 * curr + 2;
  131. }
  132. }
  133. }
  134.  
  135.  
  136.  
  137. void Heap::add(int sort_type, int ID, int value, std::vector<int>& ID_to_Index)
  138. {
  139. heap_elements vertex;
  140. vertex.value = value;
  141. vertex.own_ID = ID;
  142.  
  143. h.push_back(vertex);
  144.  
  145.  
  146. if (ID < ID_to_Index.size()) {
  147. ID_to_Index[ID] = heap_size;
  148. }
  149. else {
  150. ID_to_Index.push_back(heap_size); //должно быть общее у двух куч
  151. }
  152. heap_size++;
  153. siftup(sort_type, heap_size-1, ID_to_Index);
  154. }
  155.  
  156. void Heap::delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index)
  157. {
  158.  
  159. int pos = ID_to_Index[ID];
  160.  
  161.  
  162.  
  163.  
  164.  
  165. swap(pos, heap_size - 1, ID_to_Index); //Можно не делать свап
  166. h.pop_back();
  167. heap_size--;
  168. ID_to_Index[ID] = -1; //мб убрать
  169.  
  170. int parent = (pos - 1) / 2;
  171.  
  172. if (sort_type == heap_min)
  173. {
  174. if (h[pos] < h[parent])
  175. siftup(sort_type, pos, ID_to_Index);
  176. else
  177. siftdown(sort_type, pos, ID_to_Index);
  178. }
  179.  
  180. if (sort_type == heap_max)
  181. {
  182. if (h[parent] < h[pos])
  183. siftup(sort_type, pos, ID_to_Index);
  184. else
  185. siftdown(sort_type, pos, ID_to_Index);
  186. }
  187.  
  188. }
  189.  
  190.  
  191.  
  192.  
  193. int main()
  194. {
  195.  
  196.  
  197.  
  198. Heap heap1;
  199. Heap heap2;
  200.  
  201. std::vector<int> ID_to_Index;
  202. int NumberHeap;
  203. int curr_ID = 0;
  204. char c;
  205.  
  206. int N, M, K, ID, ID_root1, root2_ID, ID_root2, count = 1;
  207. cin >> N;
  208. cin >> M;
  209. cin >> K;
  210. int R = 0, L = 0;
  211. int a[N];
  212. for (int j = 0; j < N; j++)
  213. cin >> a[j];
  214.  
  215. int ID_to_HeapNumber[N];
  216.  
  217. heap1.add(heap_max, curr_ID, a[0], ID_to_Index);
  218. ID_to_HeapNumber[curr_ID] = 1;
  219. curr_ID++;
  220.  
  221.  
  222.  
  223. for (int i = 0; i < M; ++i)
  224. {
  225.  
  226. cin >> c;
  227.  
  228. if (c == 'R')
  229. {
  230. count++;
  231. R++;
  232.  
  233.  
  234. if (count < K)
  235. {
  236.  
  237.  
  238. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  239. ID_to_HeapNumber[curr_ID] = 1;
  240. curr_ID++;
  241.  
  242. cout << -1 << endl;
  243.  
  244. }
  245.  
  246. if (count == K)
  247. {
  248.  
  249.  
  250. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  251. ID_to_HeapNumber[curr_ID] = 1;
  252. curr_ID++;
  253.  
  254. cout << heap1.get_top().value << endl;
  255.  
  256.  
  257. }
  258.  
  259. if (count > K)
  260. {
  261.  
  262.  
  263. if (a[R] > heap1.get_top().value)
  264. {
  265.  
  266. heap2.add(heap_min, curr_ID, a[R], ID_to_Index);
  267. ID_to_HeapNumber[curr_ID] = 2;
  268. curr_ID++;
  269. cout << heap1.get_top().value << endl;
  270.  
  271. }
  272.  
  273. if (a[R] <= heap1.get_top().value)
  274.  
  275. {
  276.  
  277.  
  278. heap_elements root1, root2;
  279.  
  280. //Запоминаю корень 1 кучи
  281. root1 = heap1.get_top();
  282. ID_root1 = root1.own_ID;
  283.  
  284. // удаляю корень 1 кучи из 1 кучи
  285. heap1.delete_vertex(heap_max, ID_root1, ID_to_Index);
  286.  
  287.  
  288. // Добавляю корень 1 кучи во 2 кучу не меняя ID
  289. heap2.add(heap_min, ID_root1, root1.value, ID_to_Index);
  290. ID_to_HeapNumber[ID_root1] = 2;
  291.  
  292.  
  293. //Добавляю в 1 кучу a[R] меняя айди
  294. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  295. ID_to_HeapNumber[curr_ID] = 1;
  296. curr_ID++;
  297.  
  298.  
  299. cout << heap1.get_top().value << endl;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. L++;
  306. count--;
  307.  
  308.  
  309. ID = L - 1;
  310. NumberHeap = ID_to_HeapNumber[ID];
  311.  
  312.  
  313.  
  314.  
  315. //Если лежит в куче1 то удаляем там, иначе во 2
  316. if (NumberHeap == 1)
  317. heap1.delete_vertex(heap_max, ID, ID_to_Index);
  318. else
  319. heap2.delete_vertex(heap_min, ID, ID_to_Index);
  320.  
  321.  
  322.  
  323. if (count >= K)
  324. {
  325.  
  326.  
  327.  
  328. //Если удалили из 2 кучи, то первая не пострадала и просто принтим
  329. if (NumberHeap == 2)
  330. {
  331. cout << heap1.get_top().value << endl;
  332.  
  333. }
  334.  
  335. else
  336. {
  337.  
  338. //Запоминаю вершину кучи2
  339. heap_elements root2 = heap2.get_top();
  340. root2_ID = root2.own_ID;
  341.  
  342.  
  343. //Удаляем вершину кучи2, т.к. теперь она в куче1
  344. heap2.delete_vertex(heap_min, root2_ID, ID_to_Index);
  345.  
  346.  
  347. //Добавляем вершину кучи2 в кучу1 не меняя индекс чтобы стало K штук
  348. heap1.add(heap_max, root2_ID, root2.value, ID_to_Index);
  349. ID_to_HeapNumber[root2_ID] = 1;
  350.  
  351. cout << heap1.get_top().value << endl;
  352.  
  353.  
  354. }
  355. }
  356. else
  357. {
  358. cout << -1 << endl;
  359. }
  360. }
  361. }
  362.  
  363.  
  364.  
  365.  
  366. return 0;
  367. }
  368.  
Advertisement
Add Comment
Please, Sign In to add comment