博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1029 Median[求中位数][难]
阅读量:6934 次
发布时间:2019-06-27

本文共 2887 字,大约阅读时间需要 9 分钟。

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 17

Sample Output:

13

 题目大意:找出两个序列的中位数,需要将两列合在一起找。

 //怎样从一堆中找出中位数。应该不能用排序,因为数据量真的很大。

#include 
#include
#include
#include
#include
using namespace std;int main() { priority_queue
pq; int m,n; scanf("%d",&m); long int t; for(int i=0;i

 

//这个代码是取巧用了优先队列,利用它的堆排序的性质,先入队,然后再出队,出到顶端是中位数的情况,在牛客网上通过,但是在pat上有最后4个 测试点是内存超限,那应该就是不行了,不能用优先队列来实现。但是不用的话,我一时想不起来该怎么做。

 主要就是空间的问题,不能开两个数组的空间,会内存超限。

代码来自:https://www.liuchuo.net/archives/2248

#include 
#include
#include
#include
using namespace std;int main() { queue
a, b; long long tnum; int n, m, num, cnt = 0; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%lld", &tnum); num = min((long long)INT_MAX, tnum); a.push(num); } a.push(INT_MAX); scanf("%d", &m); for(int i = 0; i < m; i++) {
//控制对m个数的读入 scanf("%lld", &tnum); int num = min((long long)INT_MAX, tnum); 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就会+1. cnt++; } b.push(INT_MAX);//保证b队列不会空,用 /**4 11 12 13 142 2 3**///测试 for(; cnt < (n + m - 1) / 2; cnt++) { if(a.front() < b.front()) a.pop(); else b.pop(); }//肯定得有这个for循环,像下面这个样例, //情况为:当第二队列全部读入时,仍然没有弹出一般的数。 /** 10 11 26 26 29 36 40 67 68 72 82 4 23 30 62 67 **/ printf("%d", min(a.front(), b.front())); return 0;}

 

//先读入第一队列中所有的数,

1.INT_MAX在limits.h文件中

2.两个队列最后都放入一个INT_MAX保证非空的比较

3.在遇到大于INT_MAX的时候并不会是最终的结果,使用min函数作为num。

非常厉害,学习了。

转载于:https://www.cnblogs.com/BlueBlueSea/p/9446101.html

你可能感兴趣的文章
最全的MAC端截图工具推荐,寻找适合自己的截图工具
查看>>
使用 nginx 同域名下部署多个 vue 项目,并使用反向代理
查看>>
Python基本数据类型之元组
查看>>
LeetCode-数组-删除有序数组重复元素
查看>>
我所理解的原型&原型链
查看>>
工作三年,我要如何提升Java技术 | 粉丝提问
查看>>
JavaScript 如何使用闭包
查看>>
React 教程:快速上手指南
查看>>
6 个理由,让我不顾一切撑腰 Python!
查看>>
[ 一起学React系列 -- 11 ] React-Router4 (1)
查看>>
在Java中使用redisTemplate操作缓存
查看>>
Generator函数的语法以及异步的应用
查看>>
使用 qrcodejs 生成二维码的几个问题
查看>>
ES6-Promise对象
查看>>
记录一次面试题
查看>>
Flutter Exception降到万分之几的秘密
查看>>
Fiddler抓取数据并分析(完整的配置教程)
查看>>
Keras入门(一)搭建深度神经网络(DNN)解决多分类问题
查看>>
【思维导图-索引篇】搞定数据库索引就是这么简单
查看>>
Kotlin如何避免“!!”(非空断言)
查看>>