G-Christmas play

Source: Internet
Author: User

Description

My kid's kindergarten class is putting up a Christmas play. (I hope he gets the lead role .) the kids are all excited, but the teacher has a lot of work. she has to produce costumes for a scene with K soldiers. she wants to buy all the costumes in the same size, allowing for some small amount of length alteration to be done by the kids 'Parents later. so she has taken all the kids 'height measuremen Ts. can you help her select K kids from her class of N to play the soldier role, such that the height difference between the tallest and shortest in the group is minimized, and alternations will be easiest? Tell her what this minimum difference is.
 
 
Input
The first line contains the number of test cases T. T test cases follow each containing 2 lines.
 
The first line of each test case contains 2 integers N and K.
The second line contains N integers denoting the height of the N kids.
 
Output
Output t lines, each line containing the required answer for the corresponding test case.
 
Consttraints
T <= 30
1 <= k <= n <= 20000
1 <= height <= 1000000000
 
Sample Input
3
3 1
2 5 4
3 2
5 2 4
3 3
2 5 4
 
 
Sample output
0
1
3
 
Explanation
In the first test case, the teacher needs to only select 1 kid and hence she can choose any kid since the height difference is going to be 0.
In the second test case, the teacher can choose kids with height 4 and 5.
In the third test case, the teacher is forced to choose all 3 kids and hence the answer = 5-2 = 3

 

Question: 3 2 The interval between three people is 2
5, 2, 4, the minimum height difference between two
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;int main(){    int t,n,d;    int a[20002];    while(~scanf("%d",&t))        while(t--)    {        scanf("%d%d",&n,&d);        for(int i=0; i<n; i++)            scanf("%d",&a[i]);        sort(a,a+n);        int sum=1000000001;        for(int i=0; i<n-d+1; i++)        {            //cout<<a[i]<<‘ ‘<<a[i+d-1]<<"!!!!!!!!"<<endl;            if(sum>(a[i+d-1]-a[i]))                sum=(a[i+d-1]-a[i]);        }       printf("%d\n",sum);    }    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.