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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章