#FT0000039. Trung vị cửa sổ trượt (Sliding Window Median)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trung vị cửa sổ trượt (Sliding Window Median)
Sliding Window Median
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers and window length , consider all contiguous windows of exactly elements, starting at positions through .
To define a window median, sort its values in nondecreasing order. For odd , take the middle element. For even , take the smaller of the two middle elements (the element of rank , counting from ). Equal values remain separate occurrences.
Output each window median from left to right.
Input
- Line 1: integers , array length and window length.
- Line 2: integers .
Output
Print integers on one line: the window medians, from left to right.
Subtasks
Subtask 1 (20 points):
- ; for every .
Subtask 2 (30 points):
- ; for every .
Subtask 3 (50 points):
- ; for every .
Examples
Examples 1
Input
8 3
2 4 3 5 8 1 2 1
Output
3 4 5 5 2 1
Explanation
The six sorted windows are , , , , , . Their middle values are respectively .
Examples 2
Input
4 2
9 2 6 2
Output
2 2 2
Explanation
With window size , choose the smaller value in each pair: , , and .
The three windows are , , and . Their sorted forms are , , and . For even , the task requires the lower median, which is the first element of each sorted pair; all three outputs are therefore .