递归分治问题之找出两个有序序列的中间值
2017-01-13 23:39
267 查看
问题描述:
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.
解题思路:
首先,我们比较这两个序列的中间值,设第一个序列D1,中间值m1;设第二个序列,D2,中间值m2,且两个序列长度一样均为n。当第一个序列的中值m1>m2时,则说明两个序列融合后的中值一定处在D1序列的D1[n/2...n-1]和D2序列的D2[0...n/2-1]。我们把D1[n/2...n-1]作为新的D1,把D2[0...n/2-1]作为新的D2。继续上述的判断运算。直至最后的D1和D2都只有1个元素,比较找到较大(或较小或平均值),即为两个序列结合后的中值。
pseudo-code:
Merge for two datasets
p1=p2=n/2;
for(i=2 ... log2(n))//因为进行的是二元搜索,共进行的是log2(n)
m1=query(D1,p1);
m2=query(D2,p2);
if(m1>m2)
{
p1=p1-n/2^i;
p2=p2+n/2^i;
}
else
{
p1=p1+n/2^i;
p2=p2-n/2^i;
}
return min(m1,m2);
时间复杂度:因为执行的递归分治方案,时间复杂度o(nlogn)
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.
解题思路:
首先,我们比较这两个序列的中间值,设第一个序列D1,中间值m1;设第二个序列,D2,中间值m2,且两个序列长度一样均为n。当第一个序列的中值m1>m2时,则说明两个序列融合后的中值一定处在D1序列的D1[n/2...n-1]和D2序列的D2[0...n/2-1]。我们把D1[n/2...n-1]作为新的D1,把D2[0...n/2-1]作为新的D2。继续上述的判断运算。直至最后的D1和D2都只有1个元素,比较找到较大(或较小或平均值),即为两个序列结合后的中值。
pseudo-code:
Merge for two datasets
p1=p2=n/2;
for(i=2 ... log2(n))//因为进行的是二元搜索,共进行的是log2(n)
m1=query(D1,p1);
m2=query(D2,p2);
if(m1>m2)
{
p1=p1-n/2^i;
p2=p2+n/2^i;
}
else
{
p1=p1+n/2^i;
p2=p2-n/2^i;
}
return min(m1,m2);
时间复杂度:因为执行的递归分治方案,时间复杂度o(nlogn)
相关文章推荐
- [分治递归]解决最大子序列和问题
- 统计工龄 模拟EXCEL排序 银行排队问题之单队列多窗口问题 银行业务队列简单模拟 堆栈操作合法性 两个有序序列的中位数
- 两个有序序列中找出公共序列
- 数据结构_中国大学MOOC(慕课)——两个有序链表序列的合并问题
- 算法练习4.Median of Two Sorted Arrays两个有序数组的中位数(递归、分治)
- C语言 最长子序列问题(两个序列的公共子序列)
- 两个有序链表的合并问题
- 递归分治解决全排列问题
- C++递归问题之二——n皇后问题:以四、八皇后为例,给定n个皇后要求将它们放在一个n维矩阵中,任意两个皇后不能出现在同一行、列、主副对角线上,输出具体的摆放方式
- 实验一 分治与递归―棋盘覆盖问题 java实现
- 字典序问题-递归与分治
- 合并两个有序序列
- 合并两个有序序列
- 两个经典递归问题:菲波那契数列 + 汉诺塔
- 算法面试题:找出由两个有序列表合并而成的新列表中的第n个元素
- 递归实现合并两个有序链表
- 两个有序整形数组找出二者相同的元素和不同的元素
- 两个有序链表合并递归实现及非递归实现
- 两个有序的整型数组,求最大的共有整数序列。
- 找出和值等于指定值的元素,如有序数组{1,2,6,23,43,64},求和等于25的两个和数则将是元素2和23,时间复杂度是0(n)。