Advertisement
double_trouble

Untitled

Nov 9th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <climits>
  7. #include <stdio.h>
  8. #include <iomanip>
  9. #include <stack>
  10.  
  11.  
  12. #define F first
  13. #define S second
  14.  
  15. using namespace std;
  16.  
  17. long a[200000];
  18. int n;
  19.  
  20. void siftup(long i)
  21. {
  22.     while (i > 0 && a[i] > a[i / 2])
  23.     {
  24.         swap(a[i], a[i / 2]);
  25.         i /= 2;
  26.     }
  27. }
  28.  
  29. void siftdown(long i)
  30. {
  31.     int j;
  32.     while (i * 2 < n)
  33.     {
  34.         j = i;
  35.         if (a[i * 2] > a[j])
  36.             j = i * 2;
  37.         if (i * 2 + 1 < n && a[i * 2 + 1] > a[j])
  38.             j = i * 2 + 1;
  39.         if (i == j)
  40.             break;
  41.         swap(a[i], a[j]);
  42.         i = j;
  43.     }
  44. }
  45.  
  46. void ins(long i)
  47. {
  48.     a[n] = i;
  49.     siftup(n);
  50.    // n++;
  51. }
  52.  
  53. void del(long i)
  54. {
  55.     a[0] = a[n - 1];
  56.     n--;
  57.     siftdown(0);
  58. }
  59.  
  60. int main()
  61. {
  62.    freopen("input.txt","r", stdin);
  63.    freopen("output.txt", "w",stdout);
  64.    string s;
  65.    int x;
  66.    n = 0;
  67.    while (cin >> s)
  68.    {
  69.        //cout << n << endl;
  70.        //cout << s << endl;
  71.        if (s == "ADD")
  72.        {
  73.            cin >> x;
  74.            ins(x);
  75.            n++;
  76.        }
  77.        if (s == "EXTRACT")
  78.            if (n == 0)
  79.            cout << "CANNOT" << endl;
  80.            else
  81.            {
  82.                cout << a[0] << endl;
  83.                del(0);
  84.            }
  85.         if (s == "CLEAR")
  86.             while(n != 0)
  87.             del(0);
  88.    }
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement