PAT甲级 1029 Median(25 分)AC 解决内存超限

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xmj15715216140/article/details/80925186

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×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 145 9 10 15 16 17Sample Output:13

题目大意:求出两个有序序列的中位数

题解:
     第一个队列存好后,把第二个队列边读,边和第一个队列比较,选择出队。这样可以不用一次存完第二个队列,解决超内存的问题。

思路:
    第一、二个序列分别有n, m个元素,所以需要从队头剔除(n + m - 1) / 2个元素,
    最后答案就是两个队头的最小值。对于最终答案在第一第二个队列中的情况要分开处理。
    若答案在第二个队列中,在输入数据时就可以提前得出答案并退出,若答案在第一个队列中,要二次出队才能找到答案。

注意:
    在所有元素入队列完毕后,把INT_MAX入队列,一是这样队列永不为空,方便处理。
-----------------------------------------
修改:
1.题目说数据不超过long int, long int占4还是8个字节是要看具体情况的。
2.根据测试代码1,测得所有数据不超过int
3.根据测试代码2,测得PAT中 long int占8个字节
4.如果全部用long int 存储,会爆内存。题目说数据不超过lont int ,但实际没超过int。
  之前题解中是我对 long int 没考虑周全,谢谢评论区提醒

说明与致歉:
1.首先,这篇博客是原创,并且开源在柳婼github上的,有pull request记录 
  https://github.com/liuchuo/PAT/commit/ad3de403d3d750aa98efd0ecb2c5234ca17b303e
2.其次,我之前没说明,造成误解不好意思,表示歉意,现在柳婼的这题的题解已经更新其他解法了。
3.最后,如果由其他有问题也欢迎和我交流。
-----------------------------------------

#include <iostream>
#include <climits>
#include <queue>
using namespace std;
int main() {
    queue<int> a, b;
    int n, m, num, cnt = 0;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &num);
        a.push(num);
    }
    a.push(INT_MAX);
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%d", &num);
        b.push(num);
        if(cnt == (n + m - 1) / 2) {
            printf("%d", min(a.front(), b.front()));
            return 0;
        }
        if(a.front() < b.front())   
            a.pop();
        else                        
            b.pop();
        cnt++;
    }
    b.push(INT_MAX);
    for(; cnt < (n + m - 1) / 2; cnt++) {
        if(a.front() < b.front())    
            a.pop();
        else                         
            b.pop();
    }
    printf("%d", min(a.front(), b.front()));
    return 0;
}

测试代码1

#include <iostream>
#include <climits>
int main() {
    long long  n;
    while(scanf("%lld",&n) != EOF){
      if(n > INT_MAX) while(1);
    }
    return 0;
}

测试代码2

#include <iostream>
int main() {
    if(sizeof(long int) == 8) {
      while(1);
    }
    return 0;
}
展开阅读全文

没有更多推荐了,返回首页