import sys n = int(sys.stdin.readline().strip()) X = [int(x) for x in sys.stdin.readline().split(" ")] WU = [0] * n WC = [1] * n NU = [0] * n NC = [1] * n if __name__ == "__main__": for i in range(1, n): WU[i] = NC[i-1] if X[i] > X[i-1]: WU[i] = min(WU[i], NU[i-1]) NU[i] = WC[i-1] if X[i] < X[i-1]: NU[i] = min(NU[i], WU[i-1]) WC[i] = min(NU[i-1] + 1, NC[i-1] + 1) NC[i] = min(WU[i-1] + 1, WC[i-1] + 1) print(min(WU[n-1], NU[n-1], WC[n-1], NC[n-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 | import sys n = int(sys.stdin.readline().strip()) X = [int(x) for x in sys.stdin.readline().split(" ")] WU = [0] * n WC = [1] * n NU = [0] * n NC = [1] * n if __name__ == "__main__": for i in range(1, n): WU[i] = NC[i-1] if X[i] > X[i-1]: WU[i] = min(WU[i], NU[i-1]) NU[i] = WC[i-1] if X[i] < X[i-1]: NU[i] = min(NU[i], WU[i-1]) WC[i] = min(NU[i-1] + 1, NC[i-1] + 1) NC[i] = min(WU[i-1] + 1, WC[i-1] + 1) print(min(WU[n-1], NU[n-1], WC[n-1], NC[n-1])) |