PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000049. Trung vị cửa sổ trượt có số âm (Sliding Window Median)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 numsnums of nn integers, possibly negative, and window size kk, consider all consecutive windows of exactly kk elements.

After sorting a window, define its median as the middle value if kk is odd, or the arithmetic mean of its two middle values if kk is even. Thus the result may end in .5.5. Equal values are separate occurrences.

Print the medians from left to right. Each numeric answer is accepted with absolute error at most 10−510^{-5}.

Input

  • Line 1: integers n,kn,k, array length and window length.
  • Line 2: nn integers nums1,nums2,…,numsnnums_1,nums_2,\ldots,nums_n.

Output

Print n−k+1n-k+1 real numbers on one line, one window median each, with absolute error at most 10−510^{-5}.

Subtasks

Subtask 1 (20 points):

  • 1≤k≤n≤201\le k\le n\le 20; −231≤numsi≤231−1-2^{31}\le nums_i\le 2^{31}-1 for every 1≤i≤n1\le i\le n.

Subtask 2 (30 points):

  • 1≤k≤n≤20001\le k\le n\le 2000; −231≤numsi≤231−1-2^{31}\le nums_i\le 2^{31}-1 for every 1≤i≤n1\le i\le n.

Subtask 3 (50 points):

  • 1≤k≤n≤1000001\le k\le n\le 100000; −231≤numsi≤231−1-2^{31}\le nums_i\le 2^{31}-1 for every 1≤i≤n1\le i\le n.

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 [−1,1,3][-1,1,3], [−3,−1,3][-3,-1,3], [−3,−1,5][-3,-1,5], [−3,3,5][-3,3,5], [3,5,6][3,5,6], and [3,6,7][3,6,7]. Their middle elements are 1,−1,−1,3,5,61,-1,-1,3,5,6, written in decimal notation.

Examples 2

Input

4 2
1 4 -2 5

Output

2.5 1.0 1.5

Explanation

For k=2k=2, the median is the average of both values: (1+4)/2=2.5(1+4)/2=2.5, (4−2)/2=1.0(4-2)/2=1.0, and (−2+5)/2=1.5(-2+5)/2=1.5.

The windows [1,4][1,4], [4,−2][4,-2], and [−2,5][-2,5] sort to [1,4][1,4], [−2,4][-2,4], and [−2,5][-2,5]. Because the window size is 22, the median is the arithmetic mean of both elements rather than the lower one alone.