美文网首页
PAT-Alevel-1029 Median (25 分): 求

PAT-Alevel-1029 Median (25 分): 求

作者: John_Tsemin | 来源:发表于2019-08-09 23:39 被阅读0次

    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;
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:PAT-Alevel-1029 Median (25 分): 求

          本文链接:https://www.haomeiwen.com/subject/zpzkjctx.html