Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 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("prob5.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 aranjeaza(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 coboara(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 sterg() {
  47. if (k < 1) return;
  48. h[1] = h[k];
  49. k--;
  50. n--;
  51. coboara(1);
  52. }
  53.  
  54. int main() {
  55. int t;
  56. k = 0;
  57. while (cin >> t) {
  58. insert(t);
  59. aranjeaza(k);
  60. }
  61.  
  62. cout << "Avem max-heapul: " << endl;
  63. for (i = 1; i <= k; i++)
  64. cout << h[i] << " ";
  65. cout nl nl;
  66.  
  67.  
  68. cout << "Max1, Max2 si Max3 sunt: " << h[1] sp h[2] sp h[3] nl nl;
  69.  
  70. for (int i = 1; i <= 3; i++) {
  71. sterg();
  72. }
  73.  
  74. if (k == 0) {
  75. cout << "Heapul este gol!" nl;
  76. } else {
  77.  
  78. cout << "Avem max-heapul modificat: " << endl;
  79. for (i = 1; i <= k; i++)
  80. cout << h[i] << " ";
  81. cout << endl;
  82. }
  83.  
  84.  
  85.  
  86. // vector<int> lol;
  87. // for (i = 1; i <= n; i++)
  88. // lol.push_back(h[i]);
  89. // sort(lol.begin(), lol.end());
  90. // int maxxx = -999999;
  91. // int nmaxx = 0;
  92. // cout << "Elementele din heap in ordine crescatoare:\n";
  93. // for (auto &it: lol) {
  94. // if (it > maxxx) {
  95. // maxxx = it;
  96. // nmaxx = 1;
  97. // } else if (it == maxxx) nmaxx++;
  98. // cout << it << " ";
  99. // }
  100. // cout << endl;
  101. // cout << "Elemente egale cu max: " << nmaxx << " ("<< maxxx << ")\n";
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement