1029. Median (25)

来源:互联网 发布:怎么在淘宝买发票 编辑:程序博客网 时间:2024/04/29 15: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
分析:
(1)题目要求就是就两个数字串的中位数,分别用x,y,z表示3串数字的位置,得到Z等于中间数时候的数字即可。
(2)需要注意的是,要考虑到,可能一串字符串比完了还未找到中位数,所以要稍微分一下类。
(3)还有一个注意点,用vector时,如果申明时候用了(N),之后push就会在N之后PUSH。。。所以不要重复。。。
long int tmp;scanf("%ld",&tmp);s2.push_back(tmp);}int m;if ((n1+n2)%2==1)  m=(n1+n2)/2+1;else m=(n1+n2)/2;int x=0,y=0,z=0;long int median;while (z<m&&x<n1&&y<n2){if (s1[x]<=s2[y]){median=s1[x];x++;z++;}else {median=s2[y];y++;z++;}}if (x==n1){while (z<m){median=s2[y];y++;z++;}}else if (y==n2){while (z<m){median= s1[x];x++;z++;}}printf("%ld",median);//printf("1");return 0;}

0 0