vadimk772336

улучшенный файл

Nov 2nd, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.34 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. void out();
  38. void print_vector(std::vector<int>& vector);
  39. };
  40.  
  41. Heap::Heap()
  42. {
  43. std::vector<struct heap_elements> h;
  44. heap_size = 0;
  45. }
  46.  
  47. heap_elements Heap::get_top()
  48. {
  49. return h[0];
  50. }
  51.  
  52. bool Heap::isempty()
  53. {
  54. if (heap_size == 0)
  55. return true;
  56. return false;
  57. }
  58.  
  59. void Heap::swap(int i, int j, std::vector<int>& ID_to_Index)
  60. {
  61. heap_elements buff;
  62. buff = h[i];
  63. h[i] = h[j];
  64. h[j] = buff;
  65.  
  66. ID_to_Index[h[i].own_ID] = i; //На местах в этом массиве лежат айди этой кучи все ок
  67. ID_to_Index[h[j].own_ID] = j;
  68. }
  69.  
  70. void Heap::siftup(int sort_type, std::vector<int>& ID_to_Index)
  71. {
  72. int curr = heap_size - 1;
  73. int parent = (curr - 1) / 2;
  74. while (curr > 0 & parent >= 0)
  75. {
  76. if (sort_type == heap_max & h[parent] < h[curr])
  77. swap(parent, curr, ID_to_Index);
  78.  
  79. if (sort_type == heap_min & h[curr] < h[parent])
  80. swap(parent, curr, ID_to_Index);
  81.  
  82. curr = parent;
  83. parent = (curr - 1) / 2;
  84. }
  85. }
  86.  
  87. void Heap::siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index)
  88. {
  89.  
  90. int parent, max_child;
  91.  
  92. int curr = pos;
  93. int child_l = 2 * curr + 1;
  94. int child_r = 2 * curr + 2;
  95.  
  96. //max_child = (h[child_r] < h[child_l]) ? child_l : child_r;
  97.  
  98. if (h[child_r] < h[child_l])
  99. max_child = child_l;
  100. else
  101. max_child = child_r;
  102.  
  103. while (child_l < heap_size)
  104. {
  105. if (child_l == heap_size - 1)
  106. max_child = child_l;
  107. else if (h[child_r] < h[child_l])
  108. max_child = child_l;
  109. else
  110. max_child = child_r;
  111.  
  112.  
  113. if (sort_type == heap_max & h[curr] < h[max_child])
  114. swap(curr, max_child, ID_to_Index);
  115.  
  116. if (sort_type == heap_min & h[max_child] < h[curr])
  117. swap(curr, max_child, ID_to_Index);
  118.  
  119. curr = max_child;
  120. child_l = 2 * curr + 1;
  121. child_r = 2 * curr + 2;
  122. }
  123. }
  124.  
  125. void Heap::print_vector(std::vector<int>& vector)
  126. {
  127. if (vector.size() == 0)
  128. cout << "None " << endl;
  129. else {
  130. cout << "vector: ";
  131. for (int i = 0; i < vector.size() - 1; i++)
  132. cout << vector[i] << ", ";
  133. cout << vector[vector.size() - 1] << ";" << endl;
  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. //Если это новое добавление, то надо делать пушбек,
  147. //а если старое, то надо просто на место ID написать heap_size
  148. cout << "Сейчас будем добавлять. Но новый или старый?" << endl;
  149. cout << "ID_to_Index.size() = " << ID_to_Index.size() << endl;
  150. cout << "heap_size = " << heap_size << endl;
  151. cout << "ID = " << ID << endl;
  152.  
  153. if (ID < ID_to_Index.size()) {
  154. cout << "Значит это старый, ID_to_Index до" << endl;
  155. print_vector(ID_to_Index);
  156. ID_to_Index[ID] = heap_size;
  157. cout << "After";
  158. print_vector(ID_to_Index);
  159. }
  160. else {
  161. cout << "Значит это новый, ID_to_Index до" << endl;
  162. print_vector(ID_to_Index);
  163. ID_to_Index.push_back(heap_size); //должно быть общее у двух куч
  164. cout << "After";
  165. print_vector(ID_to_Index);
  166. }
  167. heap_size++;
  168. siftup(sort_type, ID_to_Index);
  169. }
  170.  
  171. void Heap::delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index)
  172. {
  173.  
  174. print_vector(ID_to_Index);
  175. int pos = ID_to_Index[ID];
  176.  
  177. cout << "Вызвали удаление, ID = " << ID << endl;
  178. cout << "pos = " << pos << endl;
  179.  
  180. swap(pos, heap_size - 1, ID_to_Index); //Можно не делать свап
  181. h.pop_back();
  182. heap_size--;
  183. ID_to_Index[ID] = -1; //мб убрать
  184. siftdown(sort_type, pos, ID_to_Index);
  185. }
  186.  
  187. void Heap::out(void)
  188. {
  189. if (heap_size == 0)
  190. cout << "heap: None " << endl;
  191. else
  192. {
  193. cout << "heap: ";
  194. for (int i = 0; i < heap_size - 1; i++)
  195. cout << h[i].value << ", ";
  196.  
  197. cout << h[heap_size - 1].value << ";" << endl;
  198. }
  199. }
  200.  
  201. void print_vector(std::vector<int>& vector)
  202. {
  203. cout << "vector: ";
  204. for (int i = 0; i < vector.size() - 1; i++)
  205. cout << vector[i] << ", ";
  206. cout << vector[vector.size() - 1] << ";" << endl;
  207. }
  208.  
  209.  
  210. int main()
  211. {
  212.  
  213.  
  214. ifstream ifs("com.txt");
  215. ofstream out("results.txt");
  216.  
  217.  
  218. Heap heap1;
  219. Heap heap2;
  220.  
  221. std::vector<int> ID_to_Index;
  222. int NumberHeap;
  223. int curr_ID = 0;
  224. char c;
  225.  
  226. int N, M, K, ID, ID_root1, root2_ID, ID_root2, count = 1;
  227. N = 49;
  228.  
  229. M = 15;
  230. int answers[M];
  231. K = 3;
  232. int R = 0, L = 0;
  233. //int a[N] = { 4, 2, 1, 3, 6, 5, 7 };
  234. int a[N] = {28 ,43, 23, 3, 24, 39 ,45, 45, 17, 25, 14, 9, 40, 5, 29, 6, 12, 1, 2, 7, 27, 38, 22, 33, 30, 15, 13, 37, 49, 41, 11, 20, 19, 21, 46, 48, 16, 44, 42, 10, 36, 35, 31, 4, 18, 32, 26, 34, 8, 47};
  235. int ID_to_HeapNumber[N]; //При перебрасывании в другую кучу
  236.  
  237. heap1.add(heap_max, curr_ID, a[0], ID_to_Index);
  238. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  239. curr_ID++;
  240. heap1.out();
  241. heap2.out();
  242.  
  243. for (int i = 0; i < M; ++i)
  244. {
  245. cout << "----------------" << endl;
  246. //cin >> c;
  247. ifs >> c;
  248.  
  249. if (c == 'R')
  250. {
  251. count++;
  252. R++;
  253.  
  254. cout << "R; R = " << R << " count =" << count << endl;
  255.  
  256. if (count < K)
  257. {
  258. cout << "count < K, просто добавляю в кучу1" << endl;
  259.  
  260. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  261. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  262. curr_ID++;
  263.  
  264. cout << "--------------------ANSWER--------------: "
  265. << "-1" << endl;
  266. answers[i] = -1;
  267.  
  268. heap1.out();
  269. heap2.out();
  270. }
  271.  
  272. if (count == K)
  273. {
  274. cout << "count == K, просто добавляю в кучу" << endl;
  275.  
  276. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  277. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  278. curr_ID++;
  279.  
  280. cout << "--------------------ANSWER--------------: " << heap1.get_top().value << endl;
  281. answers[i] = heap1.get_top().value;
  282. heap1.out();
  283. heap2.out();
  284. }
  285.  
  286. if (count > K)
  287. {
  288. cout << "count > K, Появились лишние - Надо решить куда кидать" << endl;
  289.  
  290. if (a[R] > heap1.get_top().value)
  291. {
  292. cout << "a[R] > корня - во 2 кучу просто" << endl;
  293.  
  294. heap2.add(heap_min, curr_ID, a[R], ID_to_Index);
  295. ID_to_HeapNumber[curr_ID] = 2; //Обновляю ID_to_HeapNumber
  296. curr_ID++;
  297.  
  298. cout << "--------------------ANSWER--------------: " << heap1.get_top().value << endl;
  299. answers[i] = heap1.get_top().value;
  300. heap1.out(); heap2.out();
  301.  
  302. }
  303.  
  304. if (a[R] <= heap1.get_top().value)
  305.  
  306. {
  307. cout << "a[R] <= корня - корень во 2 кучу " << endl;
  308.  
  309. heap_elements root1, root2;
  310.  
  311. //Запоминаю корень 1 кучи
  312. root1 = heap1.get_top();
  313. ID_root1 = root1.own_ID;
  314.  
  315. cout << "удаляю корень 1 кучи из 1 кучи" << endl;
  316. // удаляю корень 1 кучи из 1 кучи
  317. heap1.delete_vertex(heap_max, ID_root1, ID_to_Index);
  318. heap1.out(); heap2.out();
  319.  
  320. cout << "Добавляю корень 1 кучи во 2 кучу не меняя ID" << endl;
  321. // Добавляю корень 1 кучи во 2 кучу не меняя ID
  322. heap2.add(heap_min, ID_root1, root1.value, ID_to_Index);
  323. ID_to_HeapNumber[ID_root1] = 2; //Обновляю ID_to_HeapNumber
  324. heap1.out(); heap2.out();
  325.  
  326.  
  327. cout << "Добавляю в 1 кучу a[R] меняя айди" << endl;
  328. //Добавляю в 1 кучу a[R] меняя айди
  329. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  330. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  331. curr_ID++;
  332. heap1.out(); heap2.out();
  333. answers[i] = heap1.get_top().value;
  334. cout << "--------------------ANSWER--------------: " << heap1.get_top().value << endl;
  335.  
  336.  
  337. }
  338. }
  339. }
  340. else
  341. {
  342. L++;
  343. count--;
  344.  
  345. cout << "L,count = " << L << " " << count << endl;
  346.  
  347. cout << "Удаляем a[L-1]" << endl;
  348.  
  349. ID = L - 1;
  350. NumberHeap = ID_to_HeapNumber[ID];
  351.  
  352. cout << "NumberHeap = " << NumberHeap << endl;
  353.  
  354. cout << " Если лежит в куче1 то удаляем там, иначе во 2 " << endl;
  355. //Если лежит в куче1 то удаляем там, иначе во 2
  356. if (NumberHeap == 1)
  357. heap1.delete_vertex(heap_max, ID, ID_to_Index);
  358. else
  359. heap2.delete_vertex(heap_min, ID, ID_to_Index);
  360.  
  361. heap1.out();
  362. heap2.out();
  363.  
  364. if (count >= K)
  365. {
  366. cout << "count >= K; В 1 куче к-1, во 2 не пусто" << endl;
  367.  
  368.  
  369. //Если удалили из 2 кучи, то первая не пострадала и просто принтим
  370. if (NumberHeap == 2)
  371. {
  372. cout << "удалили из 2 кучи, то первая не пострадала и просто принтим " << endl;
  373. cout << "--------------------ANSWER--------------: " << heap1.get_top().value << endl;
  374. answers[i] = heap1.get_top().value;
  375. }
  376.  
  377. else
  378. {
  379. cout << "удалили из 1 кучи - надо восстановить баланс";
  380. //Запоминаю вершину кучи2
  381. heap_elements root2 = heap2.get_top();
  382. root2_ID = root2.own_ID;
  383.  
  384. cout << "//Удаляем вершину кучи2, т.к. она будет в куче1" << endl;
  385. //Удаляем вершину кучи2, т.к. теперь она в куче1
  386. heap2.delete_vertex(heap_min, root2_ID, ID_to_Index);
  387. heap1.out(); heap2.out();
  388.  
  389. cout << "Добавляем вершину кучи2 в кучу1 не меняя индекс чтобы стало K штук" << endl;
  390. //Добавляем вершину кучи2 в кучу1 не меняя индекс чтобы стало K штук
  391. heap1.add(heap_max, root2_ID, root2.value, ID_to_Index);
  392. ID_to_HeapNumber[root2_ID] = 1; //Обновляю ID_to_HeapNumber
  393.  
  394. cout << "--------------------ANSWER--------------: " << heap1.get_top().value << endl;
  395. answers[i] = heap1.get_top().value;
  396. heap1.out(); heap2.out();
  397. }
  398.  
  399. }
  400. else
  401. {
  402. cout << "--------------------ANSWER--------------: -1 " << endl;
  403. answers[i] = -1;
  404. }
  405. }
  406. }
  407.  
  408. cout << endl;
  409. for (int j=0;j<M;j++)
  410. cout << answers[j] << " ";
  411. cout << endl;
  412.  
  413.  
  414. return 0;
  415. }
  416.  
  417.  
Advertisement
Add Comment
Please, Sign In to add comment