PAT 1029 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
这个题我感觉有点坑,题目指明整数大小不超过long int,但是我在两台电脑跑了一下sizeof(long int)发现一个是4,另一个是8,实际上这题使用long int会内存超限错误,使用int可以AC。所以这个long int到底是什么意思?我觉得有点误导人。
这题解题思路是参考别人的,普通方法最后一个会爆内存。
参考链接

#include <iostream>
#include <cstdio>
#define maxn 200001

using namespace std;

int main()
{
    int N, M;
    int a[maxn], tmp;
    scanf("%d", &N);
    for (int i=1; i<=N; i++)
        scanf("%d", &a[i]);
    scanf("%d", &M);
    int cnt=0, mid, i, k=1;
    mid = (N+M+1)/2;
    for (i=0; i<M; i++)
    {
        scanf("%d", &tmp);
        while(a[k]<=tmp && k<=N)
        {
            k++;
            cnt++;
            if (cnt == mid)
                printf("%d", a[k-1]);
        }
        cnt++;
        if (cnt == mid)
            printf("%d", tmp);
    }
    if (cnt < mid)
        printf("%d", a[k+mid-cnt-1]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值