#include <cstdio> int f[50505]; template <typename T> T min_of(T a, T b) { return a < b ? a : b; } int compute(int *f, int n, bool up) { int mods = 0; for (int i = 1; i < n; ++i) { if((up && f[i] <= f[i-1]) || (!up && f[i] >= f[i -1])) { mods += 1; i +=1; } else { up = !up; } } return mods; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &f[i]); } printf("%d\n", min_of(compute(f, N, true), compute(f, N, false))); 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 | #include <cstdio> int f[50505]; template <typename T> T min_of(T a, T b) { return a < b ? a : b; } int compute(int *f, int n, bool up) { int mods = 0; for (int i = 1; i < n; ++i) { if((up && f[i] <= f[i-1]) || (!up && f[i] >= f[i -1])) { mods += 1; i +=1; } else { up = !up; } } return mods; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &f[i]); } printf("%d\n", min_of(compute(f, N, true), compute(f, N, false))); return 0; } |