Advertisement
Derga

Untitled

Mar 2nd, 2024
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <queue>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct SegLengthPos {
  9.     int length;
  10.     int beg_pos;
  11.     int operation_idx;
  12. };
  13.  
  14. bool operator< (const SegLengthPos& lhs, const SegLengthPos& rhs) {
  15.     if (lhs.length != rhs.length) return lhs.length > rhs.length;
  16.     return lhs.beg_pos < rhs.beg_pos;
  17. }
  18.  
  19. int main() {
  20.     list<SegLengthPos> l;
  21.    
  22.     auto comparator = [](const list<SegLengthPos>::iterator& lhs, const list<SegLengthPos>::iterator& rhs) {
  23.         return *lhs < *rhs;
  24.     };
  25.  
  26.     priority_queue <list<SegLengthPos>::iterator,
  27.         vector<list<SegLengthPos>::iterator>, decltype(comparator)> pq(comparator);
  28.  
  29.     int memory_cells_count, requests_count;
  30.     cin >> memory_cells_count >> requests_count;
  31.  
  32.     vector<list<SegLengthPos>::iterator> history(1 + requests_count);
  33.  
  34.     l.push_back({ memory_cells_count, 1 });
  35.     pq.push(l.begin());
  36.    
  37.     for (int i = 1; i <= requests_count; ++i) {
  38.         int operation;
  39.         cin >> operation;
  40.  
  41.         if (operation > 0) {
  42.             if (pq.top()->length < operation) {
  43.                 history[i] = l.end();
  44.                 cout << -1;
  45.             } else {
  46.                 auto seg_data = *pq.top();
  47.                 auto it = l.erase(pq.top());
  48.                 pq.pop();
  49.                 if (it != l.end()) prev(it);
  50.                 auto it1 = l.insert(it, { seg_data.length - operation, seg_data.beg_pos + operation, i});
  51.                 pq.push(it1);
  52.  
  53.                 history[i] = it1;
  54.                 cout << seg_data.beg_pos << '\n';
  55.             }
  56.             continue;
  57.         }
  58.  
  59.         if (history[operation] == l.end()) continue;
  60.  
  61.         auto it = history[operation];
  62.         auto [len, pos, op_odx] = *history[operation];
  63.         history[operation] = l.end();
  64.  
  65.         auto l_it = it;
  66.         auto r_it = it;
  67.         if (l_it != l.begin()) prev(l_it);
  68.         if (r_it != l.end()) next(r_it);
  69.  
  70.         if (r_it != l.end() && it->beg_pos + it->length == r_it->beg_pos) {
  71.             it->length += r_it->length;
  72.             history[r_it->operation_idx] = l.end();
  73.             l.erase(r_it);
  74.         }
  75.         if (l_it != it && l_it->beg_pos + l_it->length == it->beg_pos) {
  76.             l_it->length += it->length;
  77.             history[it->operation_idx] = l.end();
  78.             l.erase(it);
  79.         }
  80.     }
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement