Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include "optimization.h"
  4.  
  5. using namespace std;
  6.  
  7. struct HEAP {
  8.     int n;
  9.     int h[100001];
  10.  
  11.     int min() {
  12.         if (n) return h[1];
  13.         else return 2147483647;
  14.     }
  15.  
  16.     int down(int x) {
  17.         while (true) {
  18.             int l = 2 * x;
  19.             if (l < n and h[l + 1] < h[l]) l++;
  20.             if (l > n or h[l] >= h[x]) break;
  21.             swap(h[l], h[x]), x = l;
  22.         }
  23.         return x;
  24.     }
  25.  
  26.     void add(int x) {
  27.         n++;
  28.         h[n] = x;
  29.         for (int i = n; (i > 1) and (h[i / 2] > h[i]); i /= 2)
  30.             swap(h[i], h[i / 2]);
  31.     }
  32.  
  33.     int pop() {
  34.         swap(h[1], h[n]);
  35.         n--;
  36.         down(1);
  37.         return h[n + 1];
  38.     }
  39.  
  40. };
  41.  
  42. int main() {
  43. #ifdef DEBUG
  44.     assert(freopen("test.txt", "r", stdin));
  45. #endif
  46.  
  47.     HEAP minimum, min_del;
  48.     HEAP maximum, max_del;
  49.  
  50.     int n = readInt();
  51.  
  52.     while (n != 0) {
  53.         char s[99];
  54.         readWord(s);
  55.         if (s[0] == 'I') {
  56.             int x = atoi(s + 7);
  57.             minimum.add(x);
  58.             maximum.add(-x);
  59.         } else if (s[4] == 'i') {
  60.             while (min_del.min() == minimum.min()) {
  61.                 minimum.pop();
  62.                 min_del.pop();
  63.             }
  64.             int x = minimum.pop();
  65.             writeInt(x);
  66.             writeChar('\n');
  67.             max_del.add(-x);
  68.         } else if (s[4] == 'a') {
  69.             while (max_del.min() == maximum.min()) {
  70.                 maximum.pop();
  71.                 max_del.pop();
  72.             }
  73.             int x = -maximum.pop();
  74.             writeInt(x);
  75.             writeChar('\n');
  76.             min_del.add(x);
  77.         }
  78.         n--;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement