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. struts2.5新配置动态调用

    开启动态调用: <constant name="struts.enable.DynamicMethodInvocation" value="true"/& ...

  2. jquery及原生javascript对jsonp解决跨域问题实例详解

    jquery方式 前端: $.ajax({ url: 'http://m.xxx.tv/goLottery', data: { data: data }, type: 'GET', dataType: ...

  3. SESSION 与 COOKIE的区别是

    有以下几点 1.session是存在服务器端,cookie是存在客户端 2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,所以session安全性要比cookie ...

  4. java实现多叉树查找

    package tree; import java.util.List; import java.util.ArrayList; import java.io.Serializable; public ...

  5. 1671: [Usaco2005 Dec]Knights of Ni 骑士

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 254  Solved: 163 ...

  6. 自定义 Layout布局 UICollectionViewLayout

    from:   http://www.tuicool.com/articles/vuyIriN 当我们使用系统自带的UICollectionViewFlowLayout无法实现我们的布局时,我们就可以 ...

  7. Html +++++css总结

    一. Html部分 Html定义 Hyper Text Markup Language  超文本标记语言 html 1.0  ->  html  2.0   -> ... -> ht ...

  8. 清理浏览器网站缓存的几种方法(meta,form表单,ajax)

    1.meta方法   HTML header中加入 <meta http-equiv="pragma" content="no-cache"> 说明 ...

  9. ViewPager 滑动一半的判断方法以及左滑右滑判断

    做项目的时候,会碰到用viewpager + fragments去实现多页滑动.有些时候需要完成:界面在滑动到一半或是一半以上的时候,需要把title之类的切换到下一个页面.这个时候仅仅依赖Viewp ...

  10. WPF: 本地化(Localization) 实现

    本文将讨论一种较为方便的本地化方法. 由于在项目中要实现本地化,所以在网上查找相关的解决方案.通过一系列调研,发现实现本地化的方法主要有以下三种: 通过编译项目以设置 x:Uid 并使用 LocBam ...