Codeforces Round #196 (Div. 2) A. Puzzles water Problem

Source: Internet
Author: User

A. PuzzlesTime limit:2 Sec Memory limit:60 MB Topic Connection

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5373

Description

The end of the school year was near and Ms Manana, the teacher, would soon has to say goodbye to a yet another class. She decided to prepare a goodbye present for hers 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 te Ssellating pieces).

The shop assistant told the teacher that there is m puzzles in the shop, but they migh T differ in difficulty and size. Specifically, the first jigsaw puzzle consists of F 1 pieces, The second one consists of F 2 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 pre Sents must be as small as possible. Let A is the number of pieces in the largest puzzle so the teacher buys and B be the number of Pieces in the smallest such puzzle. She wants to choose such n puzzles, A - B is minimum possible. Help the teacher and find the least possible value of A - B.

Inputthe first line contains space-separated integers n and m (2≤ nm ≤50). The second line contains m space-separated integers f 1, F2, ..., f m (4≤ fi ≤1000)-the quantities of pieces in the puzzles sold in the shop.OutputPrint A single integer-the least possible difference the teacher can obtain.Sample Input
4 6
10 12 10 7 5 22

Sample OutputvHINT

Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms Manana buys the first four puzzles consisting of ten, and 7 pieces correspondingly, then the difference betwe En the sizes of the largest and the smallest puzzle would be equal to 5. It's impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

Test instructions

Give you the number of N, and then let you choose M, so that the maximum value of the m number minus the minimum value of this value, the minimum

Ask what the minimum value is.

Exercises

Just shoot the order, and then the violence.

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 2001#defineMoD 10007#defineEPS 1e-9//const int INF=0X7FFFFFFF; //infinitely LargeConst intinf=0x3f3f3f3f;/**///**************************************************************************************inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intA[MAXN];BOOLcmpintCintd) {    returnC>D;}intMain () {intN=read (), m=read ();  for(intI=0; i<m;i++) A[i]=read (); intMi=inf; Sort (A,a+m,cmp);  for(intI=0; i+n-1<m;i++) Mi=min (a[i]-a[i+n-1],mi); printf ("%d\n", MI);}

Codeforces Round #196 (Div. 2) A. Puzzles water Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.