#include <cstdio> #include <algorithm> int vol[60000]; int main() { int N; int resup=0; int resdown=0; scanf("%d\n", &N); for (int i=0; i<N; ++i) { int v; scanf("%d", &v); vol[i] = v; } int last = vol[0]; for (int i=1; i<N; ++i) { if ((i%2)==1) { //up if (vol[i]<=last) { resup++; last = 1000001; } else { last = vol[i]; } } else { //down if (vol[i]>=last) { resup++; last = -1000001; } else { last = vol[i]; } } } last = vol[0]; for (int i=1; i<N; ++i) { if ((i%2)==0) { //up if (vol[i]<=last) { resdown++; last = 1000001; } else { last = vol[i]; } } else { //down if (vol[i]>=last) { resdown++; last = -1000001; } else { last = vol[i]; } } } printf("%d\n", (int)std::min(resdown, resup)); 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 52 53 54 55 56 57 58 | #include <cstdio> #include <algorithm> int vol[60000]; int main() { int N; int resup=0; int resdown=0; scanf("%d\n", &N); for (int i=0; i<N; ++i) { int v; scanf("%d", &v); vol[i] = v; } int last = vol[0]; for (int i=1; i<N; ++i) { if ((i%2)==1) { //up if (vol[i]<=last) { resup++; last = 1000001; } else { last = vol[i]; } } else { //down if (vol[i]>=last) { resup++; last = -1000001; } else { last = vol[i]; } } } last = vol[0]; for (int i=1; i<N; ++i) { if ((i%2)==0) { //up if (vol[i]<=last) { resdown++; last = 1000001; } else { last = vol[i]; } } else { //down if (vol[i]>=last) { resdown++; last = -1000001; } else { last = vol[i]; } } } printf("%d\n", (int)std::min(resdown, resup)); return 0; } |