Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. long long Heap[1000000], size = 0;
  4. void siftUp (int v) {
  5. while (v > 0 && Heap[v] < Heap[(v - 1) / 2]) {
  6. swap(Heap[v], Heap[(v - 1) / 2]);
  7. v = (v - 1) / 2;
  8. }
  9. }
  10. void siftDown (int v) {
  11. while (2 * v + 1 < size) {
  12. int left = 2 * v + 1;
  13. int right = 2 * v + 2;
  14. int minCh = left;
  15. if (right < size && Heap[right] < Heap[left])
  16. minCh = right;
  17. if (Heap[v] <= Heap[minCh])
  18. return;
  19. swap(Heap[v], Heap[minCh]);
  20. v = minCh;
  21. }
  22. }
  23. void insert (int x) {
  24. Heap[size++] = x;
  25. siftUp(size - 1);
  26. }
  27. int extractMax() {
  28. int t = Heap[0];
  29. Heap[0] = Heap[--size];
  30. siftDown(0);
  31. return t;
  32. }
  33. int main(){
  34. int quant, cmd, N;
  35. cin>>quant;
  36. for(int i = 0; i < quant; i++){
  37. cin>>cmd;
  38. if(cmd == 1)
  39. size = 0;
  40. if(cmd == 2){
  41. cin>>N;
  42. insert(N);
  43. }
  44. if((cmd==3)&&(size != 0))
  45. cout << extractMax() << endl;
  46. else if(cmd == 3){
  47. cout<<"CANNOT"<<endl;
  48. }
  49. }
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement