选数问题

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值