Median

来源:互联网 发布:暴风影音播放器mac版 编辑:程序博客网 时间:2024/04/26 16:58

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

Given two increasing sequences of integers, you are asked to find their median.

Input

Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (<=1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

Output

For each test case you should output the median of the two given sequences in a line.

Sample Input
4 11 12 13 145 9 10 15 16 17
Sample Output
13


#include <iostream>#include <fstream>#include <vector>using namespace std;long find_recursion(long* v1,long b1,long e1,long* v2,long b2,long e2,long num){  if(e1 < b1)    return v2[b2 + num - 1];  if(e2 < b2)    return v1[b1 + num - 1];  long m1 = (b1 + e1) / 2;  long m2 = (b2 + e2) / 2;  if(v1[m1] <= v2[m2])  {    if(num <= m1 - b1 + 1 + m2 - b2)      return find_recursion(v1,b1,e1,v2,b2,m2 - 1,num);    else      return find_recursion(v1,m1 + 1,e1,v2,b2,e2,num - (m1 - b1 + 1));  }  else  {    if(num <= m2 - b2 + 1 + m1 - b1)      return find_recursion(v1,b1,m1 - 1,v2,b2,e2,num);    else      return find_recursion(v1,b1,e1,v2,m2 + 1,e2,num - (m2 - b2 + 1));  }}long find(long* v1,long* v2,int n1,int n2,long num)//寻找第num小的数{  return find_recursion(v1,0,n1 - 1,v2,0,n2 - 1,num);}int main(){  int n1,n2;  scanf("%d",&n1);  long* v1 = new long[n1];  for(long i = 0;i < n1;++ i)    scanf("%ld",v1 + i);  scanf("%d",&n2);  long* v2 = new long[n2];  for(long i = 0;i < n2;++ i)    scanf("%ld",v2 + i);  long value = find(v1,v2,n1,n2,(n1 + n2 + 1) / 2);  cout << value;}


原创粉丝点击