CodeForces -337A:Puzzles(水)
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.
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, ..., f**m (4 ≤ f**i ≤ 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个元素,要求选择n个,使得其中最大值减最小值最小
解题思路:
排序。肯定就是在连续n个元素中才会出现最大值减最小值的情况,而这个值就是第n个减第1个,整个区间往右移找出最小的就可以了
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, a, c = 10001;
cin >> n >> a;
int b[a];
for (int i = 0; i < a; i++) {
cin >> b[i];
}
sort(b, b + a);
for (int j = 0; j <= a - n; j++) {
c = min(c, abs(b[j + n - 1] - b[j]));
}
cout << c;
}
CodeForces -337A:Puzzles(水)的更多相关文章
- CodeForces 337A Puzzles
Puzzles Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origina ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- Codeforces Round #196 (Div. 2) A. Puzzles 水题
A. Puzzles Time Limit: 2 Sec Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
- codeforces A. Puzzles 解题报告
题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...
- Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces 705B (训练水题)
题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...
- codeforces hungry sequence 水题
题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...
- CodeForces 705A Hulk (水题)
题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...
- 【数学相关、规律】Codeforces 696B Puzzles
题目链接: http://codeforces.com/problemset/problem/696/B 题目大意: 给一棵树,从根节点开始递归,time=1,每次递归等概率随机访问这个节点的子节点, ...
随机推荐
- JAVAweek4
本周学习: 1.JAVA环境的搭建,包括JDK的下载,和classpath环境设置 2.*.java和*.class程序的运行 class Demo {public static void main( ...
- Linux笔记03: Linux常用命令_3.2目录操作命令
3.2 目录操作命令 3.2.1 ls命令 ●命令名称:ls. ●英文原意:list directory contents. ●所在路径:/usr/bin/ls. ●执行权限:所有用户. ●功能描述: ...
- oracle、达梦数据库、MySQL数据创建表与字段注释
/**1.oracle注释*//*表本身注释*/comment on table 表名 is '注释信息';/*字段注释*/comment on column 表名.字段名 is '注释信息';/*实 ...
- 循环返回结果结果集(connect 函数使用)
--示例: SELECT 0 + ROWNUM sonID FROM DUAL /*区间范围*/ CONNECT BY ROWNUM <= 20;
- [GDOI22pj2D] 机器人
第四题 机器人 提交文件: robot.cpp 输入文件: robot.in 输出文件: robot.out 时间空间限制: 3 秒, 512 MB 刚上初一的小纯特别喜欢机器人,这周末,她报名了学校 ...
- ubuntu 22.04.1安装雷池开源waf应用防火墙
ubuntu 22.04.1安装雷池开源waf应用防火墙 雷池waf是开源应用防火墙,国内首创.业内领先的智能语义分析算法 官方网站:https://waf-ce.chaitin.cn/ 官方文档:h ...
- 有意思,我的GitHub账号值$23806.2,快来试试你的?
睡不着,看到一个有意思的网站:Estimate Github Worth Generator. 它可以用来估算 GitHub 账号的价值.马上试了一下. 我的账号估值:$23806.2 操作很简单,点 ...
- 【scikit-learn基础】--『监督学习』之 决策树回归
决策树算法是一种既可以用于分类,也可以用于回归的算法. 决策树回归是通过对输入特征的不断划分来建立一棵决策树,每一步划分都基于当前数据集的最优划分特征.它的目标是最小化总体误差或最大化预测精度,其构建 ...
- DVWA SQL Injection(SQL注入)全等级
SQL Injection(SQL回显注入) 目录: SQL Injection(SQL回显注入) 1. Low 2.Medium 3. High 4.Impossible 5.运用sqlmap自动化 ...
- 用Roslyn玩转代码之一: 解析与执行字符串表达式
最近框架中的可视化界面设计需要使用到表达式引擎(解析代码字符串并动态执行),之前旧框架的实现是将表达式字符串解析为语法树后解释执行该表达式,本文介绍如何使用Roslyn解析表达式字符串,并直接转换 ...