Codeforces Round #196 (Div. 2) A. Puzzles
来源:互联网 发布:讲文明知礼仪的故事 编辑:程序博客网 时间:2024/05/17 02:23
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 <algorithm>#include <stdio.h>using namespace std;int q[1000],p[1000],num[1000];int fmin(int a,int b){ if(a<b) return a; return b;}int fabs(int a){ if(a<0) return -a; return a;}bool cmp(int a,int b){ return a<b;}int main(){ int n,m,s,e,l,r,i,minn; while(scanf("%d%d",&n,&m)!=EOF) { minn=1000000000; s=0,e=-1,l=0,r=-1; for(i=0;i<m;i++) { scanf("%d",&num[i]); } sort(num,num+m,cmp); for(i=0;i<m;i++) { while(s<=e&&num[i]>=num[q[e]]) { e--; } q[++e]=i; while(s<=e&&i-q[s]>=n) { s++; } while(l<=r&&num[i]<=num[p[r]]) { r--; } p[++r]=i; while(l<=r&&i-p[l]>=n) { l++; } if(i>=n-1&&fabs(q[s]-p[l])<=n) minn=fmin(minn,fabs(num[q[s]]-num[p[l]])); } printf("%d\n",minn); } return 0;}
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #196 (Div. 2) / 337A Puzzles (搜索)
- CodeForces#196Div.2-A. Puzzles
- Codeforces Round #362 (Div. 2)-D. Puzzles
- (Codeforces Round #362 (Div. 2)) D. Puzzles
- Codeforces Round #362 (Div. 2)D. Puzzles
- Codeforces Round #362 (Div. 2) D Puzzles
- Codeforces Round #121 (Div. 1) A. Dynasty Puzzles
- Codeforces Round #362 (Div. 2) D. Puzzles(概率dp)
- Codeforces Round #362 (Div. 2) D. Puzzles(dfs)
- Codeforces Round #362 (Div. 2) D Puzzles (DP)
- Codeforces Round #362 (Div. 2) Problem D——Puzzles
- Codeforces Round #362 (Div. 2) D Puzzles(树形dp)
- Codeforces Round #362 (Div. 1) B Puzzles
- CodeForces Round #121 (191A) - Dynasty Puzzles
- Codeforces Round #121 (Div. 1) / 191A Dynasty Puzzles (dp&优化)
- Codeforces Round #362 (Div. 2) B - Puzzles (树上概率dp)
- CentOS查看CPU、内存、网络流量和磁盘 I/O【详细】
- UVALive 3644 X-Plosives
- UVALive 3027 Corporative Network
- RobotFramework+Selenium2环境搭建与入门实例
- eclipse hadoop开发环境配置
- Codeforces Round #196 (Div. 2) A. Puzzles
- 【黑马程序员】java I/O流浅谈
- Codeforces Round #196 (Div. 2) B. Routine Problem
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text)
- QT中新建的类继承QWidget和QMainWindow的不同之处
- 如何基于卫星地图制作矢量化电子地图
- hdu 4324
- 简单文件操作(Linux下C++实现) 打开,读取 写入 关闭
- 找回EC2的Key Pair文件