vadimk772336

Untitled

Oct 28th, 2021 (edited)
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct heap_elements;
  5.  
  6. struct list_segments
  7. {
  8.     int l;
  9.     int r;
  10.     int status;
  11.     struct list_segments* next;
  12.     struct list_segments* prev;
  13.     int to_heap;
  14. };
  15.  
  16. struct heap_elements
  17. {
  18.     int l;
  19.     int r;
  20.     int position;
  21.     list_segments* to_list;
  22. };
  23.  
  24.  
  25. bool operator<(const heap_elements& a, const heap_elements& b)
  26. {
  27.  
  28.     if ((a.r - a.l < b.r - b.l))
  29.         return true;
  30.     if ((a.r - a.l == b.r - b.l) & (a.l > b.l))
  31.         return true;
  32.  
  33.     return false;
  34. }
  35.  
  36.  
  37. class Heap
  38. {
  39.     static const int SIZE = 100;
  40.     struct heap_elements* h;
  41.     int HeapSize;
  42. public:
  43.     Heap();
  44.     void add(heap_elements);
  45.     void out();
  46.     void siftup();
  47.     void siftdown(int);
  48.     void delete_vertex(int);
  49.     heap_elements* get_head();
  50. };
  51.  
  52. Heap::Heap()
  53. {
  54.     h = new heap_elements[SIZE];
  55.     HeapSize = 0;
  56. }
  57.  
  58. heap_elements* Heap::get_head()
  59. {
  60.     return &h[0];
  61. }
  62.  
  63. void Heap::siftup()
  64. {
  65.     int curr, parent;
  66.     curr = HeapSize - 1;
  67.     parent = (curr - 1) / 2;
  68.     while (parent >= 0 && curr > 0)
  69.     {
  70.         if (h[parent] < h[curr])
  71.         {
  72.             heap_elements buff = h[curr];
  73.             h[curr] = h[parent];
  74.             h[parent] = buff;
  75.  
  76.             h[curr].position = curr;
  77.             h[parent].position = parent;
  78.  
  79.             h[curr].to_list->to_heap = curr;
  80.             h[parent].to_list->to_heap = parent;
  81.         }
  82.         curr = parent;
  83.         parent = (curr - 1) / 2;
  84.     }
  85. }
  86.  
  87. void Heap::add(heap_elements vertex)
  88. {
  89.  
  90.     h[HeapSize] = vertex;
  91.     h[HeapSize].position = HeapSize;
  92.  
  93.     if (h[HeapSize].to_list != NULL)
  94.         h[HeapSize].to_list->to_heap = HeapSize;
  95.  
  96.     HeapSize++;
  97.     siftup();
  98. }
  99.  
  100. void Heap::siftdown(int position)
  101. {
  102.     int curr, parent,max_child;
  103.     curr = position;
  104.  
  105.     int child_l = 2 * curr+ 1;
  106.     int child_r = 2 * curr+ 2;
  107.  
  108.     if (h[child_r] < h[child_l])
  109.         max_child = child_l;
  110.     else
  111.         max_child = child_r;
  112.  
  113.     while (child_l < HeapSize)
  114.     {
  115.         if (child_l == HeapSize - 1)
  116.         {
  117.             max_child = child_l;
  118.         }
  119.         else if (h[child_r] < h[child_l])
  120.             max_child = child_l;
  121.         else
  122.             max_child = child_r;
  123.  
  124.         if (h[curr] < h[max_child])
  125.         {
  126.             heap_elements buff = h[curr];
  127.             h[curr] = h[max_child];
  128.             h[max_child] = buff;
  129.  
  130.             h[curr].position = curr;
  131.             h[max_child].position = max_child;
  132.  
  133.             h[curr].to_list->to_heap = curr;
  134.             h[max_child].to_list->to_heap = max_child;
  135.         }
  136.        
  137.         curr= max_child;
  138.         child_l = 2 * curr+ 1;
  139.         child_r = 2 * curr+ 2;
  140.     }
  141. }
  142.  
  143. void Heap::delete_vertex(int position)
  144. {
  145.     h[position] = h[HeapSize - 1];
  146.     h[HeapSize - 1].l = 0;
  147.     h[HeapSize - 1].r = 0;
  148.     HeapSize--;
  149.     siftdown(position);
  150. }
  151.  
  152. void Heap::out(void)
  153. {
  154.  
  155.     for (int i = 0; i < HeapSize; i++)
  156.     {
  157.         cout << "(" << h[i].l << "," << h[i].r << ") ";
  158.     }
  159.     cout << endl;
  160. }
  161.  
  162. //----------------------------------------------------------------LIST SEGMENTS BEGIN
  163. class LIST_SEGMENTS
  164. {
  165.     struct list_segments *Head;
  166.     struct list_segments* element;
  167.     int Count;
  168.  
  169. public:
  170.     LIST_SEGMENTS();
  171.     void add(int, int, int, int, list_segments*);
  172.     void delete_segment(list_segments*);
  173.     void Print();
  174.     void add_head(int, int, int, int);
  175.     list_segments* get_head();
  176.    
  177. };
  178.  
  179. LIST_SEGMENTS::LIST_SEGMENTS()
  180. {
  181.     Head = NULL;
  182.     Count = 0;
  183. }
  184. void LIST_SEGMENTS::add_head(int l, int r, int status, int to_heap)
  185. {
  186.    
  187.     list_segments* buff = new list_segments;
  188.  
  189.     buff->prev = 0;
  190.     buff->l = l;
  191.     buff->r = r;
  192.     buff->status = status;
  193.     buff->next = Head;
  194.  
  195.     if (Head != NULL)
  196.         Head->prev = buff;
  197.  
  198.     /*
  199.     if (Count == 0)
  200.         Head = buff;  // ----------------------------------------???
  201.     else
  202.         Head = buff;
  203.     */
  204.     Head = buff;
  205.  
  206.     Count++;
  207. }
  208.  
  209.  
  210. void LIST_SEGMENTS::add(int l, int r, int status, int to_heap, list_segments* segment)
  211. {
  212.    
  213.     list_segments* after_segment = new list_segments;
  214.    
  215.     after_segment->l = l;
  216.     after_segment->r = r;
  217.     after_segment->status = status;
  218.     after_segment->next = segment->next;
  219.     after_segment->prev = segment;
  220.     after_segment->to_heap = to_heap;
  221.     if (segment->next != NULL)
  222.         segment->next->prev = after_segment;
  223.     segment->next = after_segment;
  224.     Count++;
  225. }
  226.  
  227. void LIST_SEGMENTS::delete_segment(list_segments* segment)
  228. {
  229.     if (segment->next != NULL)
  230.         segment->next->prev = segment->prev;
  231.     if (segment->prev != NULL)
  232.         segment->prev->next = segment->next;
  233.     else
  234.         Head = segment->next;
  235.     delete segment;
  236. }
  237.  
  238. void LIST_SEGMENTS::Print()
  239. {
  240.    
  241.    
  242.     list_segments* buff = Head;
  243.     while (buff->next != NULL)
  244.     {
  245.         cout << "(" << buff->l << ";" << buff->status << ";"  << buff->r << ")"<< ";";
  246.         buff = buff->next;
  247.     }
  248.  
  249.     cout << "(" << buff->l << ";" << buff->status << ";"  << buff->r << ")"<< ";\n";
  250. }
  251.  
  252. list_segments* LIST_SEGMENTS::get_head()
  253. {
  254.     return Head;
  255. }
  256.  
  257. int main()
  258. {
  259.  
  260.     int N1,N2,N3,N4,N5;
  261.     int M1,M2,M3,M4,M5;
  262.  
  263.     //отказ -1
  264.     //освободили -2
  265.     //нечего освобождать -3
  266.    
  267.     N1 = 6; M1 = 8;
  268.     int req_mas1[] = {2,3,-1,3,3,-5,2,2};
  269.     int true_answer_mas1[] = {1,3,-2,-1,-1,-3,1,-1};
  270.    
  271.     N2 = 1; M2 = 7;
  272.     int req_mas2[] = {2,2,1,1,-4,-1,-3}; //answer = -1 -1 1 -1 none none free
  273.     int true_answer_mas2[] = {-1,-1,1,-1,-3,-3,-2};
  274.    
  275.     N3 = 6; M3 = 6;
  276.     int req_mas3[] = {2,2,2,-1,-3,-2};  //Тест со слиянием answer 1 3 5 free free free
  277.     int true_answer_mas3[] = {1,3,5,-2,-2,-2};
  278.    
  279.     /*
  280.     N4 = 6; M4 = 8;
  281.     int req_mas4[] = {2,3,-1,3,3,-5,2,2};
  282.  
  283.     N5 = 6; M5 = 8;
  284.     int req_mas5[] = {2,3,-1,3,3,-5,2,2};
  285.     */
  286.  
  287.  
  288.     Heap heap;
  289.  
  290.     int N, M, K;
  291.    
  292.     //Записываю данные для теста
  293.     N = N1;
  294.     M = M1;
  295.     int req[M];
  296.     int answer_mas[M];
  297.     int true_answer_mas[M];
  298.     for (int j=0; j < M; ++j) {
  299.         req[j] = req_mas1[j];
  300.         answer_mas[j] = -1;
  301.         true_answer_mas[j] = true_answer_mas1[j];
  302.     }
  303.        
  304.     cout << "N,M = " << N << " " << M << endl;
  305.     //cout << "Enter N: ";
  306.     //cin >> N;
  307.  
  308.     heap_elements vertex;
  309.     vertex.l = 1;
  310.     vertex.r = 6;
  311.     vertex.to_list = NULL;
  312.     vertex.position = 0;
  313.    
  314.     LIST_SEGMENTS List;
  315.     List.add_head(1, N, 1, 0);
  316.    
  317.     vertex.to_list = List.get_head();
  318.     heap.add(vertex);
  319.  
  320.     list_segments segment;
  321.     heap_elements* heap_head;
  322.     //cout << "Enter M: ";
  323.     //cin >> M;
  324.    
  325.     list_segments* requests[M];
  326.     for (int i = 0; i < M; ++i)
  327.     {
  328.        
  329.         K = req[i];
  330.         cout << "\n ЗАПРОС НОМЕР " << i <<  " Enter K: " << K << endl;
  331.         //cin >> K;
  332.  
  333.         if (K > 0)
  334.         {
  335.            
  336.             heap_head = heap.get_head();
  337.             if ((heap_head->r - heap_head->l + 1) >= K)
  338.             {
  339.                 if ((heap_head->r - heap_head->l + 1) == K){
  340.                     heap_head->to_list->status=0;
  341.                     requests[i] = heap_head->to_list;
  342.                     cout << "ANSWER:" << heap_head->l << endl;
  343.                     answer_mas[i] = heap_head->l;
  344.                     heap.delete_vertex(0);
  345.                 }
  346.                 else{
  347.                     //cout << "Можем откусить \n" << endl;
  348.                    
  349.                     int old_l = heap_head->l;
  350.                     heap_head->l = old_l + K;
  351.                     //cout << "Изменившаяся куча после откусывания: " <<heap_head->l<< "\n";
  352.                    
  353.                    
  354.                    
  355.                    
  356.                     heap_head->to_list->l = old_l + K;
  357.                    
  358.                    
  359.                    
  360.                     segment.l = old_l;
  361.                     segment.r = old_l + K - 1;
  362.                     segment.status = 0;
  363.                     segment.to_heap = -1;
  364.                    
  365.                     if (heap_head->to_list->prev != NULL)
  366.                     {
  367.                         List.add(old_l, old_l + K - 1, 0, -1,heap_head->to_list->prev);
  368.                         requests[i] = heap_head->to_list->prev;
  369.                     }
  370.                     else
  371.                     {
  372.                         List.add_head(old_l, old_l + K - 1, 0, -1);
  373.                         requests[i] = List.get_head();
  374.                     }
  375.    
  376.    
  377.                    
  378.                     heap.siftdown(0);
  379.    
  380.                     cout << "ANSWER: " << old_l << endl;
  381.                     answer_mas[i] = old_l;
  382.                 }
  383.             }
  384.             else
  385.             {
  386.                 requests[i] = NULL;
  387.                 cout << "ANSWER:" << -1 << endl;
  388.                 answer_mas[i] = -1;
  389.             }
  390.         }
  391.  
  392.         else
  393.         {
  394.             int q = abs(K) -1;
  395.             if (requests[q] != NULL)
  396.             {
  397.  
  398.                 requests[q]->status = 1;
  399.  
  400.                 if (requests[q]->prev != NULL){
  401.                     if (requests[q]->prev->status == 1 & requests[q]->prev->r == requests[q]->l - 1)
  402.                     {
  403.                         requests[q]->l = requests[q]->prev->l;
  404.                         heap.delete_vertex(requests[q]->prev->to_heap);
  405.                         List.delete_segment(requests[q]->prev);
  406.                     }
  407.                 }
  408.                 if (requests[q]->next != NULL){
  409.                     if (requests[q]->next->status == 1 &  requests[q]->next->l - 1 == requests[q]->r)
  410.                     {
  411.                         requests[q]->r = requests[q]->next->r;
  412.    
  413.                         heap.delete_vertex(requests[q]->next->to_heap);
  414.                         List.delete_segment(requests[q]->next);
  415.                     }
  416.                 }
  417.  
  418.                     vertex.l = requests[q]->l;
  419.                     vertex.r = requests[q]->r;
  420.                     vertex.to_list = requests[q];
  421.                     vertex.position = 0;  
  422.                     heap.add(vertex);
  423.                    
  424.                 requests[i] = requests[q];
  425.                 requests[q] = NULL;
  426.                 cout << "Освободили"  << endl;
  427.                 answer_mas[i] = -2;
  428.             }
  429.  
  430.             else
  431.             {
  432.                 cout << "Не было занято, поэтому не освободили"  << endl;
  433.                 requests[i] = NULL;
  434.                 answer_mas[i] = -3;
  435.             }
  436.            
  437.            
  438.         }
  439.  
  440.     cout << "Куча после запроса: ";
  441.     heap.out();
  442.     cout << "List после запроса: ";
  443.     List.Print();
  444.     cout << "requests после запроса i: ";
  445.     for (int f=0; f < i+1; ++f)
  446.         if (requests[f] == NULL)
  447.             cout << " null; ";
  448.         else
  449.             cout << "(" << requests[f]->l << ";" << requests[f]->status << ";"  << requests[f]->r << ")"<< "; ";
  450.     cout << "\n---------------";
  451.     }
  452.    
  453.     cout << "\n answer_mas      = ";
  454.     for (int i=0; i < M; ++i) {
  455.         cout << answer_mas[i] << " ";
  456.     }
  457.     cout << endl;
  458.    
  459.     cout << "true answer_mas  = ";
  460.     for (int i=0; i < M; ++i) {
  461.         cout << true_answer_mas[i] << " ";
  462.     }
  463.  
  464.     return 0;
  465. }
  466.  
Add Comment
Please, Sign In to add comment