#FT0000049. Trung vị cửa sổ trượt có số âm (Sliding Window Median)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trung vị cửa sổ trượt có số âm (Sliding Window Median)
Sliding Window Median
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array of integers, possibly negative, and window size , consider all consecutive windows of exactly elements.
After sorting a window, define its median as the middle value if is odd, or the arithmetic mean of its two middle values if is even. Thus the result may end in . Equal values are separate occurrences.
Print the medians from left to right. Each numeric answer is accepted with absolute error at most .
Input
- Line 1: integers , array length and window length.
- Line 2: integers .
Output
Print real numbers on one line, one window median each, with absolute error at most .
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
1 3 -1 -3 5 3 6 7
Output
1.0 -1.0 -1.0 3.0 5.0 6.0
Explanation
The sorted windows are , , , , , and . Their middle elements are , written in decimal notation.
Examples 2
Input
4 2
1 4 -2 5
Output
2.5 1.0 1.5
Explanation
For , the median is the average of both values: , , and .
The windows , , and sort to , , and . Because the window size is , the median is the arithmetic mean of both elements rather than the lower one alone.