CodeForces 337A Puzzles
Puzzles
This problem will be judged on CodeForces. Original ID: 337A
64-bit integer IO format: %I64d Java class name: (Any)
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.
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.
Sample Input
4 6
10 12 10 7 5 22
5
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.
Source
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int d[maxn],n,m;
int main() {
scanf("%d %d",&n,&m);
for(int i = ; i < m; ++i)
scanf("%d",d+i);
sort(d,d+m);
int ret = INT_MAX;
for(int i = + n - ; i < m; ++i)
ret = min(ret,d[i] - d[i-n+]);
printf("%d\n",ret);
return ;
}
CodeForces 337A Puzzles的更多相关文章
- codeforces A. Puzzles 解题报告
题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- 【数学相关、规律】Codeforces 696B Puzzles
题目链接: http://codeforces.com/problemset/problem/696/B 题目大意: 给一棵树,从根节点开始递归,time=1,每次递归等概率随机访问这个节点的子节点, ...
- CodeForces - 697D - Puzzles DFS
传送门:D - Puzzles 题意:在一个图中,从1开始dfs,求每一个点到达的期望: 思路:(下面是队长写的) 首先求的是到每一个点的步数的期望. 记fa( u ) = v, son( v )表示 ...
- codeforces 696B Puzzles 树形概率计算
题意:给一棵有根树,从根节点深搜,每次随机走,问每个点的dfs序的期望是多少 分析:对于每一个点,它的所有祖先节点dfs序肯定在它之前,它所在的子树的节点一定在它后面, 剩下的是既不是子树又不是祖先的 ...
- [水题]Codeforces337A Puzzles
题目链接 题意:要在m个数里面选n个数, 要求这n个数的差值要最小 题意在hint里很清晰了 这道题从题意到题目本身都没有什么trick 写这道题完全是为了用一下#include <numeri ...
- 【codeforces 696B】 Puzzles
http://codeforces.com/problemset/problem/696/B (题目链接) 题意 给出一棵树,随机dfs遍历这棵树,求解每个节点的期望dfs序. Solution 考虑 ...
- Codeforces 191A - Dynasty Puzzles - [DP]
题目链接:https://codeforces.com/problemset/problem/191/A 题意: 给出 $n$ 个小写字母组成的字符串,两个字符串如果前者的最后一个字母与后者的首字母相 ...
随机推荐
- 解码URLDecode和编码URLEnCode
在前台往后台传递参数的时候,在前台进行编码,在后台接收参数的时候,用Decode进行解码: 如果url中包含特殊字符如:&.html标签 <tr><td>等导致url无 ...
- SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...
- CDR都特价了,你还用破解版?
目前,CDR X6特价活动,从18的年的元旦一直持续到现在,真可谓是痛快淋漓,大喜若狂,但是,似乎好像是....这么强有力的活动马上就要告一段落了,因为限量1000 套的抢购要卖!完!了!难不难过,痛 ...
- [洛谷P3939]数颜色
题目大意:有n个物品,每个物品有一个颜色.现在有两种操作:1.查询l-r内有多少颜色为c的物品并输出.2.将第x个物品和第x+1个交换.现在让你实现这些操作. 解题思路:首先一共有300000种颜色, ...
- /application/nginx/sbin/nginx -h
[root@web03 ~]# /application/nginx/sbin/nginx -h nginx version: nginx/1.6.3Usage: nginx [-?hvVtq] [- ...
- 如何在ssh远程linux服务器时不需要输入密码
目的: 期望A服务器在对B服务器执行ssh或者scp等命令的时候不需要输入密码 实现方法: 1.通过安装sshpass服务 2.通过密钥验证的方式 操作过程: 一.通过sshpass的方式达到密码非交 ...
- 详解:(cron , crontab , anacron)
导读: 人类把时间做了切割,想象一条笔直的线永远向前,本来这条直线上什么都没有,但是人类根据时间的长短(单位)在这条直线上做了密密麻麻的标记(世纪-年-月-日-时-分-秒-纳秒......),通过这样 ...
- CentOS的基本设置界面
系统的基本设置,如语言.键盘鼠标.时间.网络.壁纸.通知等功能的设置 高级设置:如磁盘分区.系统日志.各种系统分析工具
- C语言变量名转字符串的方法
C语言中将变量名转换成字符串使用的是#符号,来看下边两个宏定义: #define name2str(name) (#name) #define print_val(x) printf(#x" ...
- CAD二次开发(02)-添加对象到模型空间
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...