Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin ("heap.in");
  6. ofstream fout ("heap.out");
  7.  
  8. int m, n, h[250005];
  9.  
  10. void Urca(int i)
  11. {
  12. int tata;
  13. while(i > 1)
  14. {
  15. tata = i / 2;
  16. if(h[tata] >= h[i])
  17. return;
  18. swap(h[tata], h[i]);
  19. i = tata;
  20. }
  21. }
  22.  
  23. void Coboara(int i)
  24. {
  25. int fiu;
  26. while(i <= n / 2)
  27. {
  28. fiu = 2 * i;
  29. if(fiu + 1 <= n && h[fiu] <= h[fiu + 1])
  30. fiu++;
  31. if(h[i] >= h[fiu])
  32. return;
  33. swap(h[i], h[fiu]);
  34. i = fiu;
  35. }
  36. }
  37.  
  38. int main()
  39. {
  40. int i, x;
  41. fin >> m;
  42. for(i=1; i<=m; i++)
  43. {
  44. fin >> x;
  45. if(x == 1)
  46. {
  47. n++;
  48. fin >> h[n];
  49. Urca(n);
  50. }
  51. else
  52. {
  53. fout << h[1] << "\n";
  54. h[1] = h[n];
  55. n--;
  56. Coboara(1);
  57. }
  58. }
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement