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 ...
随机推荐
- 《Django By Example》第七章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:咳咳,第七章终于来了.其 ...
- maven 项目发布时,无法引用 修改的domain 问题
其实是在bo 的smart-score 里面引用了这个interface 所以还要把 smart -Score 重新发布一下.让这个smart 引用 新的 Player-service 中的i ...
- Bat再次小试
继<Bat小试牛刀>之后,今天又需要一个小的bat文件.需求是这样的,有一个windows服务(服务名:xxxx,进程映像名:xxxx.exe)被数据库拖慢了,但目前又没时间调整代码,所以 ...
- Selenium 使用过程遇到问题随笔
最近正在学习Selenium,自学是比较难的,也很感谢网络环境中,各位大大的博文帮助. 也希望在此能够记录一下从小白学习使用selenium测试的过程,也希望能对别人有所帮助. 关于环境部署,以及入门 ...
- View的呈现(一)ActionResult
ActionResult Http是一个单纯采用请求/回复消息交换模式的网络协议,Web服务器在接收并处理来自客户端的请求后悔根据处理结果对请求予以回应.一般来说针对请求的处理最终体现在对目标Acti ...
- C/C++学习路线图
文章转载自「开发者圆桌」一个关于开发者入门.进阶.踩坑的微信公众号 这里整理的C/C++学习路线图包含初中高三个部分,你可以通过百度云盘下载观看对应的视频 链接: http://pan.baidu.c ...
- JS判断是否为数字或为空
function checkcc() { var reg = new RegExp("^[0-9]*$"); var obj = document.getEleme ...
- github 删除远程仓库项目中的任意文件夹
今天上传代码把不需要的push上去了.结果想删除那个不想要的怎么弄都不行.网上大部分都是把那个项目整个暴力删除.那可不行啊那么多都删除.下次上传不是要命啊! 试啊试终于解决了.顺便记录一下也帮助下需要 ...
- 1637: [Usaco2007 Mar]Balanced Lineup
1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 393 Solved: 263[ ...
- 记录Winform开发过程中遇到的情况
前两天开发了个Winform操作Excel和数据库的一个小程序,把Winform的一些东西又给捡了起来,当中又学到了一些新的东西,特来写出来留作纪念. 一.CSKIN美化框架的使用 刚开始做的时候,发 ...