vadimk772336

c файлом

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