1954: Alban's New Year Embellishment
题目描述
In the distant East, Mr. Alban is a wealthy landowner who owns a vast garden. This garden is filled with various types of trees, which not only showcase the beauty of nature throughout the seasons but also become the most important decoration during the Chinese New Year celebrations every year.
Today is the Chinese New Year's Eve, and all the family members have gathered to celebrate the arrival of the new year. To make this holiday warmer and more festive, Mr. Alban decided to select K trees from N trees (where 2 <= K < N <= 10^5) from his garden and decorate them with colorful lanterns and traditional red ornaments, lighting up the entire garden. Mr. Alban hopes that the heights of the K trees he selects will be as similar as possible, because the smaller the height difference, the more harmonious and beautiful the decoration will be. However, in Mr. Alban's garden, the tree heights vary widely—some trees are tens of meters tall, while others are only a few meters tall. Specifically, he wants the maximum height of the selected trees, denoted as Hmax, and the minimum height of the selected trees, denoted as Hmin, to have the smallest possible difference: Hmax - Hmin.
He is unsure how to solve this problem, so he invites you to help him. How can he select the K trees such that the height difference between the tallest and shortest tree is minimized?
输入
The first line contains two integers N and K (2 <= K < N <= 10^5), where N is the total number of trees and K is the number of trees to be selected.
The following N lines contain integers representing the heights of the N trees.
输出
样例输入
5 3
10
12
11
13
15
样例输出
2
提示
2 <= K < N <= 10^5
1 <= hi <= 10^9 (Each tree height is an integer)