#include <cstdio> #include <algorithm> int main() { int n; scanf("%d", &n); int rising_lowered = 1; int rising_unchanged = 0; int rising_increased = 1; int falling_lowered = 1; int falling_unchanged = 0; int falling_increased = 1; int prev; scanf("%d", &prev); for (int i = 1; i < n; i++) { int num; scanf("%d", &num); const bool is_falling = num < prev; const bool is_rising = num > prev; const int new_rising_lowered = 1 + falling_lowered; const int new_rising_unchanged = is_rising ? std::min(falling_lowered, falling_unchanged) : falling_lowered; const int new_rising_increased = 1 + std::min({falling_lowered, falling_unchanged, falling_increased}); const int new_falling_lowered = 1 + std::min({rising_lowered, rising_unchanged, rising_increased}); const int new_falling_unchanged = is_falling ? std::min(rising_increased, rising_unchanged) : rising_increased; const int new_falling_increased = 1 + rising_increased; rising_lowered = new_rising_lowered; rising_unchanged = new_rising_unchanged; rising_increased = new_rising_increased; falling_lowered = new_falling_lowered; falling_unchanged = new_falling_unchanged; falling_increased = new_falling_increased; prev = num; } const int result = std::min({ rising_lowered, rising_unchanged, rising_increased, falling_lowered, falling_unchanged, falling_increased, }); printf("%d\n", result); 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 59 | #include <cstdio> #include <algorithm> int main() { int n; scanf("%d", &n); int rising_lowered = 1; int rising_unchanged = 0; int rising_increased = 1; int falling_lowered = 1; int falling_unchanged = 0; int falling_increased = 1; int prev; scanf("%d", &prev); for (int i = 1; i < n; i++) { int num; scanf("%d", &num); const bool is_falling = num < prev; const bool is_rising = num > prev; const int new_rising_lowered = 1 + falling_lowered; const int new_rising_unchanged = is_rising ? std::min(falling_lowered, falling_unchanged) : falling_lowered; const int new_rising_increased = 1 + std::min({falling_lowered, falling_unchanged, falling_increased}); const int new_falling_lowered = 1 + std::min({rising_lowered, rising_unchanged, rising_increased}); const int new_falling_unchanged = is_falling ? std::min(rising_increased, rising_unchanged) : rising_increased; const int new_falling_increased = 1 + rising_increased; rising_lowered = new_rising_lowered; rising_unchanged = new_rising_unchanged; rising_increased = new_rising_increased; falling_lowered = new_falling_lowered; falling_unchanged = new_falling_unchanged; falling_increased = new_falling_increased; prev = num; } const int result = std::min({ rising_lowered, rising_unchanged, rising_increased, falling_lowered, falling_unchanged, falling_increased, }); printf("%d\n", result); return 0; } |