PAT Advanced 1029. Median (25) (C语言实现)

我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到Github Pages浏览最新内容(本篇文章链接)。此处文章目前已更新至与Github Pages同步。欢迎star我的repo

题目

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 × 1 0 5 \le 2\times 10^5 2×105 ) 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

思路

解题之前:

这道题一直到我做的时候,题目要求还是有一个严重的问题。这个问题就是题目中的数列中数的数据类型为long int,长度最大为 2 × 1 0 5 2\times10^5 2×105,这样的话一个这样的数列,需要的内存即为1.6MB,而题目却要求内存限制在1.5MB之内。

而这道题是不能在不记录下一个完整的数列的情况下解决的,例子很容易想出来。我确定没有什么神奇的方法之后,就在网上看别人答对的是怎么办的。

结果他们只是用int类型做的就过了!!!就过了!

(如果你现在看到的和我上面说的不一样,就说明题目更正过来了)

解题思路:

尽管使用int的类型,但是两个200000长度的int数组也是1.6MB,会大于题目限制。因此本道题需要只是用一个数组记录一个数列,另一个就要逐个读取而不记录,需要on the fly的处理。

具体的方法可以使用归并排序的思路,从最小的一端排序到总数一半就可以找到中位数了。

代码中,一个数列需要记录到数组中,排序时可以利用一个指针一次指向后一个数,这个比较简单。而另一个数列只能每次只读取一个数,排序需要下一个数时随时读取。

然后就是注意归并排序时特殊情况的处理,如果一个数列已经完全排完,则需要不再对其进行操作。我的方法就是在最后放一个很大的数,逐个读取的话就直接赋值,具体可看代码。一个难点是条件判断的具体临界点,要细心才行。

代码写出来其实不多,如果没有题目理解的问题,估计通过率会高很多。

代码

最新代码@github,欢迎交流

#include <stdio.h>
#include <limits.h>

int main()
{
    int N1, N2, seq[200001], *p = seq, n, c = 0, median;

    /* 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值