Cf #196div2: A-puzzles

Source: Internet
Author: User

Http://codeforces.com/contest/337/problem/A

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 herNStudents 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 areMPuzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consistsF1
Pieces, the second one consistsF2 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. LetABe the number of pieces in the largest puzzle that
Teacher buys andBBe the number of pieces in the smallest such puzzle. She wants to choose suchNPuzzles that
AAccept-Encoding-BIs minimum possible. Help the teacher and find the least possible valueAAccept-Encoding-B.

Input

The first line contains space-separated IntegersNAndM(2 cores ≤ CoresNLimit ≤ limitMLimit ≤ limit 50). The second line containsM
Space-separated IntegersF1, bytes,F2, middle..., middle ,...,FM(4 cores ≤ CoresFILimit ≤00001000)
-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 test (s) Input
4 610 12 10 7 5 22
Output
5
Note

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, m, n are the number of students, M is the number of gifts, find n gifts in the M brother gift, is the maximum and minimum difference is the minimum

Train of Thought: because the data is small, direct sorting of violence

 

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main(){    int n,m,i,a[55],Min,s;    while(~scanf("%d%d",&m,&n))    {        for(i = 0; i<n; i++)            scanf("%d",&a[i]);        sort(a,a+n);        Min = 10000;        m = m-1;        for(i = m; i<n; i++)        {            s = a[i]-a[i-m];            if(s<Min)                Min = s;        }        printf("%d\n",Min);    }    return 0;}

 

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.