vadimk772336

код ревью итог

Nov 3rd, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct list_section
  5. {
  6.     int l;
  7.     int r;
  8.     bool isallocated;
  9.     struct list_section* next;
  10.     struct list_section* prev;
  11.     int to_heap;
  12. };
  13.  
  14. struct heap_element
  15. {
  16.     int l;
  17.     int r;
  18.     int pos;
  19.     list_section* to_list;
  20. };
  21.  
  22. bool operator<(const heap_element& a, const heap_element& b)
  23. {
  24.  
  25.     if ((a.r - a.l < b.r - b.l))
  26.         return true;
  27.     if ((a.r - a.l == b.r - b.l) & (a.l > b.l))
  28.         return true;
  29.  
  30.     return false;
  31. }
  32.  
  33. class Heap
  34. {
  35.     std::vector<struct heap_element> heap_elements;
  36.     int heap_size;
  37.  
  38. public:
  39.     Heap();
  40.     void siftup(int pos);
  41.     void siftdown(int pos);
  42.     void my_swap(int i, int j);
  43.     void add(int, int, list_section*);
  44.     void del(int pos);
  45.     bool isempty();
  46.     heap_element* get_top();
  47. };
  48.  
  49. Heap::Heap()
  50. {
  51.     std::vector<struct heap_element> heap_elements;
  52.     heap_size = 0;
  53. }
  54.  
  55. heap_element* Heap::get_top()
  56. {
  57.     return &heap_elements[0];
  58. }
  59.  
  60. bool Heap::isempty()
  61. {
  62.     if (heap_size == 0)
  63.         return true;
  64.     return false;
  65. }
  66.  
  67. void Heap::my_swap(int i, int j)
  68. {
  69.     heap_element buffer;
  70.  
  71.     buffer = heap_elements[i];
  72.     heap_elements[i] = heap_elements[j];
  73.     heap_elements[j] = buffer;
  74.  
  75.     heap_elements[i].pos = i;
  76.     heap_elements[j].pos = j;
  77.  
  78.     heap_elements[i].to_list->to_heap = i;
  79.     heap_elements[j].to_list->to_heap = j;
  80. }
  81.  
  82. void Heap::siftup(int pos)
  83. {
  84.     int curr = pos;
  85.     int parent = (curr - 1) / 2;
  86.  
  87.     while (parent >= 0 & curr > 0)
  88.     {
  89.         if (heap_elements[parent] < heap_elements[curr])
  90.             my_swap(parent, curr);
  91.  
  92.         curr = parent;
  93.         parent = (curr - 1) / 2;
  94.     }
  95. }
  96.  
  97. void Heap::siftdown(int pos)
  98. {
  99.     int parent, max_child;
  100.  
  101.     int curr = pos;
  102.     int child_l = 2 * curr + 1;
  103.     int child_r = 2 * curr + 2;
  104.  
  105.     while (child_l < heap_size)
  106.     {
  107.         max_child = child_l;
  108.         if (child_r < heap_size & heap_elements[child_l] < heap_elements[child_r])
  109.             max_child = child_r;
  110.  
  111.         if (heap_elements[curr] < heap_elements[max_child])
  112.             my_swap(curr, max_child);
  113.  
  114.         curr = max_child;
  115.         child_l = 2 * curr + 1;
  116.         child_r = 2 * curr + 2;
  117.     }
  118. }
  119.  
  120. void Heap::add(int l, int r, list_section* to_list)
  121. {
  122.  
  123.     this->heap_size++;
  124.  
  125.     heap_element vertex;
  126.     vertex.l = l;
  127.     vertex.r = r;
  128.     vertex.to_list = to_list;
  129.     vertex.pos = heap_size - 1;
  130.     heap_elements.push_back(vertex);
  131.  
  132.     to_list->to_heap = heap_size - 1;
  133.     siftup(heap_size - 1);
  134. }
  135.  
  136. void Heap::del(int pos)
  137. {
  138.  
  139.     heap_elements[pos] = heap_elements[heap_size - 1];
  140.     heap_elements[pos].pos = pos;
  141.     heap_elements[pos].to_list->to_heap = pos;
  142.  
  143.     heap_elements.pop_back();
  144.     this->heap_size--;
  145.  
  146.     int parent = (pos - 1) / 2;
  147.     if (heap_elements[parent] < heap_elements[pos])
  148.         siftup(pos);
  149.     else
  150.         siftdown(pos);
  151. }
  152.  
  153. class List
  154. {
  155.     struct list_section* head;
  156.     int list_size;
  157.  
  158. public:
  159.     List();
  160.     list_section* get_head();
  161.     void add_head(int l, int r, bool isallocated, int to_heap);
  162.     void add(int l, int r, bool isallocated, int to_heap, list_section* section);
  163.     void del(list_section* section);
  164. };
  165.  
  166. List::List()
  167. {
  168.     head = NULL;
  169.     list_size = 0;
  170. }
  171.  
  172. list_section* List::get_head()
  173. {
  174.     return head;
  175. }
  176.  
  177. void List::add_head(int l, int r, bool isallocated, int to_heap)
  178. {
  179.     list_section* buffer = new list_section;
  180.     buffer->prev = 0;
  181.     buffer->l = l;
  182.     buffer->r = r;
  183.     buffer->isallocated = isallocated;
  184.     buffer->next = head;
  185.  
  186.     if (head != NULL)
  187.         head->prev = buffer;
  188.  
  189.     head = buffer;
  190.     this->list_size++;
  191. }
  192.  
  193. void List::add(int l, int r, bool isallocated, int to_heap, list_section* section)
  194. {
  195.     list_section* after_section = new list_section;
  196.     after_section->l = l;
  197.     after_section->r = r;
  198.     after_section->isallocated = isallocated;
  199.     after_section->next = section->next;
  200.     after_section->prev = section;
  201.     after_section->to_heap = to_heap;
  202.  
  203.     if (section->next != NULL)
  204.         section->next->prev = after_section;
  205.  
  206.     section->next = after_section;
  207.     this->list_size++;
  208. }
  209.  
  210. void List::del(list_section* section)
  211. {
  212.     if (section->next != NULL)
  213.         section->next->prev = section->prev;
  214.  
  215.     if (section->prev != NULL)
  216.         section->prev->next = section->next;
  217.     else
  218.         head = section->next;
  219.  
  220.     delete section;
  221. }
  222.  
  223. int allocate_memory(Heap* heap, List* List, int query, int i, list_section** requests)
  224. {
  225.     int L, answer;
  226.     heap_element* heap_top;
  227.  
  228.     heap_top = heap->get_top();
  229.     if (!heap->isempty() & (heap_top->r - heap_top->l + 1) >= query)
  230.     {
  231.         if ((heap_top->r - heap_top->l + 1) == query)
  232.         {
  233.             heap_top->to_list->isallocated = true;
  234.             requests[i] = heap_top->to_list;
  235.             answer = heap_top->l;
  236.             heap->del(0);
  237.         }
  238.         else
  239.         {
  240.             L = heap_top->l;
  241.             heap_top->l = L + query;
  242.             heap_top->to_list->l = L + query;
  243.  
  244.             int r = L + query - 1;
  245.             bool isallocated = true;
  246.             int to_heap = -1;
  247.  
  248.             if (heap_top->to_list->prev != NULL)
  249.             {
  250.                 List->add(L, r, isallocated, to_heap, heap_top->to_list->prev);
  251.                 requests[i] = heap_top->to_list->prev;
  252.             }
  253.             else
  254.             {
  255.                 List->add_head(L, r, isallocated, to_heap);
  256.                 requests[i] = List->get_head();
  257.             }
  258.  
  259.             heap->siftdown(0);
  260.             answer = L;
  261.         }
  262.     }
  263.     else
  264.     {
  265.         requests[i] = NULL;
  266.         answer = -1;
  267.     }
  268.  
  269.     return answer;
  270. }
  271.  
  272. void free_memory(Heap* heap, List* List, int query_num, int i, list_section** requests)
  273. {
  274.  
  275.     if (requests[query_num] != NULL)
  276.     {
  277.         requests[query_num]->isallocated = false;
  278.         if (requests[query_num]->prev != NULL)
  279.         {
  280.             if (requests[query_num]->prev->isallocated == false
  281.                 & requests[query_num]->prev->r == requests[query_num]->l - 1)
  282.             {
  283.                 requests[query_num]->l = requests[query_num]->prev->l;
  284.                 heap->del(requests[query_num]->prev->to_heap);
  285.                 List->del(requests[query_num]->prev);
  286.             }
  287.         }
  288.         if (requests[query_num]->next != NULL)
  289.         {
  290.             if (requests[query_num]->next->isallocated == false
  291.                 & requests[query_num]->next->l - 1 == requests[query_num]->r)
  292.             {
  293.                 requests[query_num]->r = requests[query_num]->next->r;
  294.                 heap->del(requests[query_num]->next->to_heap);
  295.                 List->del(requests[query_num]->next);
  296.             }
  297.         }
  298.  
  299.         heap->add(requests[query_num]->l, requests[query_num]->r, requests[query_num]);
  300.         requests[query_num] = NULL;
  301.     }
  302.  
  303.     requests[i] = NULL;
  304. }
  305.  
  306.  
  307. int main()
  308. {
  309.  
  310.     int N, M, query;
  311.  
  312.     std::cin >> N;
  313.     std::cin >> M;
  314.  
  315.     Heap heap;
  316.     List List;
  317.     list_section* requests[M];
  318.  
  319.     List.add_head(1, N, false, 0);
  320.     heap.add(1, N, List.get_head());
  321.  
  322.     for (int i = 0; i < M; ++i)
  323.     {
  324.         std::cin >> query;
  325.         if (query > 0)
  326.             std::cout << allocate_memory(&heap, &List, query, i, requests) << std::endl;
  327.         else
  328.             free_memory(&heap, &List, -query - 1, i, requests);
  329.     }
  330.  
  331.     return 0;
  332. }
  333.  
Add Comment
Please, Sign In to add comment