#include <stdio.h> #include <stdlib.h> #define M 1000001 #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) int check(int *buff, int n, int up) { int i,p; int ret = 0; p = up ? -M : M; for (i=0; i<=n; ++i) { if (up) { if (buff[i] > p) { p = buff[i]; } else { ret++; p = M; } } else { if (buff[i] < p) { p = buff[i]; } else { ret++; p = -M; } } up = !up; } return ret; } int main() { int n, *buff,i,a,b; scanf("%d\n", &n); buff = (int *)malloc(n*sizeof(*buff)); for (i=0; i<n; ++i) scanf("%d", buff+i); a = check(buff,n,1); b = check(buff,n,0); printf("%d\n", MIN(a,b)); free(buff); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> #include <stdlib.h> #define M 1000001 #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) int check(int *buff, int n, int up) { int i,p; int ret = 0; p = up ? -M : M; for (i=0; i<=n; ++i) { if (up) { if (buff[i] > p) { p = buff[i]; } else { ret++; p = M; } } else { if (buff[i] < p) { p = buff[i]; } else { ret++; p = -M; } } up = !up; } return ret; } int main() { int n, *buff,i,a,b; scanf("%d\n", &n); buff = (int *)malloc(n*sizeof(*buff)); for (i=0; i<n; ++i) scanf("%d", buff+i); a = check(buff,n,1); b = check(buff,n,0); printf("%d\n", MIN(a,b)); free(buff); return 0; } |