#P1886. 【模板】单调队列 / 滑动窗口
【模板】单调队列 / 滑动窗口
Description
There is a sequence of length and a window of size . The window slides from left to right, moving one position at a time. After each slide, find the minimum and maximum values within the window.
For example, for the sequence and , the process is as follows:
$$\def\arraystretch{1.2} \begin{array}{|c|c|c|}\hline \textsf{Window position} & \textsf{Minimum} & \textsf{Maximum} \\ \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}$$Input Format
The input has two lines. The first line contains two positive integers .
The second line contains integers representing the sequence .
Output Format
Output two lines. The first line contains the minimum of the window after each slide.
The second line contains the maximum of the window after each slide.
8 3
1 3 -1 -3 5 3 6 7
-1 -3 -3 -3 3 3
3 3 5 5 6 7
Hint
[Constraints]
For 50% of the testdata, .
For 100% of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号