
题目
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 Specification:
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 $(≤2×10^5)$ 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 Specification:
For each test case you should output the median of the two given sequences in a line.
Sample Input:
4 11 12 13 14
5 9 10 15 16 17
Sample Output:
13
最初的想法是合并把两个序列记录到一个数组里面,然后sort一下,取适当的pos,但是大部分的点都超时了。
第二种想法就是,先读入序列A,然后动态读序列B,记录cnt的位置值,如果与中位数的pos相等则输出值。这里主要考虑两种情况,(1)读完序列B后仍未发现中位数,(2)读序列B的过程中发现中位数。对于前一种情况,需要对剩下的序列A继续遍历寻找。
另外要注意的是,还要考虑一种情况,例如:
2 1 1
3 2 2 2
这种情况,序列A的所有值都比序列B的某个值小,此时V[x] < t一直成立,然后x++一直进行,会产生越界的情况。
1 |
|




近期评论