codeforces 337A
来源:互联网 发布:动态网页 数据库 编辑:程序博客网 时间:2024/06/16 23:49
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 integersf1, 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 610 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.
水题,排序,最大与最小的肯定是连续的n个
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int INF=0x3f3f3f;typedef long long LL;int main(){int n,m;int a[1050];while(~scanf("%d%d",&n,&m)){for(int i=1;i<=m;i++){scanf("%d",&a[i]);}sort(a+1,a+m+1);int minv=INF;for(int i=1;i<=m-n+1;i++){int sum=0;int maxn=-1;int minx=INF;for(int j=i;j<=n+i-1;j++){minx=min(minx,a[j]);maxn=max(maxn,a[j]);}minv=min(minv,maxn-minx);}printf("%d\n",minv);}return 0;}
- codeforces---#196 337A
- CodeForces 337A Puzzles
- CodeForces 337A - Puzzles
- codeforces 337A
- codeforces 337A Puzzles
- CodeForces-a
- Codeforces 903A A
- Codeforces Round #337 (Div. 2) A.
- codeforces 337A(Puzzles) Java
- CodeForces -337A:Puzzles(水)
- CodeForces 18A A - Triangle
- CodeForces 133A A. HQ9+
- codeforces 651A A. Joysticks
- 【Codeforces 597A】A. Divisibility
- 【 Codeforces 615A】A. Bulbs
- A. Divisibility CodeForces 597A
- codeforces 417A A. Elimination
- CodeForces 534A A. Exam
- 提交MapReduce任务时,Ecplise自动生成Jar包
- 对象转换
- CSS3学习笔记(一)--2015-12-3
- android开发中bitmap图片占用内存计算方法
- 《C++ 笔记》 Part1 文件打开时的CString 转换为 char*
- codeforces 337A
- 关于大型网站技术演进的思考(八)--存储的瓶颈终篇(8)
- 自学ios之路(3)
- 使用ZooKeeper实现Redis订阅消息的排他式处理
- MFC CListCtrl不响应LVN_GETDISPINFO消息
- php curl
- 笔记 - Java 反射
- python类中的__call__、__init__、__getattr__、__setattr__、__setattr__详解
- 读《从〈机器猫〉看阶级斗争残酷的本质》