a789: Puzzles ( 拼圖 )
Tags : 900 greedy sortings
Accepted rate : 25人/26人 ( 96% ) [非即時]
評分方式:
Tolerant

最近更新 : 2022-07-29 20:55

Content

The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).

The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.

Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.

學年快結束了,老師 Ms. Manana 很快就要和另一個班級說再見了。她決定為她的 n 名學生準備一份告別禮物,並給他們每個人一個拼圖遊戲(正如維基百科所述,這是一個拼圖遊戲,需要組裝許多小的、通常形狀奇特的、互鎖和鑲嵌的碎片)。

店員告訴老師,店裡有 m 個拼圖,但難度和大小可能不一樣。具體來說,第一個拼圖由 f1 塊組成,第二個由 f2 塊組成,依此類推。

Ms. Manana 不想讓孩子們不高興,所以她決定禮物中的塊數差異必須盡可能小。令 A 為老師購買的最大拼圖中的塊數,B 為最小拼圖的塊數。她想選擇 A - B 是最小可能的 n 個謎題。幫助老師找到A - B的最小可能值。

Input

The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integers f1, f2, ..., fm (4 ≤ fi ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.

第一行包含空格分隔的整數 n 和 m (2 ≤ n ≤ m ≤ 50)。 第二行包含 m 個空格分隔的整數 f1, f2, ..., fm (4 ≤ fi ≤ 1000) — 商店出售的拼圖的數量。

Output

Print a single integer — the least possible difference the teacher can obtain.

打印一個整數 — 教師可以獲得的最小可能差異。

Sample Input #1
4 6
10 12 10 7 5 22
Sample Output #1
5
測資資訊:
記憶體限制: 64 MB
公開 測資點#0 (4%): 0.4s , <1K
公開 測資點#1 (4%): 0.4s , <1K
公開 測資點#2 (4%): 0.4s , <1K
公開 測資點#3 (4%): 0.4s , <1K
公開 測資點#4 (4%): 0.4s , <1K
公開 測資點#5 (4%): 0.4s , <1K
公開 測資點#6 (4%): 0.4s , <1K
公開 測資點#7 (4%): 0.4s , <1K
公開 測資點#8 (4%): 0.4s , <1K
公開 測資點#9 (4%): 0.4s , <1K
公開 測資點#10 (4%): 0.4s , <1K
公開 測資點#11 (4%): 0.4s , <1K
公開 測資點#12 (4%): 0.4s , <1K
公開 測資點#13 (4%): 0.4s , <1K
公開 測資點#14 (4%): 0.4s , <1K
公開 測資點#15 (4%): 0.4s , <1K
公開 測資點#16 (4%): 0.4s , <1K
公開 測資點#17 (4%): 0.4s , <1K
公開 測資點#18 (4%): 0.4s , <1K
公開 測資點#19 (4%): 0.4s , <1K
公開 測資點#20 (4%): 0.4s , <1K
公開 測資點#21 (4%): 0.4s , <1K
公開 測資點#22 (4%): 0.4s , <1K
公開 測資點#23 (4%): 0.4s , <1K
公開 測資點#24 (4%): 0.4s , <1K
Hint :

Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

示例 1. 班級有 4 名學生。 商店出售 6 個拼圖。 如果 Ms. Manana 分別購買了 10、12、10 和 7 塊組成的前四個拼圖,那麼最大和最小拼圖的塊數之差將等於 5。不可能得到更小的差值。 注意,老師也可以購買拼圖1、3、4、5來獲得差值5。

Tags:
900 greedy sortings
出處:
Codeforces Round #196 [管理者:
lamkinun@gma... (Kinda Lam)
]


ID User Problem Subject Hit Post Date
沒有發現任何「解題報告」