4875 k th big number (dichotomy)

Title Description
There are two sequences a and b, whose lengths are n and m, respectively. What is the k-th element after multiplying the elements in the two sequences from large to small?

input
The first act of input is a positive integer T (T<=10), representing a total of T groups of test data.

The first row of each set of test data has three positive integers n, m and K (1<=n, m<=100000, 1<=k<=n*m), representing the length of a sequence, the length of b sequence, and the subscripts of the elements. The second action n positive integers represent sequence a. The third behavior m positive integers represent sequence b. The size of all elements in the sequence satisfies [1,100,000].

output
For each set of test data, the output line contains an integer representing the number of elements larger than k.

Sample input Copy
3
3 2 3
1 2 3
1 2
2 2 1
1 1
1 1
2 2 4
1 1
1 1
Sample output Copy
3
1
1

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
#include<bits/stdc++.h>
#include<stack>
#include<functional>
#include<cstring>
#include<set>
#include<strstream>
#include<vector>
#define mod 1000000007
#define mst(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=100000+8;
ll a[maxn];
//int dp[maxn][maxn];
//int s[maxn][maxn];
int f[maxn];
ll b[maxn];
int v[maxn]={0,12,8,9,5};
int w[maxn]={0,15,10,12,8};
int x[maxn];
int n,m;
ll k;
vector<int>vec;

ll cmp(ll a, ll b)
{
    return a>b;
}


ll judge(ll x)//Number of statistics >= x
{
    ll sum=0,j=m-1;
    for(int i=0;i<n;i++)
    {
        while(j&&a[i]*b[j]<x)j--;
        if(a[i]*b[j]>=x)sum+=j+1;
    }
    return sum;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll res=0;
        scanf("%d%d%lld",&n,&m,&k);
        for(int i=0;i<n;i++)
        {
            scanf("%lld", &a[i]);
        }
        for(int j=0;j<m;j++)
        {
            scanf("%lld", &b[j]);
        }
        sort(a,a+n,cmp);
        sort(b,b+m,cmp);
        ll lf=a[n-1]*b[m-1];
        ll rt=a[0]*b[0];
        while(lf<=rt)
        {
            ll mid=(lf+rt)/2;
            if(judge(mid)>=k)
            {
                res=mid;
                lf=mid+1;
            }
            else
            {
                rt=mid-1;
            }
        }
        cout<<res<<endl;
        //cout<<lf<<endl;

    }
    return 0;
}

Posted on Tue, 08 Oct 2019 07:58:03 -0700 by Dracolas