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. PHP链接Redis

    命令行下运行 redis-server.exe redis.windows.conf 此时,redis服务已经开启,然后我们可以再新开一个命令行,作为控制台 redis-cli.exe -h 127. ...

  2. 记录一次EF优化

    问题描述:1.第一次加载过慢(EntityFramework 6 code-first).2.一段时间间不访问页面同样变慢. 原因分析:1.第一次启动(Code First)会对比程序中的Model与 ...

  3. x01.Weiqi.13: 鼎力推荐

    鼎力推荐 : 点击后即可观看,小伙子讲的很有深度. 说到深度,自然离不了深度学习.AlphaGo 的横空出世,似乎很有学习的必要. MuGo: 点击下载后,发现是 python,自然免不了一番学习,好 ...

  4. java-信息安全(二)-对称加密算法DES,3DES,AES,Blowfish,RC2,RC4

    概述 信息安全基本概念: DES(Data Encryption Standard,数据加密标准) 3DES(Triple DES,三重数据加密算法(TDEA,Triple Data Encrypti ...

  5. 3212: Pku3468 A Simple Problem with Integers

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1053  So ...

  6. 1596: [Usaco2008 Jan]电话网络

    1596: [Usaco2008 Jan]电话网络 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 601  Solved: 265[Submit][S ...

  7. 扩大按钮 btn 响应区域

    方法一:类别 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p2 { margin: 0 ...

  8. Android中的WebView实战详解(二)

    四.如何用WebView下载软件? 四.如何用WebView下载软件? public class MainActivity extends AppCompatActivity { private We ...

  9. Jmeter BeanShell 时间格式化处理

    工作中碰到的,记录下 在XML格式的请求数据中,Soap接口请求中的日期参数格式是这样的"2016-07-20T18:03:00" 在日和时之间多了一个T 所以在Jmeter--& ...

  10. 【方法】如何限定IP访问Oracle数据库

    [方法]如何限定IP访问Oracle数据库 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知 ...