import sys def delta(x, y): if x < y: return 1 elif x == y: return 0 else: return -1 n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) d = list(map(delta, a, a[1:])) s = [2 * (x % 2) - 1 for x in range(n - 1)] c = [2 * ((x + 1) % 2) - 1 for x in range(n - 1)] sd = list(map(lambda x, y: x == y, d, s)) cd = list(map(lambda x, y: x == y, d, c)) cs = 0 i = 0 while i < len(sd)-1: if not sd[i]: cs += 1 if not sd[i+1]: i += 1 i += 1 if i < len(sd) and not sd[i]: cs += 1 cc = 0 i = 0 while i < len(cd)-1: if not cd[i]: cc += 1 if not cd[i+1]: i += 1 i += 1 if i < len(cd) and not cd[i]: cc += 1 print(min(cs, cc))
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 | import sys def delta(x, y): if x < y: return 1 elif x == y: return 0 else: return -1 n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) d = list(map(delta, a, a[1:])) s = [2 * (x % 2) - 1 for x in range(n - 1)] c = [2 * ((x + 1) % 2) - 1 for x in range(n - 1)] sd = list(map(lambda x, y: x == y, d, s)) cd = list(map(lambda x, y: x == y, d, c)) cs = 0 i = 0 while i < len(sd)-1: if not sd[i]: cs += 1 if not sd[i+1]: i += 1 i += 1 if i < len(sd) and not sd[i]: cs += 1 cc = 0 i = 0 while i < len(cd)-1: if not cd[i]: cc += 1 if not cd[i+1]: i += 1 i += 1 if i < len(cd) and not cd[i]: cc += 1 print(min(cs, cc)) |