Problem Description
Given a set of N numbers, we want to select R*C numbers from it, and filled the selected numbers into a R*C matrix(R rows with C numbers in each row).
We define D(i) as the difference between the largest and the smallest number in the i-th row of the matrix(1<=i<=R). For the entire matrix, we define F as the largest D(i) of the R rows.
The value F of the resulting matrix is expected to be as small as possible. Can you help us to tell what the smallest possible F is?
Input
The first line of the input contains a positive integer T<=20 specifying the number of test cases to follow.
The first line of each test case contains 3 positive integers N,R,C. The following line contains positive integers Pi.
For all test cases: 1<=R,C<=10^4, R*C<=N<=5*10^5,0<Pi<=10^9
Output
For each test case you should output a single line containing "Case X: Y",where X is the number of the test case (starting at 1). Y is the smallest possible F.
Sample Input
1
7 2 3
170 205 225 190 260 225 160
Sample Output
Given a set of N numbers, we want to select R*C numbers from it, and filled the selected numbers into a R*C matrix(R rows with C numbers in each row).
We define D(i) as the difference between the largest and the smallest number in the i-th row of the matrix(1<=i<=R). For the entire matrix, we define F as the largest D(i) of the R rows.
The value F of the resulting matrix is expected to be as small as possible. Can you help us to tell what the smallest possible F is?
Input
The first line of the input contains a positive integer T<=20 specifying the number of test cases to follow.
The first line of each test case contains 3 positive integers N,R,C. The following line contains positive integers Pi.
For all test cases: 1<=R,C<=10^4, R*C<=N<=5*10^5,0<Pi<=10^9
Output
For each test case you should output a single line containing "Case X: Y",where X is the number of the test case (starting at 1). Y is the smallest possible F.
Sample Input
1
7 2 3
170 205 225 190 260 225 160
Sample Output
Case 1: 30
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define Max 500010
using namespace std;
int data[Max],n,r,c;
int erfen(int low,int high)
{
int mid,i,ans=0,cnt,left=low,right=high;
while(left<=right)
{
cnt=0;
mid=(left+right)/2;
for(i=1;i+c-1<=n;)
{
if(data[i+c-1]-data[i]<=mid)
{
cnt++;
i+=c;
}
else i++;
}
if(cnt>=r)
{
right=mid-1;
ans=mid;
}
else left=mid+1;
}
return ans;
}
int main()
{
int cases,i,j;
scanf("%d",&cases);
for(i=1;i<=cases;i++)
{
scanf("%d %d %d",&n,&r,&c);
for(j=1;j<=n;j++)
scanf("%d",&data[j]);
sort(data+1,data+n+1);
printf("Case %d: %d\n",i,erfen(0,1000000000));
}
return 0;
}