PAT-Alevel-1029 Median (25 分): 求中位数(限制内存使用)

1029 Median (25 分)

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

时间限制: 200 ms
内存限制: 1.5 MB
代码长度限制: 16 KB

首先,这题一开始上来我直接放到Vector里,然后sort一下,企图直接暴力得到中位数,这种做法肯定是不可取的,时间空间都会超出。
然后稍微冷静一下,我发现给出的是两个有序的数列,应该采用归并,我改造过后,再次提交,时间够了,但是空间依然超出。仔细一算发现问题并不简单,每行有最多2*10^{5}个long类型的数字,两行全存进来需要400K * 4Byte = 1600KB,然而内存限制是1.5M,明显不是让你这么做。
由于是两个有序数列,假设可以随机访问,理论上按照外存归并排序的思想,给两个变量的内存空间,就能够让他们归并成一个有序数列。另外,在此我们并不需要一个有序数列,如果知道一共有N个数字,那么第(N+1)/2 个数字就是我们要找的,所以我们从前往后数到第(N+1)/2 个数字就得到结果了。
但是此处不能够随机访问,我们只能先把第一列数字读进内存,然后读出第二个数列的首个数字,也就是第二个数列的数字个数,我们就知道总共有多少数字了。接下来按照归并的方式从前往后数到第(N+1)/2 个数字输出即可。

#include <iostream>
#include <sstream>
#include <map>
#include <unordered_set>
#include <set>
#include <string>
#include <vector>
#include <ctime>
#include <sys/timeb.h>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <math.h>
#include <limits.h>
#include <cstdlib>
#include <stdio.h>


using namespace std;
/*
1029
*/

int main()
{

    vector<int> InputNums;
    int N1,N2;
    ios::sync_with_stdio(false);
    cin>>N1;
    int Num;

    for(int i=0;i<N1;i++){
        cin>>Num;
        InputNums.push_back(Num);

    }
    cin>>N2;

    int midPos = (N1+N2+1)/2;
    long headNumOfArray2;
    int headPosOfArray1=0,headPosOfArray2=0;
    cin>>headNumOfArray2;

    long res;
    for(int i=0;i<midPos;i++){
        if((headPosOfArray1<N1 && InputNums[headPosOfArray1] <= headNumOfArray2)
           ||(headPosOfArray2 >= N2))
        {
            res = InputNums[headPosOfArray1];
            headPosOfArray1++;

        }
        else{
                res = headNumOfArray2;
                headPosOfArray2++;
                cin>>headNumOfArray2;

        }

    }
    cout<<res;

    system("pause");
    return 0;


}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,181评论 0 13
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,493评论 0 23
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,750评论 0 38
  • 在这举国同庆的国庆佳节即将来临之际,我的心情却异常沉重与复杂。 夜深人静,家里只有洗衣机里滚筒洗衣服的声音和我敲打...
    46c085bddb0e阅读 1,490评论 0 0
  • 清晨,阳光透窗而入。拉开窗帘,微风拂面。轻倚在窗前,欣赏着院子里那一树竞开的梨花。梨树枝叶茂盛,枝头已经伸展到窗下...
    古城苍狼阅读 4,592评论 10 15

友情链接更多精彩内容