ZCMU 1540 第k大数 (二分)

来源:互联网 发布:算法导论第二版答案 编辑:程序博客网 时间:2024/05/22 14:55



时间限制: 10 Sec  内存限制: 128 MB
提交: 74  解决: 22
[提交][状态][讨论版]

题目描述

有两个序列a,b,它们的长度分别为n和m,那么将两个序列中的元素对应相乘后得到的n*m个元素从大到小排列后的第k个元素是什么?

输入

输入的第一行为一个正整数T (T<=10),代表一共有T组测试数据。

每组测试数据的第一行有三个正整数n,m和k(1<=n, m<=100000,1<=k<=n*m),分别代表a序列的长度,b序列的长度,以及所求元素的下标。第二行为n个正整数代表序列a。第三行为m个正整数代表序列b。序列中所有元素的大小满足[1,100000]。

输出

对于每组测试数据,输出一行包含一个整数代表第k大的元素是多少。

样例输入

3
3 2 3
1 2 3
1 2
2 2 1
1 1
1 1
2 2 4
1 1
1 1

样例输出

3
1
1


【题意】

n*m 从大到小排序, 为第k个数是多少

【思路】

虽然时间给出10s  但 暴力扔会超时,  

数据太多,  通过用 二分 将时间复杂度降下来,  

二分 最大 a[n]*b[m]    到最小 a[1]*b[1] 直接的 答案;

再通过统计 比mid 大得个数 与 k  比较 进行  缩或扩


【代码实现】


//#include <bits/stdc++.h>#include <iostream>#include <stdio.h>#include <algorithm>#include <cmath>#include <math.h>#include <cstring>#include <string>#include <queue>#include <deque>#include <stack>#include <stdlib.h>#include <list>#include <map>#include <set>#include <bitset>#include <vector>#define mem(a,b) memset(a,b,sizeof(a))#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define FIN      freopen("input.txt","r",stdin)#define FOUT     freopen("output.txt","w",stdout)#define S1(n)    scanf("%d",&n)#define SL1(n)   scanf("%I64d",&n)#define S2(n,m)  scanf("%d%d",&n,&m)#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)#define Pr(n)     printf("%d\n",n)#define lson rt << 1, l, mid#define rson rt << 1|1, mid + 1, r#define  FI(n) IO::read(n)#define  Be IO::begin()using namespace std;typedef long long ll;const double PI=acos(-1);const int INF=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e5+5;const int MAXN=50005;const int MOD=1e9+7;const int mod=1e9+7;int dir[5][2]={0,1,0,-1,1,0,-1,0};namespace IO {const int MT = 5e7;char buf[MT]; int c,sz;void begin(){c = 0;sz = fread(buf, 1, MT, stdin);//Ò»´ÎÐÔÊäÈë}template<class T>inline bool read(T &t){while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;if( c>=sz) return false;bool flag = 0; if( buf[c]== '-') flag = 1,c++;for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';if(flag) t=-t;return true;}}ll inv[maxn*2];inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}inline ll inv2(ll b){return qpow(b,MOD-2);}ll n,k,m;ll a[maxn],b[maxn];int judge(ll mid){    int j=1;    int  sum=0;    for(int i=n;i>=1;i--)    {        while(j<=m)        {            if(a[i]*b[j]>=mid)            {                sum+=m-j+1;                break;            }            else            {                j++;            }        }    }    return sum;}int main(){    int T;    cin>>T;    while(T--)    {        scanf("%lld %lld %lld",&n,&m,&k);        for(int i=1;i<=n;i++)            scanf("%lld",&a[i]);        for(int i=1;i<=m;i++)            scanf("%lld",&b[i]);        sort(a+1,a+n+1);        sort(b+1,b+m+1);        ll kl=a[1]*b[1];// ×îС         ll kr=a[n]*b[m];//×î´ó        ll ans=0;        while(kl<=kr)        {            ll mid= (kl+kr)>>1;            int  sum=judge(mid);            if(sum>=k)// ±Èk´ó  ´ð°¸ÔÚÓÒ ÒªËõ             {                ans=mid;               // cout<<ans<<endl;                kl=mid+1;            }            else// À©             {                kr=mid-1;            }        }        printf("%lld\n",ans);    }    return 0;}/*55 2 81 2 3 4 51 2*/


123