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

A. Puzzles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print a single integer — the least possible difference the teacher can obtain.

Sample test(s)
input
4 6
10 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.

#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)