ZOJ 3278 8G Island(二分)

ZOJ - 3278
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

 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. 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


题目大意:

有两个数组,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 long
using 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值