Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int is_heap(int *a, int n) {
- int mn = 1, mx = 1;
- for (int i = 0; i < n; ++i) {
- int f1 = 0;
- int f2 = 0;
- if (2 * i + 1 < n && a[2 * i + 1] < a[i])
- f1 = 1;
- if (2 * i + 2 < n && a[2 * i + 2] < a[i])
- f1 = 1;
- if (2 * i + 1 < n && a[2 * i + 1] > a[i])
- f2 = 1;
- if (2 * i + 2 < n && a[2 * i + 2] > a[i])
- f2 = 1;
- if (f1)
- mn = 0;
- if (f2)
- mx = 0;
- }
- if (mn)
- return 1;
- if (mx)
- return -1;
- return 0;
- }
- int main(void) {
- FILE *in = fopen("input.bin", "rb");
- FILE *out = fopen("output.bin", "wb");
- int *a = malloc(sizeof(int));
- int mem = 1;
- int sz = 0;
- int x;
- while (fread(&x, sizeof(int), 1, in)) {
- if (sz + 1 >= mem) {
- mem *= 2;
- a = realloc(a, mem * sizeof(int));
- }
- a[sz] = x;
- ++sz;
- }
- int ans = is_heap(a, sz);
- fwrite(&ans, sizeof(int), 1, out);
- free(a);
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement