Files
luogu/P1886.md
T
hanyixuanten c622586277
Check cpp / md pair / Check md headers (push) Successful in 4s
new file: P1886.cpp
2026-10-28 10:53:45 +00:00

51 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# P1886 【模板】单调队列 / 滑动窗口
## 题目描述
有一个长为 $n$ 的序列 $a$,以及一个大小为 $k$ 的窗口。现在这个窗口从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最小值和最大值。
例如,对于序列 $[1,3,-1,-3,5,3,6,7]$ 以及 $k = 3$,有如下过程:
$$\def\arraystretch{1.2}
\begin{array}{|c|c|c|}\hline
\textsf{窗口位置} & \textsf{最小值} & \textsf{最大值} \\ \hline
\verb![1 3 -1] -3 5 3 6 7 ! & -1 & 3 \\ \hline
\verb! 1 [3 -1 -3] 5 3 6 7 ! & -3 & 3 \\ \hline
\verb! 1 3 [-1 -3 5] 3 6 7 ! & -3 & 5 \\ \hline
\verb! 1 3 -1 [-3 5 3] 6 7 ! & -3 & 5 \\ \hline
\verb! 1 3 -1 -3 [5 3 6] 7 ! & 3 & 6 \\ \hline
\verb! 1 3 -1 -3 5 [3 6 7]! & 3 & 7 \\ \hline
\end{array}
$$
## 输入格式
输入一共有两行,第一行有两个正整数 $n,k$;\
第二行有 $n$ 个整数,表示序列 $a$。
## 输出格式
输出共两行,第一行为每次窗口滑动的最小值;
第二行为每次窗口滑动的最大值。
## 输入输出样例 #1
### 输入 #1
```
8 3
1 3 -1 -3 5 3 6 7
```
### 输出 #1
```
-1 -3 -3 -3 3 3
3 3 5 5 6 7
```
## 说明/提示
【数据范围】
对于 $50\%$ 的数据,$1 \le n \le 10^5$;
对于 $100\%$ 的数据,$1\le k \le n \le 10^6$,$a_i \in [-2^{31},2^{31})$。