1552 - Median

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

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.

<br />

题目输入

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.

<br />

题目输出

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


输入/输出样例

输入格式

4 11 12 13 14
5 9 10 15 16 17

输出格式

13

C语言解答

#include <stdio.h>
int main()
{
  while(1)
  {
     long n1;
     long n2;
     long A[1000000];
     long B[1000000];
     if(scanf("%ld",&n1)==EOF)
     {
       break;
     }
     for(int i=0;i<n1;i++)
     {
       scanf("%ld",&A[i]);
     }
     scanf("%ld",&n2);
     for(int i=0;i<n2;i++)
     {
       scanf("%ld",&B[i]);
     }
     
     long min=(n1+n2)/2;
     if((n1+n2)%2!=0)
     {
       min=min+1;
     }
     long count=0;
     long ap=0;
     long bp=0;
     int flag=0;
     while(count<min&&ap<n1&&bp<n2)
     {
       while(flag==0&&ap<n1&&bp<n2&&A[ap]<B[bp])
       {
         count++;
         if(count>=min)
         {
           flag=1;
           break;
         }
          ap++;
         
       }
       while(flag==0&&ap<n1&&bp<n2&&A[ap]>=B[bp])
       {
         count++;
         if(count>=min)
         {
           flag=2;
           break;
         }
         bp++;
         
         
       }
     }
     if(ap<n1&&count<min)
       {
         
          ap--;
           while(count<min)
           {
             ap++;
             count++;
             flag=1;
           }
         
       }
       if(bp<n2&&count<min)
       {
         bp--;
          while(count<min)
          {
           bp++;
           count++;
            flag=2;
         }
       }
     if(flag==1)
       {
         printf("%ld\n",A[ap]);
       }
       if(flag==2)
       {
         printf("%ld\n",B[bp]);
       }
     
  }
}

C++解答

#include <stdio.h>

int a[1100000], b[1100000];	// 定义储存两列序列的数组,由于元素很多,所以使用全局变量来存储

int main(){

    int m;		// 第一个序列的元素数目
    while(scanf("%d", &m) != EOF){
        for(int i=0;i<m;i++){	// 读入第一个序列中的元素
            scanf("%d", &a[i]);
        }
        int n;	// 第二个序列中的元素数目
        scanf("%d", &n);
        for(int i=0;i<n;i++){ // 读入第二个序列中的元素
            scanf("%d", &b[i]);
        }
        int t = (m+n-1)/2;	// 在总的序列中中间的那个数前面有多少元素
        int i=0, j=0;
        while(t--){	// 定位到中间的那个数
            if(a[i]<b[j] && i<m || j>=n){
                i++;
            }else{
                j++;
            }
        }
        printf("%d\n",a[i]<b[j] && i<m ? a[i] : b[j]); // 输出结果
    }

    return 0;
}