最多 O(log n) 次查询找出中值。

You are interested in analyzing some hard-to-obtain data from two separate databases. Each database contains n numerical values—so there are 2n values total—and you may assume that no two values are the same. You’d like to determine the median of this set of 2n values, which we will define here to be the nth smallest value.

  However, the only way you can access these values is through queries to the databases. In a single query, you can specify a value k to one of the two databases, and the chosen database will return the kth smallest value that it contains. Since queries are expensive, you would like to compute the median using as few queries as possible.

  Give an algorithm that finds the median value using at most O(log n) queries.

您想分析两个独立数据库中一些难以获得的数据。每个数据库包含 n 个数值,因此总共有 2n 个数值,您可以假设没有两个数值是相同的。您想确定这组 2n 个数值的中位数,我们在此将其定义为第 n 个最小值。

  但是,访问这些值的唯一方法是查询数据库。在一次查询中,您可以向两个数据库中的一个指定一个值 k,所选数据库将返回其包含的第 k 个最小值。由于查询的成本很高,因此你希望使用尽可能少的查询来计算中位数。

  请给出一种算法,用最多 O(log n) 次查询找出中值。

#include<iostream>
const int N=1e6+10;

int findIntwoBase(int a[],int b[],int s1,int e1,int s2,int e2,int k)
{
//    start end  //k表示要找的第k小
    if(k==1)
    {
        return min(a[s1],b[s1]);
    }
    int mid1=s1+k/2-1;
    int mid2=s2+k-k/2-1;
    if(a[mid1]<a[mid2])
        return findIntwoBase(a,b,s1+k/2,e1,s2,s2+k-k/2,k-k/2);
    else
        return findIntwoBase(a,b,s1,s1+k/2,s2+k-k/2,e2,k/2);
    
}
int main()
{
    int n;
    cin>>n;
    int a[N],b[N];
        int k;
    cin>>k;
    for(int i=0;i<0;i++)
        cin>>a[i];
     for(int i=0;i<0;i++)
        cin>>b[i];
findIntwoBase(a,b,0,n,0,n,k);
    return 0;

}

助记:a数组永远和k/2有关,b数组:mid1=s1+k/2-1、(大)findInTwoBase(,s1+k/2,e1,k/2

b数组永远和k-k/2有关,b数组:mid1=s2+k-k/2-1、(mid2大)findInTwoBase(s2,s2+k-k/2,k-k/2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熟人看不到

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值