n = int(input()) sounds = [int(x) for x in input().strip().split(" ")] def min_starting(direction: bool): min_change_max, min_change_min, min_no_change, previous, going_up = (1, 1, 0, sounds[0], direction) for x in sounds[1:]: if not going_up: no_change = min_change_max else: no_change = min_change_min if (x < previous and not going_up) or (x > previous and going_up): no_change = min(no_change, min_no_change) change_max = min([min_change_max, min_change_min, min_no_change]) + 1 change_min = min([min_change_max, min_change_min, min_no_change]) + 1 min_change_max, min_change_min, min_no_change, previous, going_up = (change_max, change_min, no_change, x, not going_up) return min([min_change_max, min_change_min, min_no_change]) print(min(min_starting(True), min_starting(False)))
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 | n = int(input()) sounds = [int(x) for x in input().strip().split(" ")] def min_starting(direction: bool): min_change_max, min_change_min, min_no_change, previous, going_up = (1, 1, 0, sounds[0], direction) for x in sounds[1:]: if not going_up: no_change = min_change_max else: no_change = min_change_min if (x < previous and not going_up) or (x > previous and going_up): no_change = min(no_change, min_no_change) change_max = min([min_change_max, min_change_min, min_no_change]) + 1 change_min = min([min_change_max, min_change_min, min_no_change]) + 1 min_change_max, min_change_min, min_no_change, previous, going_up = (change_max, change_min, no_change, x, not going_up) return min([min_change_max, min_change_min, min_no_change]) print(min(min_starting(True), min_starting(False))) |