当前位置:编程学习 > C/C++ >>

CF#196DIV2:A-Puzzles

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 integers f1, 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.


 

题意:给出n,m,n是学生数,m是礼物数,在m哥礼物中 找出n个礼物,是的最大与最小值得差最小

思路:由于数据小,直接排序暴力

 

 

#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;
}

 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,