AcWing 3194. 单调栈(python)
原题链接
简单
作者:
将情怀讲泛滥的恶果
,
2021-03-21 16:44:19
,
所有人可见
,
阅读 358
if __name__=="__main__":
n=int(input())
heights=[int(x) for x in input().split(" ")]
l = [0 for x in range(0,n)]
r = [0 for x in range(0,n)]
s=[0 for x in range(0,n)]
top=-1
for i in range(0,n):
while( top >= 0 and heights[s[top]] >= heights[i]):
top-=1
if top==-1:
l[i] = 0
else:
l[i] = s[top]+1
top+=1
s[top]=i
s=[0 for x in range(0,n)]
top=-1
for i in range(n-1,-1,-1):
while(top>=0 and heights[s[top]] >= heights[i]):
top-=1
if top==-1:
r[i]=n
else:
r[i]=s[top]
top+=1
s[top]=i
res = -1
for i in range(0,n):
res = max(res, heights[i]*(r[i]-l[i]))
print(res)