Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define cin fin
  5. #define cout fout
  6. #define sp << " " <<
  7. #define nl << "\n"
  8.  
  9. ifstream fin("prob.in");
  10. ofstream fout("prob.out");
  11.  
  12. int n, i, j, h[100], k, y, a;
  13.  
  14. void insert(int a) {
  15. n++;
  16. k++;
  17. h[k] = a;
  18. }
  19.  
  20. void sort(int nn) {
  21. int x;
  22. x = h[nn];
  23. while (nn > 1 && x > h[nn / 2]) {
  24. h[nn] = h[nn / 2];
  25. nn = nn / 2;
  26. }
  27. h[nn] = x;
  28. }
  29.  
  30. void jos(int n) {
  31. int fiu = -1;
  32. while (fiu != 0) {
  33. if (2 * n <= k)
  34. fiu = 2 * n;
  35.  
  36. if (2 * n + 1 <= k && h[2 * n + 1] >= h[fiu])
  37. fiu = 2 * n + 1;
  38. if (h[fiu] <= h[n])
  39. fiu = 0;
  40. if (fiu) {
  41. swap(h[n], h[fiu]);
  42. n = fiu;
  43. }
  44. }
  45. }
  46. void elimin() {
  47. if (k < 1) return;
  48. cout<<h[1]<<" ";
  49. h[1] = h[k];
  50. k--;
  51. n--;
  52. jos(1);
  53. }
  54.  
  55. int main() {
  56. int t;
  57. k = 0;
  58. while (cin >> t) {
  59. insert(t);
  60. sort(k);
  61. }
  62.  
  63. cout << "Max-heapul dat: " << endl;
  64. for (i = 1; i <= k; i++)
  65. cout << h[i] << " ";
  66. cout nl;
  67.  
  68.  
  69. cout << "Elementele max :" nl ;
  70.  
  71. for (int i = 1; i <= 3; i++) {
  72. elimin();
  73. }
  74. cout nl;
  75. if (k == 0) {
  76. cout << "Heapul nu are elemente" nl;
  77. } else {
  78.  
  79. cout << "Max-heapul nou: " << endl;
  80. for (i = 1; i <= k; i++)
  81. cout << h[i] << " ";
  82. cout << endl;
  83. }
  84.  
  85.  
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement