#include <stdio.h> #include <algorithm> const int C=1000001; int a[C], seq[C]; int main(){ int n, diff; scanf ("%d", &n); for (int i=0; i<n; i++) scanf ("%d", &a[i]); for (int i=1; i<n; i++){ diff = a[i] - a[i-1]; if (diff > 0) seq[i] = 2; else if (diff == 0) seq[i] = 1; else seq[i] = 0; } int res_1 = 0, res_2 = 0; for (int i=1; i<n; ){ if (i%2 == 0 && seq[i] != 0){ res_1 += 1; i+=2; } else if (i%2 == 1 && seq[i] != 2){ res_1 += 1; i+=2; } else i++; } for (int i=1; i<n; ){ if (i%2 == 0 && seq[i] != 2){ res_2 += 1; i+=2; } else if (i%2 == 1 && seq[i] != 0){ res_2 += 1; i+=2; } else i++; } printf ("%d\n", std::min(res_1, res_2)); 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 | #include <stdio.h> #include <algorithm> const int C=1000001; int a[C], seq[C]; int main(){ int n, diff; scanf ("%d", &n); for (int i=0; i<n; i++) scanf ("%d", &a[i]); for (int i=1; i<n; i++){ diff = a[i] - a[i-1]; if (diff > 0) seq[i] = 2; else if (diff == 0) seq[i] = 1; else seq[i] = 0; } int res_1 = 0, res_2 = 0; for (int i=1; i<n; ){ if (i%2 == 0 && seq[i] != 0){ res_1 += 1; i+=2; } else if (i%2 == 1 && seq[i] != 2){ res_1 += 1; i+=2; } else i++; } for (int i=1; i<n; ){ if (i%2 == 0 && seq[i] != 2){ res_2 += 1; i+=2; } else if (i%2 == 1 && seq[i] != 0){ res_2 += 1; i+=2; } else i++; } printf ("%d\n", std::min(res_1, res_2)); return 0;} |