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

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <limits.h>
using namespace std;
queue<int> a,b; int main() {
int n,m;
int x;
scanf("%d", &n);
int count=;
for (int j = ; j < n; j++) {
scanf("%d", &x);
a.push(x);
}
a.push(INT_MAX);
scanf("%d", &m);
int point = (n + m - ) / ;
for (int i = ; i < m; i++) {
int temp;
scanf("%d", &temp);
b.push(temp);
if (count == point) {
printf("%d", min(a.front(), b.front()));
return ;
}
if (a.front() < temp) {
a.pop();
}
else {
b.pop();
} count++;
}
b.push(INT_MAX);
while (count != point) {
if (a.front() < b.front()) {
a.pop();
}
else {
b.pop();
}
count++;
}
printf("%d", min(a.front(), b.front()));
}

注意点:这道题内存限制1.5M,全部储存起来做内存就超了,而题目里说给的数字不超过long int,实际测试发现没有超过int。

要不超过内存,必须使用边读数据边处理的方法,中间数就是要把前面 (n+m-1)/2 个数弹出,用队列实现,只要读第二个数组时一个个判断就好了。

ps:加入一个INT_MAX是为了判断时方便,不用再判断队列是否为空,最大数肯定不会被弹出。

PAT A1029 Median (25 分)——队列的更多相关文章

  1. A1029 Median (25 分)

    一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...

  2. PAT 1029 Median (25分) 有序数组合并与防坑指南

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

  3. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  4. 1029 Median (25 分)

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  5. 【PAT甲级】1029 Median (25 分)

    题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...

  6. 7-19 PAT Judge(25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  7. PAT A1075 PAT Judge (25 分)——结构体初始化,排序

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  8. 1095 解码PAT准考证 (25 分)

    PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...

  9. 10-排序5 PAT Judge (25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

随机推荐

  1. centos7.4+mysql5.6+virtualenv+python3.6+nginx+uwsgi+django生产环境搭建

    一 更新yum # yum update 二 安装gcc  lrzsz软件 # yum install gcc 用来编译python3.6源码 # yum install lrzsz 用来上传文件 三 ...

  2. js查询数组或者List类型是否包含某个元素

    方法一:arr.indexOf(某元素) 实际用法:if(arr.indexOf(某元素) > -1){//则包含该元素} 例: var fruits = ["Banana" ...

  3. AppBoxPro(权限管理框架--FineUIPro基础版+工厂模式+ADO.NET+存储过程)

    FineUIPro基础版火爆来袭,特献上ADO.NET纯SQL方式AppBoxPro,希望大家能够喜欢! 下载源码请到[知识星球] https://t.zsxq.com/3rrNFyv

  4. pycharm运行Django发生AppRegistryNotReady: Apps aren't loaded yet.

    pycharm中运行django默认情况下并不是执行项目的,所以如果在非manage.py,会发生异常. raise AppRegistryNotReady("Apps aren't loa ...

  5. 消除TortoiseSVN 检出到(checkout)桌面上显示一堆问号

    之前不小心直接将版本库的内容检出到桌面,后才发现桌面上的文件图标都变成了问号,新建文件夹也同样如此. 为了解决这个问题,采用如下方法(任何一个检出文件夹均可这样操作): 1.删除桌面隐藏的.SVN文件 ...

  6. java解析复杂json:JSONObject 和 JSONArray的使用

    在正式解析之前,我们需要下载解析Json所需要的jar包,一共有7个. 下载地址如下:https://download.csdn.net/download/zai_xia/10374080 大家也可以 ...

  7. 纯CSS选项卡

    html: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...

  8. 第六章 Hyper-V 2012 R2 的检查点

    "检查点"是 Windows Server 2012 R2 中对 Windows Server 2012 及以前版本的 Hyper-V"快照"功能的新称呼.之所 ...

  9. if条件简单语法

    if语句是实际工作中最重要最常用的语句. if条件语法: 单分支结构 if [ 条件 ] then 指令 fi 或 if [ 条件 ]:then 指令 fi if 单分支条件中文编程形象语法: 如果 ...

  10. Django【进阶篇】

    目录 一.Model 二.admin 三.Form组件 四.Cookie 五.Session 六.分页 七.序列化 一.Model 数据库的配置 1.django默认支持sqlite,mysql, o ...