Codeforence 337A Puzzles
来源:互联网 发布:unity3d手游开发教程 编辑:程序博客网 时间:2024/06/05 16:32
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.
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.
Print a single integer — the least possible difference the teacher can obtain.
4 610 12 10 7 5 22
5
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.
求出排好序的数之间那些数的差最小就是了,这样就可以获得最小的数了
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){ int i,j,k; int n,m; int num[100]; int num2[100]; scanf("%d%d",&n,&m); for(i=0;i<m;i++) { scanf("%d",&num[i]); } sort(num,num+m); /*for(i=0;i<m;i++) { printf("%d\n",num[i]); }*/ int h=m-n; //int h1=0; //printf("hhhhhhhhhhhh %d\n",h); /* while(h>=0) { printf("%d %d\n",num[h1+n-1],num[h1]); num2[h1]=num[h1+n-1]-num[h1]; h1++; h--; }*/ for(i = 0; i <= m-n; i++) { num2[i]=num[i+n-1]-num[i]; //printf("%d\n",num2[i]); } //printf("%d\n",h); sort(num2,num2+h+1); for(i=0;i<h;i++) { //printf("hhh %d\n",num2[i]); } printf("%d\n",num2[0]); return 0;}
- Codeforence 337A Puzzles
- CodeForces 337A Puzzles
- CodeForces 337A - Puzzles
- codeforces 337A Puzzles
- Nineteen Codeforence 393A
- Codeforence 393A
- Codeforence 352A
- Codeforence 402 A. Nuts
- A. Puzzles
- Codeforence A. Slightly Decreasing Permutations
- CF 337A(Puzzles-暴力枚举)
- CCF 337A(Puzzles) 水dp
- codeforces 337A(Puzzles) Java
- CodeForces -337A:Puzzles(水)
- Codeforence 400 A. Inna and Choose Options
- Codeforence 258A. Little Elephant and Bits
- CF A. Dynasty Puzzles
- Codeforces Round #196 (Div. 2) / 337A Puzzles (搜索)
- 第四周作业——图的表示
- 一步步学习微软InfoPath2010和SP2010--第十章节--与SharePoint Designer工作流的InfoPath集成(4)--工作流任务表单
- jpa+spring配置多数据源
- linux fork 函数目的
- Spring 2.5 注释驱动的 IoC 功能
- Codeforence 337A Puzzles
- java中ConcurrentLinkedQueue类
- USC 1143: 显示器
- 第29天
- 导入android工程后出现The resource is not on the build path of a java project
- 证书安装
- java 字节流与字符流的区别
- px、dp和sp,这些单位有什么区别?
- ARM GNU 汇编宏定义