def check(xs, up=False): prev_x = -10**9 if up else 10**9 changes = 0 for x in xs: if up and prev_x >= x: changes += 1 prev_x = 10**9 elif not up and prev_x <= x: changes += 1 prev_x = -10**9 else: prev_x = x up = not up return changes n = int(input()) xs = list(map(int, input().split())) c1 = check(xs, True) c2 = check(xs, False) print(min(c1, c2))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def check(xs, up=False): prev_x = -10**9 if up else 10**9 changes = 0 for x in xs: if up and prev_x >= x: changes += 1 prev_x = 10**9 elif not up and prev_x <= x: changes += 1 prev_x = -10**9 else: prev_x = x up = not up return changes n = int(input()) xs = list(map(int, input().split())) c1 = check(xs, True) c2 = check(xs, False) print(min(c1, c2)) |