CF#196DIV2:A-Puzzles

來源:互聯網
上載者: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 hern 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 off1
pieces, the second one consists off2 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. LetA be the number of pieces in the largest puzzle that the
teacher buys andB be the number of pieces in the smallest such puzzle. She wants to choose suchn puzzles that
A - B is minimum possible. Help the teacher and find the least possible value ofA - B.

Input

The first line contains space-separated integers n andm (2 ≤ n ≤ m ≤ 50). The second line containsm
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 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是學生數,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;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.