Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main (void) {
- FILE *fin = fopen ("input.bin", "rb");
- FILE *fout = fopen ("output.bin", "wb");
- int x, cur_index = 1, alloc = 2;
- int *a = malloc (2);
- while (fread (&x, sizeof (int), 1, fin)) {
- if (cur_index == alloc) {
- alloc <<= 1;
- a = realloc (a, alloc * sizeof (int));
- }
- a[cur_index] = x;
- ++cur_index;
- }
- int flag1 = 0;
- if (cur_index == 2) {
- x = 0;
- fwrite (&x, sizeof (int), 1, fout);
- return 0;
- }
- for (int i = 1; i < cur_index; ++i) {
- if (i * 2 < cur_index) {
- if (a[i] <= a[i * 2]) {
- flag1 = 1;
- } else {
- flag1 = 0;
- break;
- }
- }
- if (i * 2 + 1 < cur_index) {
- if (a[i] <= a[i * 2 + 1]) {
- flag1 = 1;
- } else {
- flag1 = 0;
- break;
- }
- }
- }
- if (flag1) {
- x = 1;
- fwrite (&x, sizeof (int), 1, fout);
- free (a);
- fclose (fin);
- fclose (fout);
- return 0;
- }
- for (int i = 1; i < cur_index; ++i) {
- if (i * 2 < cur_index) {
- if (a[i] > a[i * 2]) {
- flag1 = 1;
- } else {
- flag1 = 0;
- break;
- }
- }
- if (i * 2 + 1 < cur_index) {
- if (a[i] > a[i * 2 + 1]) {
- flag1 = 1;
- } else {
- flag1 = 0;
- break;
- }
- }
- }
- if (flag1) {
- x = -1;
- fwrite (&x, sizeof (int), 1, fout);
- free (a);
- fclose (fin);
- fclose (fout);
- return 0;
- } else {
- x = 0;
- fwrite (&x, sizeof (int), 1, fout);
- }
- free (a);
- fclose (fin);
- fclose (fout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement