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的更多相关文章

  1. PAT 1029 Median[求中位数][难]

    1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...

  2. 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 ...

  3. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  4. 1029 Median (25 分)

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  5. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  6. PAT 甲级 1029 Median

    https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...

  7. 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 ...

  8. 1029 Median (25分)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  9. PAT 1029 Median (25分) 有序数组合并与防坑指南

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

随机推荐

  1. DAX/PowerBI系列 - 写在前面

    今天讲的主角是: 不过,先上一个图--2017 Gartner商业智能和数据分析魔力象限 DAX关注这个玩意儿有好一段时间了,刚开始的时候(2014年?)是从Excel里面认识的.2014年当时公司用 ...

  2. JAVA开发环境搭建 - JDK安装及环境变量配置

    1.前提条件 系统:本教程以WIN7系统为例 JDK:本教程以jdk-7u79-windows-x64为例 2.安装步骤 双击运行JDK安装程序

  3. iOS 原生的 UIButton 点击事件是不允许带多参数的,唯一的一个参数就是默认UIButton本身 那么我们该怎么实现传递多个参数的点击事件呢?

    UIButton *btn = // create the button objc_setAssociatedObject(btn, "firstObject", someObje ...

  4. Visual Studio 2017正式版使用一些疑问

    刚升级完2017,是从2015升上去的,总体没有什么大的问题,就是报了一些ts的类型检查的问题,最重要的就是编译速度变得好慢啊,希望尽快出来补丁修复,以前一个解决方案只要10+秒,现在要50秒,表示体 ...

  5. Caffe学习系列(二)Caffe代码结构梳理,及相关知识点归纳

    前言: 通过检索论文.书籍.博客,继续学习Caffe,千里之行始于足下,继续努力.将自己学到的一些东西记录下来,方便日后的整理. 正文: 1.代码结构梳理 在终端下运行如下命令,可以查看caffe代码 ...

  6. Oracle-orclEXORIM

    imp pzhdb/hiway@orcl file =d:\pzhsd.dmp fromuser = pzhsd touser=pzhdb:导入 第一个pzhdb为新的用户名 hiway为密码 orc ...

  7. 机器学习基石 1 The Learning Problem

    机器学习基石 1 The Learning Problem Introduction 什么是机器学习 机器学习是计算机通过数据和计算获得一定技巧的过程. 为什么需要机器学习 1 人无法获取数据或者数据 ...

  8. Git-最简单的本地项目变成版本仓库,然后把内容推送到GitHub仓库

    (注:本文的前提是本地Git仓库和github仓库之间已经存在SSH key了,所以如果没有建立联系的小伙伴们请先建立联系) 具体操作: 一:把本地项目变成版本仓库 1.把本地的一个项目目录编程版本库 ...

  9. curl javaSSm框架中传入json数组的格式方法

    curl与java结合传入get.post,head请求, 比如: curl的地址: curl -l 127.0.0.1:8080/spacobj/core/do?acid=100 -H " ...

  10. appledoc导出iOS代码文档的使用和问题详解(干货篇)

    appledoc导出iOS代码文档的使用和问题详解(干货篇) 1. 简单说一下背景和自己感受 背景: 项目好像突然黄了,公司让详细写项目代码的注释并且导出文档,弄完之后就要封版. 说实话:听到这个消息 ...