from math import inf def solve(tab): n = len(tab) dp = [[inf for _ in range(4)] for _ in range(n)] bound = pow(10, 9) dp[0] = [0, 0, 1, 1] for i in range(1, n): if tab[i] < bound: dp[i][0] = dp[i-1][3] if tab[i-1] > tab[i]: dp[i][0] = min(dp[i][0], dp[i-1][1]) if tab[i] > -bound: dp[i][1] = dp[i-1][2] if tab[i-1] < tab[i]: dp[i][1] = min(dp[i][1], dp[i - 1][0]) if tab[i-1] > -bound: dp[i][2] = dp[i-1][1] + 1 dp[i][2] = min(dp[i][2], dp[i-1][3] + 1) if tab[i-1] < bound: dp[i][3] = dp[i-1][0] + 1 dp[i][3] = min(dp[i][3], dp[i-1][2] + 1) return min(dp[-1]) how_many = int(input()) print(solve([int(x) for x in input().split()]))
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 | from math import inf def solve(tab): n = len(tab) dp = [[inf for _ in range(4)] for _ in range(n)] bound = pow(10, 9) dp[0] = [0, 0, 1, 1] for i in range(1, n): if tab[i] < bound: dp[i][0] = dp[i-1][3] if tab[i-1] > tab[i]: dp[i][0] = min(dp[i][0], dp[i-1][1]) if tab[i] > -bound: dp[i][1] = dp[i-1][2] if tab[i-1] < tab[i]: dp[i][1] = min(dp[i][1], dp[i - 1][0]) if tab[i-1] > -bound: dp[i][2] = dp[i-1][1] + 1 dp[i][2] = min(dp[i][2], dp[i-1][3] + 1) if tab[i-1] < bound: dp[i][3] = dp[i-1][0] + 1 dp[i][3] = min(dp[i][3], dp[i-1][2] + 1) return min(dp[-1]) how_many = int(input()) print(solve([int(x) for x in input().split()])) |