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

分析:寻找中位数。一开始用sort来进行排序后找出中位数,但是这样会有两组数据超时。所以只能进行逐一比较。

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
long t;
int i; scanf("%d",&n);
vector<long> vec1(n);
for(i=0; i<n; i++)
scanf("%ld",&vec1[i]); scanf("%d",&n);
vector<long> vec2(n);
for(i=0; i<n; i++)
scanf("%ld",&vec2[i]); int j,temp = 0;
long v;
vector<long> result; for(i=0,j=0; i<vec1.size() && j<vec2.size(); )
{
if(vec1[i] <= vec2[j]){
v = vec1[i];
i++;
}
else{
v = vec2[j];
j++;
}
result.push_back(v);
if(result.size() == ( vec1.size() + vec2.size() + 1)/2 ){
cout<<result[result.size() - 1]<<endl;
break;
}
} if(result.size() != ( vec1.size() + vec2.size() + 1)/2)
{
while(i<vec1.size())
{
result.push_back(vec1[i]);
if(result.size() == ( vec1.size() + vec2.size() + 1)/2 ){
cout<<result[result.size() - 1]<<endl;
break;
}
i++;
}
while(j<vec2.size())
{
result.push_back(vec2[j]);
if(result.size() == ( vec1.size() + vec2.size() + 1)/2 ){
cout<<result[result.size() - 1]<<endl;
break;
}
j++;
}
}
return 0;
}

【PAT】1029. Median (25)的更多相关文章

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

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

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

  3. 【PAT甲级】1029 Median (25 分)

    题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...

  4. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

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

  6. 1029 Median (25 分)

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

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

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

  8. 【PAT】B1075 链表元素分类(25 分)

    这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...

  9. 【LeetCode】4. Median of Two Sorted Arrays(思维)

    [题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...

随机推荐

  1. 庞锋 OpenCV 视频 学习进度备忘

    书签:另外跳过的内容有待跟进 学习资源: opencv视频教程目录(初级)   主讲:庞锋,毕业于电子科技大学 知识基础支持: 线性代数 应用数学 跳过的内容: 1.第1~6集跳过,简单.(2014- ...

  2. Jcrop+strut2+jsp实现图片剪切

    在网上找,发现都是不全的,要么没获取图片路径,要么没后台等等,今天就来个全的 一:总体步骤 =>页面上传图片 =>获取上传图片剪切的四个值x,y,w,h =>后天进行剪切 接下来就开 ...

  3. 解决g++:command not found(centos7.0)

    问题背景,因为装了虚拟机,系统为centos7.0,由于是纯净版,没有gcc,使用命令yum install gcc安装了gcc,但是没安装g++,导致g++:command not found问题. ...

  4. Scrum流程

    敏捷Scrum流程图: Sprint Planing Meeting: 1.Next Spring Goal; 2.Sprint Backlog; 3.Updated Product Backlog; ...

  5. 怎么从sqlserver 数据库导出 insert 的数据语句

    In SSMS in the Object Explorer, right click on the database right-click and pick "Tasks" a ...

  6. SQL Server 非聚集索引的覆盖,连接,交叉和过滤 <第二篇>

    在SQL Server中,非聚集索引其实可以看做是一个含有聚集索引的表,但相对实际的表来说,非聚集索引中所存储的表的列数要少得多,一般就是索引列,聚集键(或RID).非聚集索引仅仅包含源表中的非聚集索 ...

  7. SQL Server数学函数

    数学函数 1.计算绝对值ABS ABS函数对一个数值表达式结果计算绝对值(bit数据类型除外),返回整数. 语法结构: ABS(数值表达式) 返回值:与数值表达式类型一致的数据 示例: ) --输出 ...

  8. 数据库中使用 Synonym和openquery

    如果,你想在一台数据库服务器上,查询另一个台数据服务器的数据该如何做呢?如果,你想在同一台数据服务器上,在不同的数据库之间查询数据,又该怎么办呢?那就让我为你介绍Synonym和openquery吧. ...

  9. 30大最有影响力的Web设计与开发英文博客

    1stwebdesigner的创始人Dainis Graveris挑选出30个高质量和具有影响力的Web设计与前端技术博客,其中很多我们都耳熟能详.但这么完整的列表,还是值得收藏的.另外,你大概不会了 ...

  10. SpriteParticle II

    [SpriteParticle II] 1.Randomizing the Starting Position 2.Setting the Initial Angle 3.Setting a Part ...