PAT Advanced 1029. Median (25) (C语言实现)
题目
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 ( $\le 2\times 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:
1
2
4 11 12 13 14
5 9 10 15 16 17
Sample Output:
1
13
思路
解题之前:
这道题一直到我做的时候,题目要求还是有一个严重的问题。这个问题就是题目中的数列中数的数据类型为long int
,长度最大为$2\times10^5$,这样的话一个这样的数列,需要的内存即为1.6MB,而题目却要求内存限制在1.5MB之内。
而这道题是不能在不记录下一个完整的数列的情况下解决的,例子很容易想出来。我确定没有什么神奇的方法之后,就在网上看别人答对的是怎么办的。
结果他们只是用int
类型做的就过了!!!就过了!
(如果你现在看到的和我上面说的不一样,就说明题目更正过来了)
解题思路:
尽管使用int
的类型,但是两个200000长度的int
数组也是1.6MB,会大于题目限制。因此本道题需要只是用一个数组记录一个数列,另一个就要逐个读取而不记录,需要on the fly的处理。
具体的方法可以使用归并排序的思路,从最小的一端排序到总数一半就可以找到中位数了。
代码中,一个数列需要记录到数组中,排序时可以利用一个指针一次指向后一个数,这个比较简单。而另一个数列只能每次只读取一个数,排序需要下一个数时随时读取。
然后就是注意归并排序时特殊情况的处理,如果一个数列已经完全排完,则需要不再对其进行操作。我的方法就是在最后放一个很大的数,逐个读取的话就直接赋值,具体可看代码。一个难点是条件判断的具体临界点,要细心才行。
代码写出来其实不多,如果没有题目理解的问题,估计通过率会高很多。
代码
Github最新代码,欢迎交流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <limits.h>
int main()
{
int N1, N2, seq[200001], *p = seq, n, c = 0, median = 0;
/* Read one sequence into array */
scanf("%d", &N1);
for (int i = 0; i < N1; i++)
scanf("%d", seq + i);
/* Boundary setup */
seq[N1] = INT_MAX;
/* Read the other sequence on the fly while doing merge sort.
* This is due to the memory limitation of the problem (1.5MB) */
scanf("%d", &N2);
/* read the first one */
scanf("%d", &n);
/* just enough to find the median: skip the first half */
for (int i = 0; i < (N1 + N2 + 1) / 2; i++) {
/* Record the smallest of the two */
if (*p < n)
median = *p;
else
median = n;
/* Either point p at the next number or read a new number into n */
if ((*p < n && p < seq + N1) || (*p > n && c == N2)) {
p++;
} else {
if (c == N2 - 1)
n = INT_MAX;
else
scanf("%d", &n);
c++;
}
}
printf("%d", median);
return 0;
}