两个排序数组求中值

来源:互联网 发布:mac ndk环境变量 编辑:程序博客网 时间:2024/05/18 20:49
http://blog.csdn.net/abcjennifer/article/details/7291758
题目描述:

    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 non-decreasing 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.

输入:

    Each input file may contain more than 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.

输出:

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

样例输入:
4 11 12 13 145 9 10 15 16 17
样例输出:

13


解法一:

[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "stdio.h"  
  3. #include "math.h"  
  4. #include "map"  
  5. #include "vector"  
  6. #include "queue"  
  7. #include "memory.h"  
  8. #include "algorithm"  
  9. #include "string"  
  10. using namespace std;  
  11. #define N 1000005  
  12. #define INF 1<<29  
  13. #define max(a,b) a>b?a:b  
  14.   
  15. int a[N],b[N];  
  16. int array3[N];  
  17.   
  18. int MergeArray(int la,int ha,int lb,int hb)  
  19. {  
  20.     int i,j,x;  
  21.     i=la,j=lb,x=0;  
  22.     while(i<=ha&&j<=hb)  
  23.     {  
  24.         if(a[i]>b[j])   array3[x]=b[j++];  
  25.         else    array3[x]=a[i++];  
  26.         x++;  
  27.     }  
  28.     while(i<=ha) array3[x++]=a[i++];  
  29.     while(j<=hb)array3[x++]=b[j++];  
  30.     x--;  
  31.     return array3[x/2];  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     int m,n,i,j;  
  37.     while(scanf("%d",&n)!=EOF)  
  38.     {  
  39.         for(i=0;i<n;i++)          
  40.             scanf("%d",&a[i]);  
  41.         cin>>m;  
  42.         for(j=0;j<m;j++)  
  43.             scanf("%d",&b[j]);  
  44.         int la,lb,ha,hb,ma,mb;  
  45.         la=lb=0;ha=n-1;hb=m-1;  
  46.         int res;  
  47.         int len=1;  
  48.         while(len>0)  
  49.         {  
  50.             ma=(la+ha)>>1;mb=(lb+hb)>>1;  
  51.             if(a[ma]==b[mb])  
  52.             {  
  53.                 res=a[ma];  
  54.                 break;  
  55.             }  
  56.             if(a[ma]>b[mb])  
  57.             {  
  58.                 len=min(ha-ma,mb-lb);  
  59.                 ha-=len;  
  60.                 lb+=len;  
  61.             }  
  62.             else if(a[ma]<b[mb])  
  63.             {  
  64.                 len=min(ma-la,hb-mb);  
  65.                 la+=len;  
  66.                 hb-=len;           
  67.             }  
  68.         }  
  69.         if(a[ma]==b[mb])  
  70.         {  
  71.             printf("%d\n",res);  
  72.             continue;  
  73.         }  
  74.         res=MergeArray(la,ha,lb,hb);  
  75.         printf("%d\n",res);  
  76.     }  
  77. }  

或者,其实这道题目不进行main中的那个优化也行的。
[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "stdio.h"  
  3. #include "math.h"  
  4. #include "map"  
  5. #include "vector"  
  6. #include "queue"  
  7. #include "memory.h"  
  8. #include "algorithm"  
  9. #include "string"  
  10. using namespace std;  
  11. #define N 1000005  
  12. #define INF 1<<29  
  13. #define max(a,b) a>b?a:b  
  14.   
  15. int a[N],b[N];  
  16. int array3[N];  
  17.   
  18. int MergeArray(int la,int ha,int lb,int hb)  
  19. {  
  20.     int i,j,x;  
  21.     i=la,j=lb,x=0;  
  22.     while(i<=ha&&j<=hb)  
  23.     {  
  24.         if(a[i]>b[j])   array3[x]=b[j++];  
  25.         else    array3[x]=a[i++];  
  26.         x++;  
  27.     }  
  28.     while(i<=ha) array3[x++]=a[i++];  
  29.     while(j<=hb)array3[x++]=b[j++];  
  30.     x--;  
  31.     return array3[x/2];  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     int m,n,i,j;  
  37.     while(scanf("%d",&n)!=EOF)  
  38.     {  
  39.         for(i=0;i<n;i++)          
  40.             scanf("%d",&a[i]);  
  41.         cin>>m;  
  42.         for(j=0;j<m;j++)  
  43.             scanf("%d",&b[j]);  
  44.         int la,lb,ha,hb,ma,mb;  
  45.         la=lb=0;ha=n-1;hb=m-1;  
  46.         int res;  
  47.           
  48.         res=MergeArray(la,ha,lb,hb);  
  49.         printf("%d\n",res);  
  50.     }  
  51. }  


解法2:(利用algorithm 中的nth_element())

[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "stdio.h"  
  3. #include "math.h"  
  4. #include "map"  
  5. #include "vector"  
  6. #include "queue"  
  7. #include "memory.h"  
  8. #include "algorithm"  
  9. #include "string"  
  10. using namespace std;  
  11. #define N 1000005  
  12. #define INF 1<<29  
  13. #define max(a,b) a>b?a:b  
  14.    
  15. long a[1000000];  
  16. int main()  
  17. {  
  18.        long n1,n2,i,mid;  
  19.         while(cin>>n1)  
  20.         {  
  21.               for(i=0;i<n1;i++)  
  22.                    cin>>a[i];  
  23.               cin>>n2;  
  24.               for(;i<n1+n2;i++)  
  25.                    cin>>a[i];  
  26.               mid=(i-1)/2;  
  27.               nth_element(a,a+mid,a+i);  
  28.               cout<<a[mid]<<endl;  
  29.          }  
  30.     return 0;