vadimk772336

Принята

Nov 3rd, 2021
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  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, int pos, std::vector<int>& ID_to_Index);
  32.     void siftdown(int sort_type, int pos, std::vector<int>& ID_to_Index);
  33.     void my_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. };
  37.  
  38. Heap::Heap()
  39. {
  40.     std::vector<struct heap_elements> h;
  41.     heap_size = 0;
  42. }
  43.  
  44. heap_elements Heap::get_top()
  45. {
  46.     return h[0];
  47. }
  48.  
  49. bool Heap::isempty()
  50. {
  51.     if (heap_size == 0)
  52.         return true;
  53.     return false;
  54. }
  55.  
  56. void Heap::my_swap(int i, int j, std::vector<int>& ID_to_Index)
  57. {
  58.     heap_elements buff;
  59.     buff = h[i];
  60.     h[i] = h[j];
  61.     h[j] = buff;
  62.  
  63.     ID_to_Index[h[i].own_ID] = i; // На местах в этом массиве лежат айди этой кучи все ок
  64.     ID_to_Index[h[j].own_ID] = j;
  65. }
  66.  
  67. void Heap::siftup(int sort_type, int pos, std::vector<int>& ID_to_Index)
  68. {
  69.     int curr = pos;
  70.     int parent = (curr - 1) / 2;
  71.     while (curr > 0 & parent >= 0)
  72.     {
  73.         if (sort_type == heap_max & h[parent] < h[curr])
  74.             my_swap(parent, curr, ID_to_Index);
  75.  
  76.         if (sort_type == heap_min & h[curr] < h[parent])
  77.             my_swap(parent, curr, ID_to_Index);
  78.  
  79.         curr = parent;
  80.         parent = (curr - 1) / 2;
  81.     }
  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, min_child;
  89.  
  90.     int curr = pos;
  91.     int child_l = 2 * curr + 1;
  92.     int child_r = 2 * curr + 2;
  93.  
  94.     while (child_l < heap_size)
  95.     {
  96.         if (sort_type == heap_max)
  97.         {
  98.             if (child_l == heap_size - 1)
  99.                 max_child = child_l;
  100.             else if (h[child_r] < h[child_l])
  101.                 max_child = child_l;
  102.             else
  103.                 max_child = child_r;
  104.  
  105.             if (h[curr] < h[max_child])
  106.                 my_swap(curr, max_child, ID_to_Index);
  107.  
  108.             curr = max_child;
  109.             child_l = 2 * curr + 1;
  110.             child_r = 2 * curr + 2;
  111.         }
  112.  
  113.         if (sort_type == heap_min)
  114.         {
  115.  
  116.             if (child_l == heap_size - 1)
  117.                 min_child = child_l;
  118.             else if (h[child_r] < h[child_l])
  119.                 min_child = child_r;
  120.             else
  121.                 min_child = child_l;
  122.  
  123.  
  124.             if (h[min_child] < h[curr])
  125.             {
  126.                 my_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. void Heap::add(int sort_type, int ID, int value, std::vector<int>& ID_to_Index)
  137. {
  138.     heap_elements vertex;
  139.     vertex.value = value;
  140.     vertex.own_ID = ID;
  141.  
  142.     h.push_back(vertex);
  143.  
  144.  
  145.     if (ID < ID_to_Index.size())
  146.     {
  147.         ID_to_Index[ID] = heap_size;
  148.     }
  149.     else
  150.     {
  151.         ID_to_Index.push_back(heap_size); // должно быть общее у двух куч
  152.     }
  153.     heap_size++;
  154.     siftup(sort_type, heap_size - 1, ID_to_Index);
  155. }
  156.  
  157. void Heap::delete_vertex(int sort_type, int ID, std::vector<int>& ID_to_Index)
  158. {
  159.  
  160.     int pos = ID_to_Index[ID];
  161.  
  162.  
  163.     my_swap(pos, heap_size - 1, ID_to_Index); // Можно не делать свап
  164.     h.pop_back();
  165.     heap_size--;
  166.     ID_to_Index[ID] = -1; // мб убрать
  167.  
  168.     int parent = (pos - 1) / 2;
  169.  
  170.     if (sort_type == heap_min)
  171.     {
  172.         if (h[pos] < h[parent])
  173.             siftup(sort_type, pos, ID_to_Index);
  174.         else
  175.             siftdown(sort_type, pos, ID_to_Index);
  176.     }
  177.  
  178.     if (sort_type == heap_max)
  179.     {
  180.         if (h[parent] < h[pos])
  181.             siftup(sort_type, pos, ID_to_Index);
  182.         else
  183.             siftdown(sort_type, pos, ID_to_Index);
  184.     }
  185. }
  186.  
  187.  
  188.  
  189. int main()
  190. {
  191.  
  192.  
  193.     Heap heap_one;
  194.     Heap heap_two;
  195.  
  196.     std::vector<int> ID_to_Index;
  197.     int NumberHeap;
  198.     int curr_ID = 0;
  199.     char c;
  200.  
  201.     int N, M, K, ID, ID_root_one, root_two_ID, ID_root_two, count = 1;
  202.     std::cin >> N;
  203.     std::cin >> M;
  204.     std::cin >> K;
  205.     int R = 0, L = 0;
  206.     int a[N];
  207.     for (int j = 0; j < N; ++j)
  208.         std::cin >> a[j];
  209.  
  210.     int ID_to_HeapNumber[N];
  211.  
  212.     heap_one.add(heap_max, curr_ID, a[0], ID_to_Index);
  213.     ID_to_HeapNumber[curr_ID] = 1;
  214.     curr_ID++;
  215.  
  216.  
  217.     for (int i = 0; i < M; ++i)
  218.     {
  219.  
  220.         std::cin >> c;
  221.  
  222.         if (c == 'R')
  223.         {
  224.             count++;
  225.             R++;
  226.  
  227.  
  228.             if (count < K)
  229.             {
  230.  
  231.  
  232.                 heap_one.add(heap_max, curr_ID, a[R], ID_to_Index);
  233.                 ID_to_HeapNumber[curr_ID] = 1;
  234.                 curr_ID++;
  235.  
  236.                 std::cout << -1 << std::endl;
  237.             }
  238.  
  239.             if (count == K)
  240.             {
  241.  
  242.  
  243.                 heap_one.add(heap_max, curr_ID, a[R], ID_to_Index);
  244.                 ID_to_HeapNumber[curr_ID] = 1;
  245.                 curr_ID++;
  246.  
  247.                 std::cout << heap_one.get_top().value << std::endl;
  248.             }
  249.  
  250.             if (count > K)
  251.             {
  252.  
  253.  
  254.                 if (a[R] > heap_one.get_top().value)
  255.                 {
  256.  
  257.                     heap_two.add(heap_min, curr_ID, a[R], ID_to_Index);
  258.                     ID_to_HeapNumber[curr_ID] = 2;
  259.                     curr_ID++;
  260.                     std::cout << heap_one.get_top().value << std::endl;
  261.                 }
  262.  
  263.                 if (a[R] <= heap_one.get_top().value)
  264.  
  265.                 {
  266.  
  267.  
  268.                     heap_elements root_one, root_two;
  269.  
  270.                     // Запоминаю корень 1 кучи
  271.                     root_one = heap_one.get_top();
  272.                     ID_root_one = root_one.own_ID;
  273.  
  274.                     //  удаляю корень 1 кучи из 1 кучи
  275.                     heap_one.delete_vertex(heap_max, ID_root_one, ID_to_Index);
  276.  
  277.  
  278.                     //  Добавляю корень 1 кучи во 2 кучу не меняя ID
  279.                     heap_two.add(heap_min, ID_root_one, root_one.value, ID_to_Index);
  280.                     ID_to_HeapNumber[ID_root_one] = 2;
  281.  
  282.  
  283.                     // Добавляю в 1 кучу a[R]  меняя айди
  284.                     heap_one.add(heap_max, curr_ID, a[R], ID_to_Index);
  285.                     ID_to_HeapNumber[curr_ID] = 1;
  286.                     curr_ID++;
  287.  
  288.  
  289.                     std::cout << heap_one.get_top().value << std::endl;
  290.                 }
  291.             }
  292.         }
  293.         else
  294.         {
  295.             L++;
  296.             count--;
  297.  
  298.  
  299.             ID = L - 1;
  300.             NumberHeap = ID_to_HeapNumber[ID];
  301.  
  302.  
  303.             // Если лежит в куче1 то удаляем там, иначе во 2
  304.             if (NumberHeap == 1)
  305.                 heap_one.delete_vertex(heap_max, ID, ID_to_Index);
  306.             else
  307.                 heap_two.delete_vertex(heap_min, ID, ID_to_Index);
  308.  
  309.  
  310.             if (count >= K)
  311.             {
  312.  
  313.  
  314.                 // Если удалили из 2 кучи, то первая не пострадала и просто принтим
  315.                 if (NumberHeap == 2)
  316.                 {
  317.                     std::cout << heap_one.get_top().value << std::endl;
  318.                 }
  319.  
  320.                 else
  321.                 {
  322.  
  323.                     // Запоминаю вершину кучи2
  324.                     heap_elements root_two = heap_two.get_top();
  325.                     root_two_ID = root_two.own_ID;
  326.  
  327.  
  328.                     // Удаляем вершину кучи2, т.к. теперь она в куче1
  329.                     heap_two.delete_vertex(heap_min, root_two_ID, ID_to_Index);
  330.  
  331.  
  332.                     // Добавляем вершину кучи2 в кучу1  не меняя индекс чтобы стало K штук
  333.                     heap_one.add(heap_max, root_two_ID, root_two.value, ID_to_Index);
  334.                     ID_to_HeapNumber[root_two_ID] = 1;
  335.  
  336.                     std::cout << heap_one.get_top().value << std::endl;
  337.                 }
  338.             }
  339.             else
  340.             {
  341.                 std::cout << -1 << std::endl;
  342.             }
  343.         }
  344.     }
  345.  
  346.  
  347.     return 0;
  348. }
  349.  
Advertisement
Add Comment
Please, Sign In to add comment