Advertisement
Guest User

9

a guest
Dec 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int is_heap(int *a, int n) {
  6.     int mn = 1, mx = 1;
  7.     for (int i = 0; i < n; ++i) {
  8.         int f1 = 0;
  9.         int f2 = 0;
  10.         if (2 * i + 1 < n && a[2 * i + 1] < a[i])
  11.             f1 = 1;
  12.         if (2 * i + 2 < n && a[2 * i + 2] < a[i])
  13.             f1 = 1;
  14.  
  15.         if (2 * i + 1 < n && a[2 * i + 1] > a[i])
  16.             f2 = 1;
  17.         if (2 * i + 2 < n && a[2 * i + 2] > a[i])
  18.             f2 = 1;
  19.  
  20.         if (f1)
  21.             mn = 0;
  22.         if (f2)
  23.             mx = 0;
  24.     }
  25.     if (mn)
  26.         return 1;
  27.     if (mx)
  28.         return -1;
  29.     return 0;
  30. }
  31.  
  32. int main(void) {
  33.     FILE *in = fopen("input.bin", "rb");
  34.     FILE *out = fopen("output.bin", "wb");
  35.  
  36.     int *a = malloc(sizeof(int));
  37.     int mem = 1;
  38.     int sz = 0;
  39.     int x;
  40.  
  41.     while (fread(&x, sizeof(int), 1, in)) {
  42.         if (sz + 1 >= mem) {
  43.             mem *= 2;
  44.             a = realloc(a, mem * sizeof(int));
  45.         }
  46.         a[sz] = x;
  47.         ++sz;
  48.     }
  49.  
  50.     int ans = is_heap(a, sz);
  51.     fwrite(&ans, sizeof(int), 1, out);
  52.  
  53.     free(a);
  54.  
  55.     fclose(in);
  56.     fclose(out);
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement