vadimk772336

Untitled

Oct 29th, 2021
1,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.03 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.     h[pos] = h[heap_size - 1];
  157.     h.pop_back();
  158.     heap_size--;
  159.     siftdown(pos);
  160. }
  161.  
  162. void Heap::out(void)
  163. {
  164.  
  165.     for (int i = 0; i < heap_size; i++)
  166.     {
  167.         cout << "(" << h[i].l << "," << h[i].r << ") ";
  168.     }
  169.     cout << endl;
  170. }
  171.  
  172. class LIST_SEGMENTS
  173. {
  174.     struct list_segments* Head;
  175.     int count;
  176.  
  177. public:
  178.     LIST_SEGMENTS();
  179.     list_segments* get_head();
  180.     void add(int, int, int, int, list_segments*);
  181.     void delete_segment(list_segments* segment);
  182.     void add_head(int, int, int, int);
  183.     void Print();
  184. };
  185.  
  186. LIST_SEGMENTS::LIST_SEGMENTS()
  187. {
  188.     Head = NULL;
  189.     count = 0;
  190. }
  191.  
  192. void LIST_SEGMENTS::add_head(int l, int r, int status, int to_heap)
  193. {
  194.     list_segments* buff = new list_segments;
  195.     buff->prev = 0;
  196.     buff->l = l;
  197.     buff->r = r;
  198.     buff->status = status;
  199.     buff->next = Head;
  200.  
  201.     if (Head != NULL)
  202.         Head->prev = buff;
  203.  
  204.     Head = buff;
  205.     count++;
  206. }
  207.  
  208. void LIST_SEGMENTS::add(int l, int r, int status, int to_heap, list_segments* segment)
  209. {
  210.     list_segments* after_segment = new list_segments;
  211.     after_segment->l = l;
  212.     after_segment->r = r;
  213.     after_segment->status = status;
  214.     after_segment->next = segment->next;
  215.     after_segment->prev = segment;
  216.     after_segment->to_heap = to_heap;
  217.  
  218.     if (segment->next != NULL)
  219.         segment->next->prev = after_segment;
  220.  
  221.     segment->next = after_segment;
  222.     count++;
  223. }
  224.  
  225. void LIST_SEGMENTS::delete_segment(list_segments* segment)
  226. {
  227.     if (segment->next != NULL)
  228.         segment->next->prev = segment->prev;
  229.     if (segment->prev != NULL)
  230.         segment->prev->next = segment->next;
  231.     else
  232.         Head = segment->next;
  233.     delete segment;
  234. }
  235.  
  236. list_segments* LIST_SEGMENTS::get_head()
  237. {
  238.     return Head;
  239. }
  240.  
  241. void LIST_SEGMENTS::Print()
  242. {
  243.    
  244.    
  245.     list_segments* buff = Head;
  246.     while (buff->next != NULL)
  247.     {
  248.         cout << "(" << buff->l << ";" << buff->status << ";"  << buff->r << ")"<< ";";
  249.         buff = buff->next;
  250.     }
  251.  
  252.     cout << "(" << buff->l << ";" << buff->status << ";"  << buff->r << ")"<< ";\n";
  253. }
  254.  
  255. void find_memory(Heap* heap, LIST_SEGMENTS* List, int K, int query_number,
  256.     std::vector<int>& answers, list_segments** requests)
  257. {
  258.     int old_l;
  259.     heap_elements* heap_head;
  260.  
  261.     heap_head = heap->get_head();
  262.     if (!heap->isempty() & (heap_head->r - heap_head->l + 1) >= K)
  263.     {
  264.         if ((heap_head->r - heap_head->l + 1) == K)
  265.         {
  266.             heap_head->to_list->status = 0;
  267.             requests[query_number] = heap_head->to_list;
  268.             answers.push_back(heap_head->l);
  269.             heap->delete_vertex(0);
  270.         }
  271.         else
  272.         {
  273.             old_l = heap_head->l;
  274.             heap_head->l = old_l + K;
  275.             heap_head->to_list->l = old_l + K;
  276.  
  277.             if (heap_head->to_list->prev != NULL)
  278.             {
  279.                 List->add(old_l, old_l + K - 1, 0, -1, heap_head->to_list->prev);
  280.                 requests[query_number] = heap_head->to_list->prev;
  281.             }
  282.             else
  283.             {
  284.                 List->add_head(old_l, old_l + K - 1, 0, -1);
  285.                 requests[query_number] = List->get_head();
  286.             }
  287.  
  288.             heap->siftdown(0);
  289.             answers.push_back(old_l);
  290.         }
  291.     }
  292.     else
  293.     {
  294.         requests[query_number] = NULL;
  295.         answers.push_back(-1);
  296.     }
  297. }
  298.  
  299. void free_up_memory(Heap* heap, LIST_SEGMENTS* List, int q, int query_number,
  300.     std::vector<int>& answers, list_segments** requests)
  301. {
  302.     if (q==246) cout << "246; in" << endl;
  303.     if (requests[q] != NULL)
  304.     {
  305.         if (q==246) cout << "1" << endl;
  306.         requests[q]->status = 1;
  307.         if (requests[q]->prev != NULL)
  308.         {
  309.             if (q==246) cout << "2" << endl;
  310.             if (requests[q]->prev->status == 1 & requests[q]->prev->r == requests[q]->l - 1)
  311.             {
  312.                 requests[q]->l = requests[q]->prev->l;
  313.                 heap->delete_vertex(requests[q]->prev->to_heap);
  314.                 List->delete_segment(requests[q]->prev);
  315.             }
  316.         }
  317.         if (requests[q]->next != NULL)
  318.         {
  319.             if (q==246) cout << "3" << endl;
  320.             if (requests[q]->next->status == 1 & requests[q]->next->l - 1 == requests[q]->r)
  321.             {
  322.                 requests[q]->r = requests[q]->next->r;
  323.                 heap->delete_vertex(requests[q]->next->to_heap);
  324.                 List->delete_segment(requests[q]->next);
  325.             }
  326.         }
  327.         if (q==246) cout << "4" << endl;
  328.         heap->add(requests[q]->l, requests[q]->r, requests[q]);
  329.         requests[query_number] = requests[q];
  330.         requests[q] = NULL;
  331.     }
  332.     else
  333.     {  
  334.         if (q==246) cout << "5" << endl;
  335.         requests[query_number] = NULL;
  336.     }
  337. }
  338.  
  339.  
  340.  
  341.  
  342. int main()
  343. {
  344.    
  345.     /*
  346.     -9
  347.     6 8 2 3 -1 3 3 -5 2 2 //1 3 -1 -1 1 -1
  348.     1 7 2 2 1 1 -4 -1 -3 //-1 -1 1 -1
  349.     6 9 2 2 2 -1 -3 -2  2 2 2 // 1 3 5 1 3 5
  350.     6 2 3 -1 //1,-1,1,3
  351.     6 7 6 6-2-1 2 4 -5
  352.     10 14 8 2 -2 5 -4 -4 -1 4 4 2 -8 -10 -9 10 //1 9 -1 1 5 9 1
  353.     1 1 1  // 1
  354.     1 2 1 2   //1 -1
  355.     1 9 2 -1 1 1 -1 2 1 -3 1 //-1 1 -1 -1 -1 1
  356.     8 10 2 2 -1 1 3 -2 3 -4 3 -9
  357.  
  358.     */
  359.     int N, M, K, q, old_l;
  360.  
  361.     int count_tests;
  362.     ifstream ifs("tests.txt");
  363.     ifs >> count_tests;
  364.     ofstream out("results.txt");
  365.  
  366.     for (int i = 0; i < count_tests; ++i)
  367.     {
  368.         ifs >> N;
  369.         ifs >> M;
  370.         Heap heap;
  371.         LIST_SEGMENTS List;
  372.         heap_elements* heap_head;
  373.         std::vector<int> answers;
  374.    
  375.         List.add_head(1, N, 1, 0);
  376.         heap.add(1, N, List.get_head());
  377.         list_segments* requests[M];
  378.         for (int query_number = 0; query_number < M; ++query_number)
  379.         {
  380.             ifs >> K;
  381.             cout << "\n ЗАПРОС НОМЕР " << query_number+1 <<  " Enter K: " << K << endl;
  382.             if (K > 0)
  383.             {
  384.                 find_memory(&heap, &List, K, query_number, answers, requests);
  385.             }
  386.             else
  387.             {
  388.                 q = -K - 1;
  389.                 free_up_memory(&heap, &List, q, query_number, answers, requests);
  390.             }
  391.            
  392.             if (query_number > 275) {
  393.                 cout << "Куча после запроса: ";
  394.                 heap.out();
  395.                 cout << "List после запроса: ";
  396.                 List.Print();
  397.                 cout << "requests после запроса " << query_number << ": ";
  398.    
  399.                 for (int f=240; f < query_number+1; ++f) {
  400.                     if (requests[f] == NULL)
  401.                         cout << " null; ";
  402.                     else
  403.                         cout << "(" << requests[f]->l << ";" << requests[f]->status << ";"  << requests[f]->r << ")"<< "; ";
  404.                 }
  405.                 cout << "\n---------------";
  406.             }
  407.            
  408.            
  409.         }
  410.     for (int i = 0; i < answers.size(); ++i){
  411.         out << answers[i] << ' ';
  412.         cout << answers[i] << " ";
  413.     }
  414.     cout << endl;
  415.     out << '\n';
  416.     }
  417.  
  418.     return 0;
  419. }
  420.  
Advertisement
Add Comment
Please, Sign In to add comment