Codeforces Round #196 (Div. 2)
来源:互联网 发布:淘宝开女装店去哪进货 编辑:程序博客网 时间:2024/05/17 23:21
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 hern 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 off1 pieces, the second one consists off2 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. LetA be the number of pieces in the largest puzzle that the teacher buys andB be the number of pieces in the smallest such puzzle. She wants to choose suchn puzzles thatA - B is minimum possible. Help the teacher and find the least possible value ofA - B.
The first line contains space-separated integers n andm (2 ≤ n ≤ m ≤ 50). The second line containsm space-separated integers f1, 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<algorithm>using namespace std;int main(){ int n,m,i,c; cin>>n>>m; int a[m]; for(i=0;i<m;i++) cin>>a[i]; sort(a,a+m); c=a[n-1]-a[0]; for(i=0;i<=m-n;i++) { if(a[i+n-1]-a[i]<=c) c = a[i+n-1]-a[i]; } cout<<c; return 0;}总结:C++中使用 != 做循环终结标志很容易考虑不全而出错, 使用 < 做边界判断才行
- Codeforces Round #196 (Div. 2)
- Codeforces Round #196 (Div. 2)
- Codeforces Round #196 (Div. 2)
- Codeforces Round #196 (Div. 2)
- Codeforces Round #196 (Div. 2)
- Codeforces Round #196 (Div. 2)
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #196 (Div. 2) C. Quiz
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #196 (Div. 2) C. Quiz
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- rails
- 找出字符串中,指定的相同子串的数量
- android.view.ViewRootImpl$CalledFromWrongThreadException: UI线程和子线程(工作线程)冲突
- 给Ubuntu 13.04换源 软件源、更新源
- 有源晶振 串电阻
- Codeforces Round #196 (Div. 2)
- VB6给MDI窗口(父窗口)动态绘制背景的种种方法
- hdu 3040 Happy Girls(模拟)
- 有序列表的归并算法
- Java科普之算法剖析
- python+Tornado + Supervisor + nginx部署
- VS2010下配置ACE
- 图书
- MySQL出错代码