321
社区成员




给定n个柱面的高度,表示降雨某地n块区域的海拔高度。 计算降雨之后该地最大储水面积。如果低于地平线,也就是小于0,则一定积水
这是题目里带图?抄题都没抄好的最佳证据之一。
总之呢,这个题目貌似和 leetcode第11题玩法一模一样了。嗯,多了个初始海平面高度0。
n = int(input())
arr = list(map(int,input().split()))
ans,l,r,lh,rh = 0,0,n - 1,0,0
while l <= r:
if arr[l] >= arr[r]:
rh = max(rh,arr[r])
ans += rh - arr[r]
r -= 1
elif arr[l] < arr[r]:
lh = max(lh,arr[l])
ans += lh - arr[l]
l += 1
print(ans)