Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- bool sift_down(int* a, int n, int i) {
- int par = i;
- int l_child = 2 * i + 1;
- int r_child = 2 * i + 2;
- if (l_child < n && a[l_child] < a[par]) par = l_child;
- if (r_child < n && a[r_child] < a[par]) par = r_child;
- if (par != i) return true;
- return false;
- }
- int main(){
- ifstream fin("isheap.in");
- ofstream fout("isheap.out");
- int n;
- fin >> n;
- int* a = new int[n];
- for (int i = 0; i < n; i++) fin >> a[i];
- for (int i = n / 2 - 1; i >= 0; i--) {
- if (sift_down(a, n, i)) {
- fout << "NO" << endl;
- return 0;
- }
- }
- fout << "YES" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement