PAT 1029 Median[求中位数][难]
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 14
5 9 10 15 16 17
Sample Output:
13
题目大意:找出两个序列的中位数,需要将两列合在一起找。
//怎样从一堆中找出中位数。应该不能用排序,因为数据量真的很大。
#include <stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
#include<queue>
using namespace std; int main() {
priority_queue<long int> pq;
int m,n;
scanf("%d",&m);
long int t;
for(int i=;i<m;i++){
scanf("%ld",&t);
pq.push(t);
}
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%ld",&t);
pq.push(t);
}
int x=(m+n)/;
for(int i=;i<x;i++){
pq.pop();
}
printf("%ld",pq.top());
return ;
}
//这个代码是取巧用了优先队列,利用它的堆排序的性质,先入队,然后再出队,出到顶端是中位数的情况,在牛客网上通过,但是在pat上有最后4个 测试点是内存超限,那应该就是不行了,不能用优先队列来实现。但是不用的话,我一时想不起来该怎么做。
主要就是空间的问题,不能开两个数组的空间,会内存超限。
代码来自:https://www.liuchuo.net/archives/2248
#include <iostream>
#include <climits>
#include <queue>
#include<stdio.h>
using namespace std;
int main() {
queue<int> a, b;
long long tnum;
int n, m, num, cnt = ;
scanf("%d", &n);
for(int i = ; 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 = ; i < m; i++) {//控制对m个数的读入
scanf("%lld", &tnum);
int num = min((long long)INT_MAX, tnum);
b.push(num);
if(cnt == (n + m - ) / ) {
printf("%d", min(a.front(), b.front()));
return ;
}
if(a.front() < b.front())
a.pop();//这样边入边弹,非常好。就不会内存超限了。
else
b.pop();//每弹出一个cnt就会+1.
cnt++;
}
b.push(INT_MAX);//保证b队列不会空,用
/**
4 11 12 13 14
2 2 3
**///测试
for(; cnt < (n + m - ) / ; 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 ;
}
//先读入第一队列中所有的数,
1.INT_MAX在limits.h文件中
2.两个队列最后都放入一个INT_MAX保证非空的比较
3.在遇到大于INT_MAX的时候并不会是最终的结果,使用min函数作为num。
非常厉害,学习了。
PAT 1029 Median[求中位数][难]的更多相关文章
- 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
尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还tmd一直再算平均,卧槽 #include <iostream> #include <cstdio> #include ...
- PAT 甲级 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
- URAL 1306 - Sequence Median 小内存求中位数
[题意]给出n(1~250000)个数(int以内),求中位数 [题解]一开始直接sort,发现MLE,才发现内存限制1024k,那么就不能开int[250000]的数组了(4*250000=1,00 ...
- PAT甲 1029. Median (25) 2016-09-09 23:11 27人阅读 评论(0) 收藏
1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...
- 1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】
题目:戳这里 百度之星初赛原题:戳这里 题意:n个不同的数,求中位数为m的区间有多少个. 解题思路: 此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比 ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- POJ 2388 Who's in the Middle(水~奇数个数排序求中位数)
题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #in ...
随机推荐
- WP8.1开发:简单天气预报应用(转)
今天小梦给大家分享一个简单的天气预报应用源码:调用的是百度API.整个应用都没有什么难点.只是一个简单的网络请求和json数据处理.在WP8.1有小娜的情况下,天气预报应用还有意义吗?我认为还是有点意 ...
- iPhone 上如何通过 Safari 使用 Pocket
在开始之前,请确认你的机器上已经安装了 Pocket 应用软件. 如何安装 1.打开Pocket应用,点击左上角的菜单(三条横岗),找到最下面的 Help ,点击 How To Save ,找到 ...
- MFC 常见问题
一 常见变量获得 CDC * cDc=GetDC(); HDC m_Screenhdc = this->GetDC()->m_hDC; // 整个窗口客户区的坐标 this->Ge ...
- truncate表恢复
Fy_Recover_Data 2014.03.07 更新 -- 现在可以从离线文件中恢复被Truncated的数据了 2014新版本的恢复思路 与 2012的不同 2014年是离线恢复 http:/ ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- nginx命令行参数和信号
nginx命令行参数 [user@host dir]$ /usr/local/nginx/sbin/nginx -hnginx version: nginx/1.8.0Usage: nginx [-? ...
- mysql优化之伪哈希索引
想法非常简单,在标准的B-Tree索引上创建一个伪哈希索引.它和真正的哈希索引不是一回事,因为它还是使用B-Tree索引进行查找.然而,它将会使用键的哈希值进行查找,而不是键自身.你所要做的事情就是在 ...
- ssh命令远程登录
1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...
- 安装cnpm
使用淘宝镜像的cnpm $ npm install -g cnpm --registry=https://registry.npm.taobao.org
- The request associated with the AsyncContext has already completed processing
Some time ago there was a problem with the servlet3.0, is in servlet in asynchronous processing data ...