PAT(甲级)1029

来源:互联网 发布:淘宝新店如何提升信誉 编辑:程序博客网 时间:2024/05/17 07:31

1029. Median (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 <cstdio>#include <cstring>#define SIZE 1000006long a[SIZE];long b[SIZE];long c[2*SIZE];void Merge(long c[],long a[],long &num1,long b[],long &num2){long i,j,k;i=j=k=0;while(i<num1&&j<num2){if(a[i]<b[j]){c[k++]=a[i++];}else    c[k++] = b[j++];}while(i<num1)    c[k++] = a[i++];while(j<num2)    c[k++] =b[j++];}int main(){memset(a,0,sizeof(long)*SIZE);memset(b,0,sizeof(long)*SIZE);memset(c,0,sizeof(long)*2*SIZE);long i,NUM1,NUM2;scanf("%ld",&NUM1);for(i=0;i<NUM1;i++)    scanf("%ld",&a[i]);scanf("%ld",&NUM2);for(i=0;i<NUM2;i++)    scanf("%ld",&b[i]);Merge(c,a,NUM1,b,NUM2);if((NUM1+NUM2)&0x1)    printf("%ld\n",c[(NUM1+NUM2)/2]);else    printf("%ld\n",c[(NUM1+NUM2)/2-1]);return 0;}

版本二:

#include <cstdio>#include <cstdlib>#define SIZE 1000006long a[SIZE];long b[SIZE];//can ignore the auxiliaray array to minimize the storage//long c[2*SIZE];/*void Merge(long c[],long a[],long &num1,long b[],long &num2){long i,j,k;i=j=k=0;while(i<num1&&j<num2){if(a[i]<b[j]){c[k++]=a[i++];}else    c[k++] = b[j++];}while(i<num1)    c[k++] = a[i++];while(j<num2)    c[k++] =b[j++];}*/int main(){//memset(a,0,sizeof(long)*SIZE);//memset(b,0,sizeof(long)*SIZE);long i,NUM1,NUM2,index;scanf("%ld",&NUM1);//long *a=(long *)calloc(NUM1,sizeof(long));for(i=0;i<NUM1;i++)    scanf("%ld",&a[i]);scanf("%ld",&NUM2);//long *b=(long *)calloc(NUM2,sizeof(long));for(i=0;i<NUM2;i++)    scanf("%ld",&b[i]);if((NUM1+NUM2)&0x1)            //determine the location    index=(NUM1+NUM2)/2+1;else    index=(NUM1+NUM2)/2;long j,count=0;i=j=0;bool flag,found=false;while(i<NUM1&&j<NUM2){if(a[i]<b[j]){count++;if(count==index){flag=true;found=true;index=i;break;}i++;}else{count++;if(count==index){flag=false;found = true;index=j;break;}j++;}}if(found){if(flag)    printf("%ld\n",a[index]);else    printf("%ld\n",b[index]);}else if(i <NUM1){index = index-count+i-1;printf("%ld\n",a[index]);}else{index = index-count+j-1;printf("%ld\n",b[index]);}      return 0;}


0 0