#include <stdio.h> #define MAX (1000*1000*1000) #define MIN (-1*MAX) int main() { int ret1 = 0, ret2 = 0; int n, in[50000]; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &(in[i])); } int prev = MIN; for(int i = 0; i < n; i++) { if(i % 2 == 0 && in[i] <= prev) { ret1++; prev = MAX; } else if(i % 2 == 1 && in[i] >= prev) { ret1++; prev = MIN; } else { prev = in[i]; } } prev = MAX; for(int i = 0; i < n; i++) { if(i % 2 == 1 && in[i] <= prev) { ret2++; prev = MAX; } else if(i % 2 == 0 && in[i] >= prev) { ret2++; prev = MIN; } else { prev = in[i]; } } printf("%d", (ret1 < ret2 ? ret1 : ret2)); 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 | #include <stdio.h> #define MAX (1000*1000*1000) #define MIN (-1*MAX) int main() { int ret1 = 0, ret2 = 0; int n, in[50000]; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &(in[i])); } int prev = MIN; for(int i = 0; i < n; i++) { if(i % 2 == 0 && in[i] <= prev) { ret1++; prev = MAX; } else if(i % 2 == 1 && in[i] >= prev) { ret1++; prev = MIN; } else { prev = in[i]; } } prev = MAX; for(int i = 0; i < n; i++) { if(i % 2 == 1 && in[i] <= prev) { ret2++; prev = MAX; } else if(i % 2 == 0 && in[i] >= prev) { ret2++; prev = MIN; } else { prev = in[i]; } } printf("%d", (ret1 < ret2 ? ret1 : ret2)); return 0; } |