seq_len = input() sequence = [int(i) for i in input().split()] i = 0 result1 = 0 # starting with lower sequence2 = sequence.copy() while i < len(sequence[:-1]): if i % 2 == 0: if sequence[i+1] <= sequence[i]: result1 += 1 try: sequence[i+1] = sequence[i+2] + 1 except IndexError: sequence[i+1] = sequence[i] + 1 else: if sequence[i+1] >= sequence[i]: result1 += 1 try: sequence[i+1] = sequence[i+2] - 1 except IndexError: sequence[i+1] = sequence[i] - 1 i += 1 i = 0 result2 = 0 # starting with greater while i < len(sequence2[:-1]): if i % 2 == 0: if sequence2[i+1] >= sequence2[i]: result2 += 1 try: sequence2[i+1] = sequence2[i+2] - 1 except IndexError: sequence2[i+1] = sequence2[i] - 1 else: if sequence2[i+1] <= sequence2[i]: result2 += 1 try: sequence2[i+1] = sequence2[i+2] + 1 except IndexError: sequence2[i+1] = sequence2[i] + 1 i += 1 print(min(result1, result2))
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 | seq_len = input() sequence = [int(i) for i in input().split()] i = 0 result1 = 0 # starting with lower sequence2 = sequence.copy() while i < len(sequence[:-1]): if i % 2 == 0: if sequence[i+1] <= sequence[i]: result1 += 1 try: sequence[i+1] = sequence[i+2] + 1 except IndexError: sequence[i+1] = sequence[i] + 1 else: if sequence[i+1] >= sequence[i]: result1 += 1 try: sequence[i+1] = sequence[i+2] - 1 except IndexError: sequence[i+1] = sequence[i] - 1 i += 1 i = 0 result2 = 0 # starting with greater while i < len(sequence2[:-1]): if i % 2 == 0: if sequence2[i+1] >= sequence2[i]: result2 += 1 try: sequence2[i+1] = sequence2[i+2] - 1 except IndexError: sequence2[i+1] = sequence2[i] - 1 else: if sequence2[i+1] <= sequence2[i]: result2 += 1 try: sequence2[i+1] = sequence2[i+2] + 1 except IndexError: sequence2[i+1] = sequence2[i] + 1 i += 1 print(min(result1, result2)) |