import sys n = int(input().strip()) numbers = list(map(int, input().strip().split(' '))) def sign(a, b): if a == b: return 0 elif a > b: return -1 elif a < b: return 1 def compute(numbers, current_sign): # print('current sign', current_sign) numbers = numbers[:] cost = 0 for i in range(1, len(numbers)): prev = numbers[i-1] curr = numbers[i] s = sign(prev, curr) if s == current_sign: pass else: cost += 1 numbers[i] = current_sign * 1e9 # print('cost at', i, prev, curr) current_sign *= -1 return cost print(min(compute(numbers, 1), compute(numbers, -1)))
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 | import sys n = int(input().strip()) numbers = list(map(int, input().strip().split(' '))) def sign(a, b): if a == b: return 0 elif a > b: return -1 elif a < b: return 1 def compute(numbers, current_sign): # print('current sign', current_sign) numbers = numbers[:] cost = 0 for i in range(1, len(numbers)): prev = numbers[i-1] curr = numbers[i] s = sign(prev, curr) if s == current_sign: pass else: cost += 1 numbers[i] = current_sign * 1e9 # print('cost at', i, prev, curr) current_sign *= -1 return cost print(min(compute(numbers, 1), compute(numbers, -1))) |