选数问题

来源:互联网 发布:汽车报价大全软件下载 编辑:程序博客网 时间:2024/06/06 06:38
Problem DescriptionGiven 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?InputThe 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^9OutputFor 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 Input17 2 3170 205 225 190 260 225 160Sample OutputCase 1: 30//题意:给你N个数,和一个R*C 的矩阵选取R*C个数放入矩阵中,每行有个最大值和一个最小值一个,一个矩阵有R个这样的值,组成了一个集合R,它们的元素分别为:R(i) = Max(i) - Min(i),从R集合中选取一个最大值,重新组成一个集合Z,我们要求的就是Z集合中的最小值.//标程:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int inf = 1000000007;int a[500005];int main(){//    freopen("a.txt","r",stdin);    int t, n, r, c, i, k = 0;    scanf("%d",&t);    while(t --)    {        ++ k;        int ans = 0;        scanf("%d%d%d",&n,&r,&c);        for(i = 0; i < n; ++ i)            scanf("%d",a+i);        sort(a,a+n);        int low = 0, high = inf, mid;        while(low <= high)        {            mid = (low + high) / 2;            int cnt = 0;            for(i = 0; i + c - 1 < n; )            {                if(a[i+c-1] - a[i] <= mid)                {                    i += c;                    cnt ++;                }                else i ++;            }            if(cnt >= r)            {                high = mid - 1;                ans = mid;            }            else low = mid + 1;        }        printf("Case %d: %d\n",k,ans);    }    return 0;}

0 0