题目:给定一个长度为n,由非零整数成的数组,求连续子数组乘积为负数的个数example:
55 -3 3 -1 178
ChatGPT的答案,基本正确,有个地方多+1了
n = int(input())arr = list(map(int, input().split()))dp1, dp2 = [0] * (n+1), [0] * (n+1)for i in range(1, n+1): if arr[i-1] > 0: dp1[i] = dp1[i-1] + 1 dp2[i] = dp2[i-1] else: dp2[i] = dp1[i-1] + 1 dp1[i] = dp2[i-1]print(sum(dp1))print(sum(dp2))