以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 算法理论与分析 』  (http://bbs.xml.org.cn/list.asp?boardid=60)
----  [讨论]一道北大CS考研题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=60&rootid=&id=32735)


--  作者:Logician
--  发布时间:5/20/2006 9:53:00 AM

--  [讨论]一道北大CS考研题
有两个(递增)有序数组A和B,长度均为n。为简单起见,不妨设这2n个数全是互异的整数。
设计一个算法,能在O(log n)时间内找到两个数组“合并后的中数”。即,假设这2n个数已经归并成了一个长度为2n的有序数组C(C[0],...,C[2n-1])。 “合并后的中数”x=(C[n-1]+C[n])/2。输出这个x。
--  作者:phoenixinter
--  发布时间:5/20/2006 4:55:00 PM

--  
这个是原题么?感觉O(logn)不大可能的说
--  作者:Logician
--  发布时间:5/20/2006 5:19:00 PM

--  
是回忆题。O(log n)这里应该没错。
因为原题还要求证明log n阶是最优的(这个当然很简单)。
互异的条件是我加的……
--  作者:phoenixinter
--  发布时间:5/20/2006 9:15:00 PM

--  
Say the two arrays are sorted and increasing, namely A and B.
It is easy to find the median of each array in O(1) time.
Assume the median of array A is m and the median of array B is n.
Then,
1' If m=n, then clearly the median after merging is also m, the algorithm holds.
2' If m<n, then reserve the half of sequence A in which all numbers are greater than
   m, also reserve the half of sequence B in which all numbers are smaller than n.
   Run the algorithm on the two new arrays.
3' If m>n, then reserve the half of sequence A in which all numbers are smaller than
   m, also reserve the half of sequence B in which all numbers are larger than n.
   Run the algorithm on the two new arrays.

Time complexity: O(logn)


--  作者:Logician
--  发布时间:5/21/2006 3:25:00 AM

--  
nod.
好办法。
^_^
--  作者:carroty
--  发布时间:6/14/2006 2:11:00 PM

--  
#include <iostream>

using namespace std ;

int main(){

 cout<<"Enter size of the array:";

 int size;
 cin>>size;

 int * a= new int[size];
 int * b= new int[size];

 cout<<"Enter array A:";
 for(int i=0;i<size;i++)
  cin>>a[i];

 cout<<"Enter array B:";
 for(i=0;i<size;i++)
  cin>>b[i];

 if(a[0]>b[size-1]){
  cout<<(a[0]+b[size-1])/2<<endl;
  delete [] a;
  delete [] b;
  return 0;
 }
 if(b[0]>a[size-1]){
  cout<<(b[0]+a[size-1])/2<<endl;
  delete [] a;
  delete [] b;
  return 0;
 }

 int as,bs,ae,be;
 as=bs=0;
 ae=be=(size-1);
 while(1)
 {
  if((as+1)==ae)break;
  if(a[(as+ae)/2]>b[(size-1)-(as+ae)/2]){
   ae=(as+ae)/2;
  }
  else if(a[(as+ae)/2]<b[(size-1)-(as+ae)/2]){
   as=(as+ae)/2;
  }
 }
 int tmp_min=(a[ae]<b[(size-1)-as])?a[ae]:b[(size-1)-as];
 int tmp_max=(a[as]>b[(size-1)-ae])?a[as]:b[(size-1)-ae];

 cout<<(tmp_min+tmp_max)/2<<endl;
 delete [] a;
 delete [] b;
 return 0;
}


--  作者:Supremgoooo
--  发布时间:6/14/2006 4:57:00 PM

--  

赞大家!!
--  作者:toniee
--  发布时间:7/10/2006 8:05:00 PM

--  
好!
--  作者:liustream
--  发布时间:7/17/2006 5:54:00 PM

--  
善!
长见识了
--  作者:flykk
--  发布时间:12/19/2006 5:07:00 PM

--  
int tmp_min=(a[ae]<b[(size-1)-as])?a[ae]:b[(size-1)-as];
int tmp_max=(a[as]>b[(size-1)-ae])?a[as]:b[(size-1)-ae]
是不是反了
还有能不能把算法的原理说明一下

--  作者:xiaoyou8519
--  发布时间:7/26/2008 11:42:00 PM

--  
无语
--  作者:lzyzsd
--  发布时间:10/3/2008 9:37:00 AM

--  
我觉得用归并排序应该可以,把这两个数组看作是归并排序第一步拆出的两个数组即可吧
--  作者:hust512
--  发布时间:2/26/2009 11:16:00 PM

--  
下次再看,走过留痕!
--  作者:scdxjch
--  发布时间:2/21/2011 11:24:00 AM

--  
今年的考研题~
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.012ms