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. Python - 列表解析式

    列表解析——用来动态地创建列表 [expr for iter_var in iterable if cond_expr] 例子一: map(lambda x: x**2, range(6)) [0, ...

  2. 《linux内核设计与分析》内核模块编程

    内核模块编程 一.准备工作 虚拟机:VMware Workstation 12操作系统:ubuntu当前内核版本:linux-headers-4.4.0-22-generic 二.有关于内核模块的知识 ...

  3. “i词汇”宣传文案

    目录 "i词汇"微信小程序 队名 :颜罗王team 成员: 姓名 学号 杨雪莹(PM) 201521123005 林楚虹 201521123002 董美凤 201521123003 ...

  4. oracle mysql gbk varchar varchar2

    http://www.cnblogs.com/kxdblog/p/4042331.html https://wenku.baidu.com/view/97524e0f844769eae009ed80. ...

  5. JavaScript模拟表单(带数组的复杂数据结构)提交

    function test(){    var typeArray = new Array();    typeArray.push("mm");    typeArray.pus ...

  6. Jira 自定义工作流

    一.添加修改工作流 打开 设置--问题--工作流 复制一个工作流,然后进去编辑页面 添加状态 增加转换动作 切换到文本,设置跳转过程中的事件 针对Stop Progress事件,修改跳转界面(界面需先 ...

  7. 防止xss攻击。

    function htmlEscape(text){ return text.replace(/[<>&\"=]/g,function(match,pos,origina ...

  8. C#微信扫码支付Demo

    1.打开微信支付开发平台: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1 2.下载SDK Demo: C#版下载

  9. MES模块

    基础数据管理:产品模型.工厂模型.工艺模型 仓储管理 成本管理 绩效管理 看板管理 日志管理 设备管理:工装管理.设计器具管理.设备保养管理.设备备件管理.设备采集管理.设备点检管理.设备故障管理.设 ...

  10. vue实现带规格商品的表格编辑

    实现效果: 需求分析: 商品分为 启用规格 和 未启用规格 两种状态, 启用时显示带规格表格, 不启用时显示无规格价格 规格大项最多添加两个, 比如 "颜色", "尺寸& ...