Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int a[100000];
  6.  
  7. bool sift_down(int n, int i) {
  8. int MaxRoot = i;
  9. int l_child = 2 * i + 1;
  10. int r_child = 2 * i + 2;
  11.  
  12. if (l_child < n && a[l_child] < a[MaxRoot])
  13. MaxRoot = l_child;
  14.  
  15. if (r_child < n && a[r_child] < a[MaxRoot])
  16. MaxRoot = r_child;
  17.  
  18. if (MaxRoot != i)
  19. return true;
  20.  
  21. return false;
  22. }
  23.  
  24. int main()
  25. {
  26. ifstream fin("isheap.in");
  27. ofstream fout("isheap.out");
  28. int n;
  29. cin >> n;
  30. for (int i = 0; i < n; i++)
  31. cin >> a[i];
  32.  
  33. for (int i = n / 2 - 1; i >= 0; i--) {
  34. if (sift_down(n, i)) {
  35. cout << "NO" << endl;
  36. return 0;
  37. }
  38. }
  39. cout << "YES" << endl;
  40.  
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement