CF337A.Puzzles

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 _f_1 pieces, the second one consists of _f_2 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.

学年即将结束,曼纳老师很快就要和又一届学生告别了。她决定为她的 nn 名学生每人准备一份告别礼物——一幅拼图(据维基百科介绍,拼图是一种镶嵌类益智游戏,需要将大量细小、形状各异、相互咬合且能密铺的碎片拼合在一起)。

商店售货员告诉老师,店里共有 mm 幅拼图,但它们在难度和尺寸上可能各不相同。具体而言,第一幅拼图包含 f1f_1 块碎片,第二幅包含 f2f_2 块碎片,依此类推。

曼纳老师不想让学生们感到不快,因此她希望所选礼物中拼图碎片数量的最大差值尽可能小。设 AA 为老师所购 nn 幅拼图中碎片数最多的那幅的碎片数量,BB 为其中碎片数最少的那幅的碎片数量。她希望选出 nn 幅拼图,使得 A−BA - B 尽可能小。请帮助老师找出 A−BA - B 的最小可能值。

输入格式

The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integers _f_1, _f_2, ..., f__m (4 ≤ f__i ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.

第一行包含两个用空格分隔的整数 nn 和 mm(2 ≤ n ≤ m ≤ 502 \leq n \leq m \leq 50)。第二行包含 mm 个用空格分隔的整数 f1, f2, ..., fmf_1,\,f_2,\,...,\,f_m(4 ≤ fi ≤ 10004 \leq f_i \leq 1000)——表示商店中所售拼图的块数。

输出格式

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

输出一个整数——教师所能得到的最小可能差值。

输入输出样例

  • 输入#1

    4 6
    10 12 10 7 5 22

    输出#1

    5

说明/提示

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 款拼图。若玛娜老师购买前四款拼图(其拼图片数分别为 10、12、10 和 7),则最大拼图与最小拼图的尺寸之差为 5。无法获得更小的差值。注意,老师也可以购买第 1、3、4 和 5 款拼图,从而同样得到差值 5。

输入解题思路,AI测评打分。不知道怎么写?

首页