AcWing 154. 滑动窗口 python
原题链接
简单
作者:
申侠
,
2020-11-03 14:53:08
,
所有人可见
,
阅读 250
n, k = map(int, input().split())
lt = list(map(int, input().split()))
q = [0] * 1000100
hh = 0
tt = -1
for i in range(n):
if hh <= tt and i-k+1>q[hh]:
hh += 1
while hh <= tt and lt[q[tt]] > lt[i]:
tt -= 1
tt += 1
q[tt] = i
if i >= k-1:
print(lt[q[hh]],end=' ')
print()
hh = 0
tt = -1
for i in range(n):
if hh <= tt and i-k+1>q[hh]:
hh += 1
while hh <= tt and lt[q[tt]] < lt[i]:
tt -= 1
tt += 1
q[tt] = i
if i >= k-1:
print(lt[q[hh]],end=' ')