Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<list>
  4.  
  5. struct Block;
  6.  
  7. std::list<Block *> List_;
  8.  
  9. struct Block {
  10.     int left, right;
  11.     bool type;
  12.     std::list<Block *>::iterator listPos_it;
  13.     int heapPos;
  14.     Block(int lo, int ro, bool typeo) {
  15.         left = lo;
  16.         right = ro;
  17.         type = typeo;
  18.         listPos_it = List_.end();
  19.         heapPos = -1;
  20.     }
  21.     friend bool operator< (Block a, Block b) {
  22.         if (a.right - a.left < b.right - b.left) {
  23.             return true;
  24.         }
  25.         if (a.right - a.left > b.right - b.left) {
  26.             return false;
  27.         }
  28.         return a.right > b.right;
  29.     }
  30.     int length() {
  31.         return this -> right - this -> left;
  32.     }
  33. };
  34.  
  35. struct Heap {
  36.     static const int CAPACITY = 1000000;
  37.     Block * heap[CAPACITY];
  38.     int size;
  39.  
  40.     Heap() {
  41.         size = 0;
  42.     }
  43.     void shift_down(int it) {
  44.         int max = it;
  45.         if (2 * it + 1 < size && *heap[max] <  *heap[2 * it + 1]) {
  46.             max = 2 * it + 1;
  47.         }
  48.         if (2 * it + 2 < size && *heap[max] < *heap[2 * it + 2]) {
  49.             max = 2 * it + 2;
  50.         }
  51.         if (max != it) {
  52.             std::swap(heap[it], heap[max]);
  53.             std::swap(heap[it] -> heapPos, heap[max] -> heapPos);
  54.             shift_down(max);
  55.         }
  56.     }
  57.     void shift_up(int it) {
  58.         if (it != 0 && *heap[(it - 1) / 2] < *heap[it]) {
  59.             std::swap(heap[it], heap[(it - 1) / 2]);
  60.             std::swap(heap[it] -> heapPos, heap[(it - 1) / 2] -> heapPos);
  61.             shift_up((it - 1) / 2);
  62.         }
  63.     }
  64.     void insert(Block * val) {
  65.         heap[size] = val;
  66.         heap[size] -> heapPos = size;
  67.         shift_up(size);
  68.         size++;
  69.     }
  70.     void erase(int it) {
  71.         std::swap(heap[it], heap[size - 1]);
  72.         heap[it] -> heapPos = it;
  73.         heap[size - 1] -> heapPos = -1;
  74.         heap[size - 1] = 0;
  75.         size--;
  76.         if (it != size) {
  77.             shift_down(it);
  78.             shift_up(it);
  79.         }
  80.     }
  81.     Block * get_max() {
  82.         return heap[0];
  83.     }
  84. };
  85.  
  86. int allocate(int reque_number, int element, Heap * Pyr, std::list<Block *> List, Block ** Array) {
  87.     if (element > Pyr -> get_max() -> length()) {
  88.         return -2;
  89.     }
  90.     Block * RootBlock = Pyr -> get_max();
  91.     Block * FillRootBlock = new Block(RootBlock -> left, RootBlock -> left + element, 1);
  92.     Block * EmptyRootBlock = new Block(RootBlock -> left + element, RootBlock -> right, 0);
  93.     Pyr -> erase(0);
  94.     Pyr -> insert(EmptyRootBlock);
  95.     std::list<Block*>::iterator it = RootBlock -> listPos_it;
  96.     ++it;
  97.     List.insert(List.insert(it, EmptyRootBlock), FillRootBlock);
  98.     List.erase(it);
  99.     Array[reque_number] = FillRootBlock;
  100.     return FillRootBlock -> left;
  101. }
  102.  
  103. void deallocate(int element, Heap * Pyr, std::list<Block *> List, Block ** Array) {
  104.     Block * Cur = Array[-element - 1];
  105.     if (!Cur) return;
  106.     Cur -> type = 0;
  107.     std::list<Block*>::iterator it = Cur -> listPos_it;
  108.     std::list<Block*>::iterator LeftNode = it;
  109.     LeftNode--;
  110.     std::list<Block*>::iterator RightNode = it;
  111.     RightNode++;
  112.     if (RightNode != List.end() && (*RightNode) -> type == 0) {
  113.         Cur -> right = (*RightNode) -> right;
  114.         Pyr -> erase((*RightNode) -> heapPos);
  115.         List.erase(RightNode);
  116.     }
  117.     if (LeftNode != List.begin() && (*LeftNode) -> type == 0) {
  118.         Cur -> left = (*LeftNode) -> left;
  119.         Pyr -> erase((*LeftNode) -> heapPos);
  120.         List.erase(LeftNode);
  121.     }
  122.     Pyr -> insert(Cur);
  123.     Array[-element - 1] = 0;
  124. }
  125.  
  126. int main() {
  127.     int num, req;
  128.     std::cin >> num >> req;
  129.     int * Request = new int[req];
  130.     for (int i = 0; i < req; ++i) {
  131.         std::cin >> Request[i];
  132.     }
  133.     Block * Bl = new Block(0, num, 0);
  134.     Heap * He = new Heap();
  135.     He->insert(Bl);
  136.     List_.insert(List_.begin(), Bl);
  137.     Block ** Mas = new Block *[req];
  138.     for (int i = 0; i < req; ++i) {
  139.         if (Request[i] >= 0) {
  140.             std::cout << allocate(i, Request[i], He, List_, Mas) + 1 << std::endl;
  141.         }
  142.         else {
  143.             deallocate(Request[i], He, List_, Mas);
  144.         }
  145.     }
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement