PAT A1029 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 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 分)——队列的更多相关文章
- A1029 Median (25 分)
一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 1029 Median (25 分)
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- 7-19 PAT Judge(25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- PAT A1075 PAT Judge (25 分)——结构体初始化,排序
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- 1095 解码PAT准考证 (25 分)
PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...
- 10-排序5 PAT Judge (25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
随机推荐
- C#设计模式——简单工厂模式、工厂模式和抽象工厂模式
一:简单工厂模式 1:描述:简单工厂模式是由一个工厂对象根据接收到的消息决定要创建哪一个类的对象事例. 2:优点:工厂类中有相关逻辑判断,可以根据需要动态创建相关的对象事例,而客户端只需要告诉工厂类创 ...
- PHP cURL获取微信公众号access_token
1.开发微信公众号首先要获取access_token,在运行代码前现在开发者设置中把本服务器IP添加到白名单中 public function index(){ $appId = 'wxd0e50fe ...
- MySQL chartset
-- # https://dev.mysql.com/doc/refman/8.0/en/charset-database.html -- create database aixinyz; -- 默認 ...
- csharp: Use of Is and As operators in csharp
/// <summary> /// Geovin Du 20170622 /// </summary> /// <param name="sender" ...
- Linux LB--负载均衡和高可靠
1.负载均衡典型应用场景,外网.内网.私网公共服务. 典型场景: (1)用户通过公网访问数据中心的ftp.web.https服务器. (2) 在数据中心内部东西向访问其他服务时,例如,访问其他虚拟机. ...
- Linux 学习笔记之超详细基础linux命令 Part 11
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...
- logcat use
将已经存在的工程导入到eclipse步骤: ①:首先复制当前工程所在的路径. ②:然后在eclipse,右键->Import->General->Existing Projects ...
- React数据流和组件间的通信总结
今天来给大家总结下React的单向数据流与组件间的沟通. 首先,我认为使用React的最大好处在于:功能组件化,遵守前端可维护的原则. 先介绍单向数据流吧. React单向数据流: React是单向数 ...
- tomcat 7.0 最大连接数和线程设置
部署项目时需要根据服务器配置调整连接数 <Connector port="8080" protocol="HTTP/1.1"URIEncoding=&qu ...
- [MapReduce_8] MapReduce 中的自定义分区实现
0. 说明 设置分区数量 && 编写自定义分区代码 1. 设置分区数量 分区(Partition) 分区决定了指定的 Key 进入到哪个 Reduce 中 分区目的:把相同的 Key ...