ZOJ 3278 8G Island(二分)

来源:互联网 发布:淘宝关于退货运费规则 编辑:程序博客网 时间:2024/06/02 01:17
ZOJ - 3278
8G Island
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

Description

8g Island has N boys and M girls. Every person has a "charm value". When a boy with charm value t1 and a girl with charm value t2 get together, their "8g value" is t1*t2. Every year the king will choose the Kth greatest number from all the N*M possible 8g values as the lucky number of the year. People on the island knows nothing but 8g, so they ask you to help them find the lucky number.

Input

The input contains multiply test cases(<= 10). Each test case contains three parts:

    1 Three integers NM(1 <= N,M <= 100000) and K(1 <= K <= N*M) in a line, as mentioned above.

    N integers in a line, the charm value of each boy.

    M integers in a line, the charm value of each girl.

    All the charm values are integers between 1 and 100000(inclusive).

Process to the end-of-file.

Output

For each test case print a single line that contains the lucky number.

Sample Input

3 2 31 2 31 22 2 11 11 12 2 41 11 1

Sample Output

311


题目大意:

有两个数组,a[n],b[m], 求第K小a[i]*b[j]。

解题思路:

因为N,M的数量级是10^5 。所以不能算出所有的乘积再去找第K大。 那么想到第K大,是一个有序表。那么可以用二分。

首先二分一个答案X.单调性易证。

对于答案X,判断方法是:判断有多少个比他大的,将B数组排序,枚举A[I],二分a[i]*b[j] >= X。

#include <cstdio>#include <algorithm>#include <vector>#include <algorithm>#include <iostream>#include <string>#define LL long longusing namespace std;LL a[100000+1],b[100000+1];LL n,m,k;int cmp(int a,int b){    return a > b;}bool judge(LL x){    LL ok = 0;    for(int i = 1;i <= n;i++)    {        LL l = 1,r = m;        LL res = 0;        while(l <= r)        {            LL mid = (l+r)>>1;            if(a[i]*b[mid] >= x)            {                res = mid;                l = mid+1;            }            else                r = mid-1;        }        ok += res;    }    return ok >= k;}int main(){    while(scanf("%lld %lld %lld",&n,&m,&k) != EOF)    {        for(int i = 1;i <= n;i++) scanf("%lld",&a[i]);        for(int i = 1;i <= m;i++) scanf("%lld",&b[i]);        sort(b+1,b+m+1,cmp);        sort(a+1,a+n+1,cmp);        judge(1);        LL l = a[n]*b[m],r = a[1]*b[1];        LL ans = l;        while(l <= r)        {            LL mid = (l+r)>>1;            if(judge(mid))//判断大于mid的个数            {                l = mid+1;                ans = mid;            }            else            {                r = mid-1;            }        }        printf("%lld\n",ans);    }    return 0;}

0 0
原创粉丝点击