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)