SPOJ8061

Discuss Computer Science and Programming related problems

Moderators:Labib, bristy1588

HandaramTheGreat
Posts:135
Joined:Thu Dec 09, 2010 12:10 pm
SPOJ8061

Unread post by HandaramTheGreat » Mon Dec 27, 2010 1:05 pm

in which case it's getting WA?

problem statement::
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 measurements. 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
my code::

Code: Select all

#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
	int t, n, k, i, j, h[30000];
	scanf("%d", &t);
	for(i=1; i<=t; i++)
	{
		scanf("%d %d", &n, &k);
		for(j=0; j<n; j++)
		{
			scanf("%d", &h[j]);
		}
		sort(h, h+n);
		printf("%d\n", h[n-1]-h[n-k]);
	}
	return 0;
}

User avatar
Zzzz
Posts:172
Joined:Tue Dec 07, 2010 6:28 am
Location:22° 48' 0" N / 89° 33' 0" E

Re: SPOJ8061

Unread post by Zzzz » Mon Dec 27, 2010 5:27 pm

1
4 3
1 2 3 6


Correct output: 2 (if you select the students of height 1,2,3)
Every logical solution to a problem has its own beauty.
(Important: Please make sure that you have read about the Rules, Posting Permissions and Forum Language)

HandaramTheGreat
Posts:135
Joined:Thu Dec 09, 2010 12:10 pm

Re: SPOJ8061

Unread post by HandaramTheGreat » Mon Dec 27, 2010 7:40 pm

:oops: thanks :)

Hasib
Posts:238
Joined:Fri Dec 10, 2010 11:29 am
Location:খুলনা, বাংলাদেশ
Contact:

Re: SPOJ8061

Unread post by Hasib » Mon Dec 27, 2010 8:46 pm

I think it's c++. Am i ri8?
A man is not finished when he's defeated, he's finished when he quits.

User avatar
Zzzz
Posts:172
Joined:Tue Dec 07, 2010 6:28 am
Location:22° 48' 0" N / 89° 33' 0" E

Re: SPOJ8061

Unread post by Zzzz » Tue Dec 28, 2010 5:32 am

hasib.mo wrote:I think it's c++. Am i ri8?
humm...
Every logical solution to a problem has its own beauty.
(Important: Please make sure that you have read about the Rules, Posting Permissions and Forum Language)

Post Reply