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
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int num1[], num2[];
int main(){
int N1, N2;
scanf("%d", &N1);
for(int i = ; i < N1; i++){
scanf("%d", &num1[i]);
}
scanf("%d", &N2);
for(int i = ; i < N2; i++){
scanf("%d", &num2[i]);
}
int index = -, ans = , i = , j = , mid = (N1 + N2 - ) / ;
while(i < N1 && j < N2 && index != mid){
if(num1[i] < num2[j])
ans = num1[i++];
else ans = num2[j++];
index++;
}
while(i < N1 && index != mid){
ans = num1[i++];
index++;
}
while(j < N2 && index != mid){
ans = num2[j++];
index++;
}
printf("%d", ans);
cin >> N1;
return ;
}

A1029. Median的更多相关文章

  1. PAT A1029 Median (25 分)——队列

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

  2. A1029 Median (25 分)

    一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...

  3. PAT甲级——A1029 Median

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

  4. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  5. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  6. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  8. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  9. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

    B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...

随机推荐

  1. javaScript常用API合集

    节点 1.1 节点属性 Node.nodeName   //返回节点名称,只读 Node.nodeType   //返回节点类型的常数值,只读 Node.nodeValue  //返回Text或Com ...

  2. 总结and规划

    不知不觉中又过去了一年,马上就要读研究生了,因此有必要对自己进行必要的总结,以及对自己有个良好的规划. 首先,描述自己当前的心情——对未来充满了恐惧和焦虑. 马上大学就要毕业了,回首经历的大学生涯,似 ...

  3. 一些调格式的经验 & 插入图注和尾注

    一些调格式的经验(以Word2010为例) 1. 从目录正文分别编页码 将光标放在要重新编写页码起始页的最开始位置 分节:页面布局->分隔符->分节符(连续) 插入页码后,选中页码起始页页 ...

  4. GIthub地址

    https://github.com/cuibaoxue/Text1

  5. Jenkins 安装简记录

    下载jenkins.war,放入tomcat 启动tomcat,如果console报错java.lang.OutOfMemoryError: PermGen space,则修改startup.bat( ...

  6. HTML的input类型为hidden导致无法reset改字段的value问题

    问题关键:根据HTML规范,hidden是非ui类元素,不接受用户处理.所以form的 reset并不影响它. http://stackoverflow.com/questions/6367793/w ...

  7. 变更RHEL(Red Hat Enterprise Linux 5.8)更新源使之自动更新

    HP 4411s Install Red Hat Enterprise Linux 5.8) pick up from http://blog.chinaunix.net/uid-423637-id- ...

  8. [转帖]从 2G 到 5G,手机上网话语权的三次改变

    从 2G 到 5G,手机上网话语权的三次改变 美国第一大电信运营商 Verizon 公司的 CEO Hans Vestberg 手持一部 iPad,屏幕上显示俯瞰地面的飞行地图.400 多公里外的洛杉 ...

  9. Laravel Eloquent ORM 时如何查询表中指定的字段

    导读:在使用Laravel ORM的Model方法find, get, first方法获取数据对象时返回的数据对象的attributes属性数组里会包含数据表中所有的字段对应...原文地址:http: ...

  10. big emoji & emoji

    big emoji & emoji font-size: 10 rem; https://www.clickemoji.com/ https://www.cnblogs.com/xgqfrms ...