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. java程序员级别划分

    IT路虽好,却难走.1级   为会基本语法 大学里的JAVA教程 能及格 2级   自己可以写个 俄罗斯方块,扫雷,贪吃蛇, 拼图之类的小游戏 3级   能够进手机游戏CP,SP公司,做手机游戏 或者 ...

  2. AI 的会议总结(by南大周志华)

    原文链接:http://blog.csdn.net/akipeng/article/details/6533897 这个列的更详细:http://www.cvchina.info/2010/08/31 ...

  3. Js 字符串中提取数字

    一 parseInt()方法: 首先想到的是js提供的parseInt方法,例子: var str ="4500元"; var num = parseInt(str); alert ...

  4. Apex语言(四)选择(决策)结构

    1.选择结构 选择结构是当满足某个条件或不满足某个条件时,需要进行决策以控制执行的流程. 2.if语句 if语句由布尔表达式后跟一个或多个语句组成. [格式] if(条件表达式){ 语句: } [流程 ...

  5. spring cloud(五) hystrix

    开启feign 熔断 hystrix    整合hystrix-dashboard监控面板 1. 服务调用者boot工程 pom引入依赖 <!-- hystrix-dashboard 监控依赖 ...

  6. PAT_A1152#Google Recruitment

    Source: PAT A1152 Google Recruitment (20 分) Description: In July 2004, Google posted on a giant bill ...

  7. PAT_A1118#Birds in Forest

    Source: PAT A1118 Birds in Forest (25 分) Description: Some scientists took pictures of thousands of ...

  8. [CodeForces1059E] Split the Tree

    树形DP. 用倍增处理出来每个点往上能延伸出去的最远路径,nlogn 对于每个节点,如果它能被后代使用过的点覆盖,就直接覆盖,这个点就不使用,否则就ans++,让传的Max改成dp[x] #inclu ...

  9. Linux系统学习之 二:新手必须掌握的Linux命令2

    2018-10-03 22:20:48 一.文件目录管理命令 1.touch 命令 用于创建空白文件或设置文件的时间,格式为“touch [选项] [文件]”. 参数: -a :仅修改“读取时间(at ...

  10. 在MySQL的表中增加一列

    MySql中增加一列 如果想在一个已经建好的表中添加一列,可以用: alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(45) not ...