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

#BS0000154. Tìm k phần tử gần nhất (Find K Closest Elements)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tìm k phần tử gần nhất (Find K Closest Elements)

Find K Closest Elements

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given a nondecreasing sequence of nn integers, an integer kk, and a target xx, select exactly kk array elements closest to xx. Elements are compared by absolute distance to xx; if distances tie, the smaller value wins. Equal values at different indices count separately. Print the chosen values in nondecreasing order.

Input

  • The first line contains n,k,xn,k,x.
  • The second line contains nn values a1,…,ana_1,\ldots,a_n in nondecreasing order.

Output

Print exactly kk selected integer values, sorted in nondecreasing order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le40; 1≤k≤n1\le k\le n; −100≤ai,x≤100-100\le a_i,x\le100 for all ii; ai≤ai+1a_i\le a_{i+1}.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤k≤n1\le k\le n; −5000≤ai,x≤5000-5000\le a_i,x\le5000 for all ii; ai≤ai+1a_i\le a_{i+1}.
  • Subtask 3 (50 points): 1≤n≤1041\le n\le10^4; 1≤k≤n1\le k\le n; −104≤ai,x≤104-10^4\le a_i,x\le10^4 for all ii; ai≤ai+1a_i\le a_{i+1}.

Examples

Example 1

Input

5 4 3
1 2 3 4 5

Output

1 2 3 4

Explanation

For x=3x=3, the distances of 1,2,3,4,51,2,3,4,5 are 2,1,0,1,22,1,0,1,2. The three closest are 3,2,43,2,4. For the last place, both 11 and 55 have distance 22, but 11 wins the smaller-value tie-break. Sorted output is 1 2 3 4.