1029. Median
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
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 (<=1000000) 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
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
本题是本意是考察归并排序, 两路归并, 但是由于题目的数据量比较大($ N \le 10 $) 因此如果直接开两个long数组
int a[1000000], b[1000000]
会开崩掉, 接下来需要解决数据量大的问题, 一种办法是在全局区域定义a, b这样可以防止数组开崩掉,但是具体的没有试过, 这题用了一个比较奇怪的方法, 使用vector先装好一个数组,然后一边读取一边归并,这样可以省一个中间数组,虽然这样做的, 结果速度还行,但是内存占用很多,不知道为什么,有机会研究一下, Mark
#include <iostream>
#include <vector>
using namespace std;
void merge(vector<long> a, vector<long> & res)
{
int N;
cin >> N;
int i = 0;
while(N--)
{
long num;
cin >> num;
if(a[i] < num)
{
while(a[i] < num && i != a.size()) //一边读取一边归并
{
res.push_back(a[i]);
i++;
}
res.push_back(num);
if(i == a.size())
{
break;
}
}
else
{
res.push_back(num);
}
}
if(i == a.size())
{
while(N--)
{
long num;
cin >> num;
res.push_back(num);
}
}
else{
while( i != a.size())
{
res.push_back(a[i]);
i++;
}
}
}
int main()
{
vector<long> a;
int N;
cin >> N;
int n = N;
while(n--)
{
long num;
cin >> num;
a.push_back(num);
}
vector<long> res;
merge(a, res);
cout << res[(res.size() - 1) / 2] << endl;
return 0;
}
1029. Median的更多相关文章
- PAT 1029 Median[求中位数][难]
1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...
- 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 ...
- 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)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
- PAT Advanced 1029 Median (25) [two pointers]
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 1029 Median (25分)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
随机推荐
- 史上最全的AJAX
概述 对于web应用程序:用户浏览器发送请求.服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML),渲染并显示浏览器上· Ajax和Form表单提交数据的的好 ...
- tp框架知识 之(链接数据库和操作数据)
框架有时会用到数据库的内容,在"ThinkPhp框架知识"的那篇随笔中提到过,现在这篇随笔详细的描述下. 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写con ...
- [.net 面向对象程序设计深入](14)Redis——基础
[.net 面向对象程序设计深入](14)Redis——基础 很长一段时间没更新博客了,坚持做一件事,真不是件容易的事,后面我会继续尽可能的花时间更新完这个系列文章. 因这个系列的文章涉及的范围太大了 ...
- (八)javaScript对象简介
脚本对象(JavaScript对象) <script type="text/javascript"> var time=new Date(); time ...
- (七)javascript中的数组
一. 一维数组 1.1 声明数组 var 数组名=new Array(数组大小); 1.2 添加元素 <script> var a=new Array(3); a[0]="张三 ...
- 复杂SQL代码实例
DECLARE @begin DATETIME,@end DATETIME,@shanghutype INT, @beginshanghuarea BIGINT ,@endshanghuarea bi ...
- SignalR的一点点东西
JS部分 $.connection.hub.start().done(function () { alert(ok); }).fail(function (error) { alert(error); ...
- WP8.1小梦词典开发2:百度翻译API使用
原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...
- 【原】老生常谈-从输入url到页面展示到底发生了什么
刚开始写这篇文章还是挺纠结的,因为网上搜索“从输入url到页面展示到底发生了什么”,你可以搜到一大堆的资料.而且面试这道题基本是必考题,二月份面试的时候,虽然知道这个过程发生了什么,不过当面试官一步步 ...
- 走入PHP-初次见面
FROM:实验楼 Linux启动WEB服务器: $ sudo service apache2 start 新建并打开test.php文件: $ sudo gvim /path/test.php PHP ...