Advertisement
Guest User

Untitled

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