#Q177. 「一本通 5.5 例 1」滑动窗口

「一本通 5.5 例 1」滑动窗口

Description

Original Source: POJ 2823

Given an array of length NN, a sliding window of size KK moves from the far left to the far right. You can only see the KK numbers in the window at any time. Each time the window moves one position to the right, as shown below:

Window Position Minimum Maximum
[1 3 -1] -3 5 3 6 7\texttt{[1 3 -1] -3 5 3 6 7} 1-1 33
  1 [3 -1 -3] 5 3 6 7 \ \texttt{ 1 [3 -1 -3] 5 3 6 7} 3-3
  1 3 [-1 -3 5] 3 6 7 \ \texttt{ 1 3 [-1 -3 5] 3 6 7} 55
  1 3 -1 [-3 5 3] 6 7 \ \texttt{ 1 3 -1 [-3 5 3] 6 7}
  1 3 -1 -3 [5 3 6] 7 \ \texttt{ 1 3 -1 -3 [5 3 6] 7} 33 66
  1 3 -1 -3 5 [3 6 7] \ \texttt{ 1 3 -1 -3 5 [3 6 7]} 77

Your task is to determine the maximum and minimum values in the sliding window at every position.

Input Format

Line 1: Two integers NN and KK;
Line 2: NN integers representing the elements of the array (2×109≤2\times 10^9);

Output Format

The first line contains the minimum values as the sliding window moves from left to right, with each number separated by a space;
The second line contains the maximum values as the sliding window moves from left to right, with each number separated by a space.

Sample 1

8 3
1 3 -1 -3 5 3 6 7

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Constraints & Hints

For 20%20\% of the data, KN1000K≤N≤1000;
For 50%50\% of the data, KN105K≤N≤10^5;
For 100%100\% of the data, KN106K≤N≤10^6.