vadimk772336

РАБОАТЕТ файл

Nov 3rd, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.89 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. void out();
  38. void outHeap();
  39. void print_vector(std::vector<int>& vector);
  40. };
  41.  
  42. Heap::Heap()
  43. {
  44. std::vector<struct heap_elements> h;
  45. heap_size = 0;
  46. }
  47.  
  48. heap_elements Heap::get_top()
  49. {
  50. return h[0];
  51. }
  52.  
  53. bool Heap::isempty()
  54. {
  55. if (heap_size == 0)
  56. return true;
  57. return false;
  58. }
  59.  
  60. void Heap::swap(int i, int j, std::vector<int>& ID_to_Index)
  61. {
  62. heap_elements buff;
  63. buff = h[i];
  64. h[i] = h[j];
  65. h[j] = buff;
  66.  
  67. ID_to_Index[h[i].own_ID] = i; //На местах в этом массиве лежат айди этой кучи все ок
  68. ID_to_Index[h[j].own_ID] = j;
  69. }
  70.  
  71. void Heap::siftup(int sort_type, int pos, std::vector<int>& ID_to_Index)
  72. {
  73. int curr = pos;
  74. int parent = (curr - 1) / 2;
  75. while (curr > 0 & parent >= 0)
  76. {
  77. if (sort_type == heap_max & h[parent] < h[curr])
  78. swap(parent, curr, ID_to_Index);
  79.  
  80. if (sort_type == heap_min & h[curr] < h[parent])
  81. swap(parent, curr, ID_to_Index);
  82.  
  83. curr = parent;
  84. parent = (curr - 1) / 2;
  85. }
  86. }
  87.  
  88. void Heap::outHeap() {
  89. int i = 0;
  90. int k = 1;
  91. while(i < heap_size) {
  92. while((i < k) && (i < heap_size)) {
  93. cout << h[i].value << " ";
  94. i++;
  95. }
  96. cout << endl;
  97. k = k * 2 + 1;
  98. }
  99. }
  100.  
  101. void Heap::siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index)
  102. {
  103.  
  104. int parent, max_child, min_child;
  105.  
  106. int curr = pos;
  107. int child_l = 2 * curr + 1;
  108. int child_r = 2 * curr + 2;
  109.  
  110. while (child_l < heap_size)
  111. {
  112. cout << "child_l < heap_size:" << child_l << "< " << heap_size << endl;
  113. if (sort_type == heap_max)
  114. {
  115. if (child_l == heap_size - 1)
  116. max_child = child_l;
  117. else if (h[child_r] < h[child_l])
  118. max_child = child_l;
  119. else
  120. max_child = child_r;
  121.  
  122. if (h[curr] < h[max_child])
  123. swap(curr, max_child, ID_to_Index);
  124.  
  125. curr = max_child;
  126. child_l = 2 * curr + 1;
  127. child_r = 2 * curr + 2;
  128. }
  129.  
  130. if (sort_type == heap_min)
  131. {
  132. cout << "sort_type == heap_min" << endl;
  133. if (child_l == heap_size - 1)
  134. min_child = child_l;
  135. else if (h[child_r] < h[child_l])
  136. min_child = child_r;
  137. else
  138. min_child = child_l;
  139.  
  140. cout << "min_child = " << min_child << endl;
  141. if (h[min_child] < h[curr]) {
  142. cout << "min_child, curr " << min_child << " " << curr << endl;
  143. cout << "h[min_child] < h[curr]: " << h[min_child].value << " < " << h[curr].value << endl;
  144. swap(curr, min_child, ID_to_Index);
  145. }
  146. curr = min_child;
  147. child_l = 2 * curr + 1;
  148. child_r = 2 * curr + 2;
  149. }
  150. }
  151. }
  152.  
  153. void Heap::print_vector(std::vector<int>& vector)
  154. {
  155. if (vector.size() == 0)
  156. cout << "None " << endl;
  157. else {
  158. cout << "vector: ";
  159. for (int i = 0; i < vector.size() - 1; i++)
  160. cout << vector[i] << ", ";
  161. cout << vector[vector.size() - 1] << ";" << endl;
  162. }
  163. }
  164.  
  165. void Heap::add(int sort_type, int ID, int value, std::vector<int>& ID_to_Index)
  166. {
  167. heap_elements vertex;
  168. vertex.value = value;
  169. vertex.own_ID = ID;
  170.  
  171. h.push_back(vertex);
  172.  
  173.  
  174. //Если это новое добавление, то надо делать пушбек,
  175. //а если старое, то надо просто на место ID написать heap_size
  176. cout << "Сейчас будем добавлять. Но новый или старый?" << endl;
  177. cout << "ID_to_Index.size() = " << ID_to_Index.size() << endl;
  178. cout << "heap_size = " << heap_size << endl;
  179. cout << "ID = " << ID << endl;
  180.  
  181. if (ID < ID_to_Index.size()) {
  182. cout << "Значит это старый, ID_to_Index до" << endl;
  183. print_vector(ID_to_Index);
  184. ID_to_Index[ID] = heap_size;
  185. cout << "After";
  186. print_vector(ID_to_Index);
  187. }
  188. else {
  189. cout << "Значит это новый, ID_to_Index до" << endl;
  190. print_vector(ID_to_Index);
  191. ID_to_Index.push_back(heap_size); //должно быть общее у двух куч
  192. cout << "After";
  193. print_vector(ID_to_Index);
  194. }
  195. heap_size++;
  196. siftup(sort_type, heap_size-1, ID_to_Index);
  197. }
  198.  
  199. void Heap::delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index)
  200. {
  201.  
  202. int pos = ID_to_Index[ID];
  203.  
  204. cout << "Вызвали удаление, ID = " << ID << endl;
  205. cout << "pos = " << pos << endl;
  206. print_vector(ID_to_Index);
  207.  
  208.  
  209.  
  210. swap(pos, heap_size - 1, ID_to_Index); //Можно не делать свап
  211. h.pop_back();
  212. heap_size--;
  213. ID_to_Index[ID] = -1; //мб убрать
  214.  
  215. int parent = (pos - 1) / 2;
  216.  
  217. if (sort_type == heap_min)
  218. {
  219. if (h[pos] < h[parent])
  220. siftup(sort_type, pos, ID_to_Index);
  221. else
  222. siftdown(sort_type, pos, ID_to_Index);
  223. }
  224.  
  225. if (sort_type == heap_max)
  226. {
  227. if (h[parent] < h[pos])
  228. siftup(sort_type, pos, ID_to_Index);
  229. else
  230. siftdown(sort_type, pos, ID_to_Index);
  231. }
  232.  
  233. }
  234.  
  235. void Heap::out(void)
  236. {
  237. if (heap_size == 0)
  238. cout << "heap: None " << endl;
  239. else
  240. {
  241. cout << "heap: ";
  242. for (int i = 0; i < heap_size - 1; i++)
  243. cout << h[i].value << ", ";
  244.  
  245. cout << h[heap_size - 1].value << ";" << endl;
  246. }
  247. }
  248.  
  249. void print_vector(std::vector<int>& vector)
  250. {
  251. cout << "vector: ";
  252. for (int i = 0; i < vector.size() - 1; i++)
  253. cout << vector[i] << ", ";
  254. cout << vector[vector.size() - 1] << ";" << endl;
  255. }
  256.  
  257.  
  258. int main()
  259. {
  260.  
  261.  
  262. ifstream ifs("com.txt");
  263. ofstream out("results.txt");
  264. int count_tests;
  265. ifs >> count_tests;
  266.  
  267. for (int k = 0; k < count_tests; k++)
  268. {
  269.  
  270. Heap heap1;
  271. Heap heap2;
  272.  
  273. std::vector<int> ID_to_Index;
  274. int NumberHeap;
  275. int curr_ID = 0;
  276. char c;
  277.  
  278. int N, M, K, ID, ID_root1, root2_ID, ID_root2, count = 1;
  279. ifs >> N;
  280. ifs >> M;
  281. int answers[M];
  282. K = 3;
  283. int R = 0, L = 0;
  284. int a[N];
  285. for (int j = 0; j < N; j++)
  286. ifs >> a[j];
  287.  
  288. int ID_to_HeapNumber[N]; //При перебрасывании в другую кучу
  289.  
  290. heap1.add(heap_max, curr_ID, a[0], ID_to_Index);
  291. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  292. curr_ID++;
  293. heap1.out();
  294. heap2.outHeap();
  295.  
  296. for (int i = 0; i < M; ++i)
  297. {
  298. cout << "-------------------------------ЗАПРОС НОМЕР " << i
  299. << "-----------------------------------------" << endl;
  300. // cin >> c;
  301. ifs >> c;
  302.  
  303. if (c == 'R')
  304. {
  305. count++;
  306. R++;
  307.  
  308. cout << "R; R = " << R << " count =" << count << endl;
  309.  
  310. if (count < K)
  311. {
  312. cout << "count < K, просто добавляю в кучу1" << endl;
  313.  
  314. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  315. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  316. curr_ID++;
  317.  
  318. cout << "--------------------ANSWER--------------: "
  319. << "-1" << endl;
  320. answers[i] = -1;
  321.  
  322. heap1.out();
  323. heap2.outHeap();
  324. }
  325.  
  326. if (count == K)
  327. {
  328. cout << "count == K, просто добавляю в кучу" << endl;
  329.  
  330. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  331. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  332. curr_ID++;
  333.  
  334. cout << "--------------------ANSWER--------------: " << heap1.get_top().value
  335. << endl;
  336. answers[i] = heap1.get_top().value;
  337. heap1.out();
  338. heap2.outHeap();
  339. }
  340.  
  341. if (count > K)
  342. {
  343. cout << "count > K, Появились лишние - Надо решить куда кидать" << endl;
  344.  
  345. if (a[R] > heap1.get_top().value)
  346. {
  347. cout << "a[R] > корня - во 2 кучу просто" << endl;
  348.  
  349. heap2.add(heap_min, curr_ID, a[R], ID_to_Index);
  350. ID_to_HeapNumber[curr_ID] = 2; //Обновляю ID_to_HeapNumber
  351. curr_ID++;
  352.  
  353. cout << "--------------------ANSWER--------------: "
  354. << heap1.get_top().value << endl;
  355. answers[i] = heap1.get_top().value;
  356. heap1.out();
  357. heap2.outHeap();
  358. }
  359.  
  360. if (a[R] <= heap1.get_top().value)
  361.  
  362. {
  363. cout << "a[R] <= корня - корень во 2 кучу " << endl;
  364.  
  365. heap_elements root1, root2;
  366.  
  367. //Запоминаю корень 1 кучи
  368. root1 = heap1.get_top();
  369. ID_root1 = root1.own_ID;
  370.  
  371. cout << "удаляю корень 1 кучи из 1 кучи" << endl;
  372. // удаляю корень 1 кучи из 1 кучи
  373. heap1.delete_vertex(heap_max, ID_root1, ID_to_Index);
  374. heap1.out();
  375. heap2.outHeap();
  376.  
  377. cout << "Добавляю корень 1 кучи во 2 кучу не меняя ID" << endl;
  378. // Добавляю корень 1 кучи во 2 кучу не меняя ID
  379. heap2.add(heap_min, ID_root1, root1.value, ID_to_Index);
  380. ID_to_HeapNumber[ID_root1] = 2; //Обновляю ID_to_HeapNumber
  381. heap1.out();
  382. heap2.outHeap();
  383.  
  384.  
  385. cout << "Добавляю в 1 кучу a[R] меняя айди" << endl;
  386. //Добавляю в 1 кучу a[R] меняя айди
  387. heap1.add(heap_max, curr_ID, a[R], ID_to_Index);
  388. ID_to_HeapNumber[curr_ID] = 1; //Обновляю ID_to_HeapNumber
  389. curr_ID++;
  390. heap1.out();
  391. heap2.outHeap();
  392. answers[i] = heap1.get_top().value;
  393. cout << "--------------------ANSWER--------------: "
  394. << heap1.get_top().value << endl;
  395. }
  396. }
  397. }
  398. else
  399. {
  400. L++;
  401. count--;
  402.  
  403. cout << "L,count = " << L << " " << count << endl;
  404.  
  405. cout << "Удаляем a[L-1]" << endl;
  406.  
  407. ID = L - 1;
  408. NumberHeap = ID_to_HeapNumber[ID];
  409.  
  410. cout << "NumberHeap = " << NumberHeap << endl;
  411.  
  412. cout << " Если лежит в куче1 то удаляем там, иначе во 2 " << endl;
  413. //Если лежит в куче1 то удаляем там, иначе во 2
  414. if (NumberHeap == 1)
  415. heap1.delete_vertex(heap_max, ID, ID_to_Index);
  416. else
  417. heap2.delete_vertex(heap_min, ID, ID_to_Index);
  418.  
  419. heap1.out();
  420. heap2.outHeap();
  421.  
  422. if (count >= K)
  423. {
  424. cout << "count >= K; В 1 куче к-1, во 2 не пусто" << endl;
  425.  
  426.  
  427. //Если удалили из 2 кучи, то первая не пострадала и просто принтим
  428. if (NumberHeap == 2)
  429. {
  430. cout << "удалили из 2 кучи, то первая не пострадала и просто принтим "
  431. << endl;
  432. cout << "--------------------ANSWER--------------: "
  433. << heap1.get_top().value << endl;
  434. answers[i] = heap1.get_top().value;
  435. }
  436.  
  437. else
  438. {
  439. cout << "удалили из 1 кучи - надо восстановить баланс";
  440. //Запоминаю вершину кучи2
  441. heap_elements root2 = heap2.get_top();
  442. root2_ID = root2.own_ID;
  443.  
  444. cout << "//Удаляем вершину кучи2, т.к. она будет в куче1" << endl;
  445. //Удаляем вершину кучи2, т.к. теперь она в куче1
  446. heap2.delete_vertex(heap_min, root2_ID, ID_to_Index);
  447. heap1.out();
  448. heap2.outHeap();
  449.  
  450. cout
  451. << "Добавляем вершину кучи2 в кучу1 не меняя индекс чтобы стало K штук"
  452. << endl;
  453. //Добавляем вершину кучи2 в кучу1 не меняя индекс чтобы стало K штук
  454. heap1.add(heap_max, root2_ID, root2.value, ID_to_Index);
  455. ID_to_HeapNumber[root2_ID] = 1; //Обновляю ID_to_HeapNumber
  456.  
  457. cout << "--------------------ANSWER--------------: "
  458. << heap1.get_top().value << endl;
  459. answers[i] = heap1.get_top().value;
  460. heap1.out();
  461. heap2.outHeap();
  462. }
  463. }
  464. else
  465. {
  466. cout << "--------------------ANSWER--------------: -1 " << endl;
  467. answers[i] = -1;
  468. }
  469. }
  470. }
  471.  
  472. cout << endl;
  473. int y;
  474. int true_ans[M];
  475. for (int j = 0; j < M; j++)
  476. {
  477. cout << answers[j] << " ";
  478. out << answers[j] << " ";
  479. }
  480. out << endl;
  481. for (int j = 0; j < M; j++)
  482. {
  483. ifs >> y;
  484. true_ans[j] = y;
  485. out << y << " ";
  486. }
  487. out << endl;
  488. for (int j = 0; j < M; j++)
  489. {
  490. if (true_ans[j] != answers[j])
  491. out << "WA" << j << " ";
  492. else
  493. out << "1 ";
  494. }
  495. out << endl;
  496. }
  497.  
  498.  
  499. return 0;
  500. }
  501.  
Advertisement
Add Comment
Please, Sign In to add comment