#include #include int m, n; int i, j, k; int min, max; int length = 0; int temp_j = 0; int temp_k, temp_m, temp_length; /*This program with a command-line parameters: m is a non-negative integer,= and f is the name of a file containing n integers, i0, i1, ..., in-1, in free f= ormat. The program would find the longest interval, ij, ij+1, ..., ik, such that m= x-mn<=m, and print the interval's start position, j, its end position, k, and its le= ngth, k-j+1.*/ void check() { temp_m = max - min; if(temp_m <= m) { temp_length = temp_k - temp_j + 1; if(temp_length > length) { j = temp_j; k = temp_k; length = temp_length; } } temp_m = m + m; } main(int argc, char *argv[]) { int* a; int b, count = 0; int front, gear; char f[10]; FILE* readfile = NULL; a = (int*)malloc(sizeof(int)*1000000); m = atol(argv[1]); strcpy(f,argv[2]); readfile = fopen(f, "r"); if (readfile == NULL) { fprintf(stderr, "Unable to open input file.\n"); exit(1); } else { while(fscanf(readfile, "%d", &a[count]) != EOF) { count++; } fclose(readfile); } while(temp_j < count - 1) { temp_k = temp_j + 1; if(a[temp_j] > a[temp_k]) { front = 1; gear = 0; min = a[temp_k]; max = a[temp_j]; } else { front = 0; gear = 1; min = a[temp_j]; max = a[temp_k]; } check(); b = temp_j + 2; if (b == count) { break; } for(i = b; i < count; i++) { if(front == 0) { if(a[temp_j] > a[i]) { front = 1; gear = 0; temp_j++; break; } else { if(a[i] >= max) { max = a[i]; temp_k = i; check(); } } } else { if(a[temp_j] < a[i]) { front = 0; gear = 1; temp_j++; break; } else { if(a[i] <= min) { min = a[i]; temp_k = i; check(); } } } if(i == count - 1) { temp_j++; } } } printf("solution: %d %d %d\n", j, k, length); return 0; }