题目
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:
|
|
Sample Output:
|
|
想法
把两个数组分成两个部分,得到中位数
要考虑时间复杂度和空间复杂度
emm 先写一个普通的版本:
将两个数组存下来,然后从头开始遍历,两个指针比较着向前走,直到到达一半为止
实现如下:
|
|
十分干净简单,但是最后一个测试点内存超限,而且,最后一个测试点10分,占了2/5的分数。
用的空间,也就存两个输入的数组,没有其它的空间了
这都内存超限的话,看来,不能把数据都存下来。
肯定要存输入的第一个数组。
对于第二个,必须边读入,边处理。
不存第二个,不就对了。
改进之后的版本:
|
|
用了一个buffer来存比较之后,并没有用num2的情况下的num2.
内存超限没有了,不过还有个测试点没过,而且也是10分!!!
问题出在哪里呢???
不解,实在想不到..
看来,还是,考虑的不周到,没有玄学,只是问题不明显而已,buffer没有考虑全。
答案
|
|
感想
- 找问题,仔细找,用到每个“变”量,都要审核是否符合条件
- PAT更新后的分数好迷
ios::sync_with_stdio(false)
大法好,应该不用担心cin超时了hhh