vadimk772336

Untitled

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