Problem description

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 - Bis minimum possible. Help the teacher and find the least possible value of 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.

Output

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

Examples

Input

4 6
10 12 10 7 5 22

Output

5

Note

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.

解题思路:题目的意思就是从m块pizza中选择n块(n<=m),要求这n块pizza中尺寸最大与尺寸最小之差是所有选择中的最小差值。做法:先将pizza的尺寸按升序排序,然后从n-1开始依次枚举到m-1,则[i-n+1,i]构成连续排列的n块pizza的尺寸区间,接下来取其差值和最小值minc比较,依次更新最小值minc。为什么连续取n块pizza呢?因为如果去掉该区间中的1或2个元素,从其他区间中挑选1个或2个元素加入其中,此时的区间范围必将向前或向后延伸(变大),这样会使得差值增大,显然不符合条件要求。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,a[],minc=;
cin>>n>>m;
for(int i=;i<m;++i)cin>>a[i];
sort(a,a+m);
for(int i=n-;i<m;++i)
minc=min(minc,(a[i]-a[i-n+]));
cout<<minc<<endl;
return ;
}

C - Puzzles的更多相关文章

  1. codeforces A. Puzzles 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...

  2. What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?

    QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...

  3. C puzzles详解

    题目:http://www.gowrikumar.com/c/ 参考:http://wangcong.org/blog/archives/291 http://www.cppblog.com/smag ...

  4. codeforces 377A. Puzzles 水题

    A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...

  5. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  6. 《algorithm puzzles》——谜题

    这篇文章开始正式<algorithm puzzles>一书中的解谜之旅了! 狼羊菜过河: 谜题:一个人在河边,带着一匹狼.一只羊.一颗卷心菜.他需要用船将这三样东西运至对岸,然而,这艘船空 ...

  7. 《algorithm puzzles》——概述

    这个专题我们开始对<algorithm puzzles>一书的学习,这本书是一本谜题集,包括一些数学与计算机起源性的古典命题和一些比较新颖的谜题,序章的几句话非常好,在这里做简单的摘录. ...

  8. Puzzles

    Puzzles Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 thr ...

  9. [CF697D]Puzzles 树形dp/期望dp

    Problem Puzzles 题目大意 给一棵树,dfs时随机等概率选择走子树,求期望时间戳. Solution 一个非常简单的树形dp?期望dp.推导出来转移式就非常简单了. 在经过分析以后,我们 ...

  10. [cf contest697] D - Puzzles

    [cf contest697] D - Puzzles time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. 读书笔记「Python编程:从入门到实践」_4.操作列表

    4.1 遍历整个列表   4.1.1 深入地研究循环   4.1.2 在for循环中执行更多的操作   4.1.3 在for循环结束后执行一些操作  例 magicians = ['alice', ' ...

  2. Memcached 之增删改查命令

    memcache是高性能 ,分布式的内存对象缓存系统,非关系型数据库. 一.增 语法:add  key  flag  expire  length key:值的一个名字 flag:一个标志,是一个正整 ...

  3. Tomcat内存分析相关方法(jmap和mat)

    Linux环境命令行 首先,根据进程命令,获取运行的tomcat的进程ID ps aux | grep tomcat | grep java | grep bsc 在第二列可以看到进程ID 然后使用j ...

  4. Object.assign和序列/反序列

    Object.assign let testObj = { a:[1,2,4], b:{ name:'ls', school:['huf','yelu'], parent:{ father:'lili ...

  5. TextInputLayout使用时各个地方的字体颜色

    我们现在在做Android端的输入框时,要具备如下功能: 默认提示获取焦点时提示上移至输入框顶部获取焦点时输入框有提示错误时增加错误提示直接上图: 默认情况: 获取焦点时: 开始输入文字时: 有错误时 ...

  6. eas之执行sql的方式

    客户端:    1. 有返回集合:查询     //查询出DB中所有该字段的值,与其进行比较,若有相同的则报错      String sql="select CFWuliaoCode fr ...

  7. 【剑指Offer】34、第一个只出现一次的字符

      题目描述:   在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).   解题思路: ...

  8. 【剑指Offer】19、顺时针打印矩阵

      题目描述:   输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依 ...

  9. win10家庭版转专业版并激活

    之前重装win10的时候没注意,不小心装成家庭版. 本以为家庭版也没什么,后来发现这对程序员来说造成致命打击. 在系统信息页面底部点击“更改密匙”,输入win10升级产品密匙:VK7JG-NPHTM- ...

  10. 01-Linux命令基础-第01天(命令基础,软件安装与卸载、磁盘管理)

    01-   Linux初步 最早一直是单道程序设计模型的操作系统 69年贝尔实验室决定开发多道程序设计模型的操作系统 Multics计划 (失败了) x86 IA(Intel Architecture ...