ZOJ - 3278 8G Island

ZOJ - 3278 8G Island 

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*Mpossible 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. 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 3
1 2 3
1 2

2 2 1
1 1
1 1

2 2 4
1 1
1 1

Sample Output

3
1
1

【分析】有两个序列,求两个序列相乘后的第K大的数。

数据范围很大。。。只能二分,用二分的方法查找比每一个二分的数大的数的数量

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
LL a[maxn],b[maxn];
int cmp(int a,int b)
{
    return a>b;
}

int main()
{
    int n,m;
    LL k;
    while(~scanf("%d%d%lld",&n,&m,&k))
    {
        int i;
        for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
        for(i=1;i<=m;i++)
        scanf("%d",&b[i]);
        sort(a+1,a+n+1,cmp);
        sort(b+1,b+m+1,cmp);
        LL l1=a[n]*b[m],r1=a[1]*b[1],mid1;
        LL ans=l1;
        while(l1<=r1)
        {
            mid1=(l1+r1)/2;
            LL sum=0;
            for(i=1;i<=n;i++){
                int l2=1,r2=m,mid2;
                LL t=0;
                while(l2<=r2){
                    mid2=(l2+r2)/2;
                    if(a[i]*b[mid2]>=mid1)  {
                        t=mid2;
                        l2=mid2+1;
                    }
                    else
                        r2=mid2-1;
                }
                sum+=t;
            }
            if(sum>=k)  {
                ans=mid1;
                l1=mid1+1;
            }
            else
            r1=mid1-1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值