gt22

Untitled

Dec 20th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct Heap
  6. {
  7.     vector<int> v, heap_v, pos;  ///v - значения, heap_v - куча индексов, pos - позиции в куче
  8.     int n = 0;
  9.     bool flag_; ///определитель кучи на максимум или минимум
  10.  
  11.     Heap(bool flag){
  12.         heap_v.resize(1);
  13.         flag_ = flag;
  14.     }
  15.  
  16.     void siftUp(int i)
  17.     {
  18.         while ((i > 1) && ( v[heap_v[i/2]] > v[heap_v[i]])^flag_) {
  19.  
  20.             pos[heap_v[i]]  = i/2;
  21.             pos[heap_v[i/2]] = i;
  22.             swap(heap_v[i], heap_v[i/2]);
  23.             i /= 2;
  24.         }
  25.     }
  26.  
  27.     void siftDown(int i) {
  28.         while (2*i<=n){
  29.             int l = 2*i;
  30.             if ((l+1<=n) && (v[heap_v[l]] < v[heap_v[l+1]])^flag_) l++;
  31.             if ((v[heap_v[l]] < v[heap_v[i]])^flag_ && l > n) break;
  32.             else {
  33.                 pos[heap_v[l]]  = i;
  34.                 pos[heap_v[i]] = l;
  35.                 swap(heap_v[l], heap_v[i]);
  36.                 i = l;
  37.             }
  38.         }
  39.     }
  40.  
  41.     void Insert(int new_el) {
  42.         v.push_back(new_el);
  43.         pos.push_back(n+1);
  44.         heap_v.push_back(v.size()-1);
  45.         n++;
  46.         siftUp(n);
  47.     }
  48.  
  49.     void Del(int i) {
  50.         ///i = pos[i];
  51.         swap(heap_v[i], heap_v[n]);
  52.  
  53.         heap_v.pop_back();
  54.         n--;
  55.         if(i <= n) {
  56.             pos[heap_v[i]] = i;
  57.             siftUp(i);
  58.             siftDown(i);
  59.         }
  60.     }
  61.  
  62.     int Get(int i) { return v[heap_v[i]];}
  63. };
  64.  
  65. int main()
  66. {
  67.     ios_base:: sync_with_stdio(false);
  68.     cin.tie(0);
  69.     int k;
  70.     int x;
  71.     string command, num;
  72.     Heap min_heap(0);
  73.     Heap max_heap(1);
  74.     cin >> k;
  75.  
  76.     for (int i=0; i<k; i++){
  77.         num="";
  78.         cin >> command;
  79.  
  80.         for (int j=0; j<command.size(); j++){
  81.             if ( '0' <= command[j] && command[j] <= '9') {num+= command[j];}
  82.             else continue;
  83.         }
  84.         if (num != "") x = stoi(num);
  85.         if (command[0]=='I') {
  86.             min_heap.Insert(x);
  87.             max_heap.Insert(x);
  88.         }
  89.         else {
  90.             if (command[4] == 'i') {
  91.                 max_heap.Del(max_heap.pos[min_heap.heap_v[1]]);
  92.                 cout << min_heap.Get(1) << '\n';
  93.                 min_heap.Del(1);
  94.             }
  95.  
  96.             else {
  97.                 min_heap.Del(min_heap.pos[max_heap.heap_v[1]]);
  98.                 cout << max_heap.Get(1) << '\n';
  99.                 max_heap.Del(1);
  100.             }
  101.  
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment