AcWing 154. 滑动窗口
原题链接
简单
作者:
HumbleFool
,
2025-04-09 21:34:27
· 重庆
,
所有人可见
,
阅读 1
q = [0] * (10 ** 6 + 10)
hh, tt = 0, -1
n, k = map(int, input().split())
a = list(map(int, input().split()))
res = []
for i in range(n):
if hh <= tt and i - k + 1 > q[hh]:
hh += 1
while hh <= tt and a[q[tt]] >= a[i]:
tt -= 1
tt += 1
q[tt] = i
if i >= k - 1:
res.append(a[q[hh]])
print(' '.join(map(str, res)))
hh, tt = 0, -1
res = []
for i in range(n):
if hh <= tt and i - k + 1 > q[hh]:
hh += 1
while hh <= tt and a[q[tt]] <= a[i]:
tt -= 1
tt += 1
q[tt] = i
if i >= k - 1:
res.append(a[q[hh]])
print(' '.join(map(str, res)))