PAT.A1029. Median

原创 2018年04月16日 22:46:04

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 <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <iostream>
using namespace std;
int a1[1000010], a2[1000010],a3[2000010];
int main(){
	int n, m;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a1[i]);
	scanf("%d", &m);
	for (int i = 0; i < m; i++)
		scanf("%d", &a2[i]);
	int i = 0, j = 0,p=0;
	while (i < n&&j < m) {
		if (a1[i] <=a2[j]) {
			a3[p++] = a1[i];
			i++;
		}
		else {
			a3[p++] = a2[j];
			j++;
		}
	}
	while (i < n) a3[p++] = a1[i++];
	while (j < m) a3[p++] = a2[j++];
	int l = (n + m);
	if (l % 2 == 0) printf("%d", a3[l / 2 - 1]);
	else printf("%d", a3[(l - 1) / 2]);
	return 0;
	
}

median of medians(中位数的中位数)

BFPRT(Blum、Floyd、Pratt、Rivest、Tarjan)算法https://en.wikipedia.org/wiki/Median_of_mediansvoid insertion...
  • hz5034
  • hz5034
  • 2015-07-25 22:12:15
  • 1327

median

中位数是一个可将数值集合划分为相等的上下两部分的一个数值。如果列表数据的个数是奇数,则列表中间那个数据就是列表数据的中位数;如果列表数据的个数是偶数,则列表中间那2个数据的算术平均值就是列表数据的中位...
  • koko66
  • koko66
  • 2014-10-16 14:14:05
  • 917

1029. Median (25)【排序】——PAT (Advanced Level) Practise

题目信息1029. Median (25)时间限制400 ms 内存限制65536 kB 代码长度限制16000 BGiven an increasing sequence S of N inte...
  • xianyun2009
  • xianyun2009
  • 2015-12-02 21:52:53
  • 1172

两个有序数组的中位数Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/ 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1]...
  • gao1440156051
  • gao1440156051
  • 2016-06-21 11:11:03
  • 4886

MATLAB中median函数的用法

1.功能     求矩阵的中间值。 2.格式     ①median(M)      每一列返回一个值,为M该列的从大到小排列的中间值.     ②median(M,di...
  • wangyang20170901
  • wangyang20170901
  • 2017-12-07 20:40:31
  • 259

Python数据处理 numpy.median

numpy模块下的median作用为: 计算沿指定轴的中位数 返回数组元素的中位数其函数接口为:median(a, axis=None, out=None, ...
  • chaipp0607
  • chaipp0607
  • 2017-07-04 17:46:48
  • 3923

【算法】中位数 median

median 是 STL 内部的算法,用于求三个数的中位数,它将用于 std::sort, std::nth_element 的实现中,快速排序和求第 k 小数算法都用到了划分,选取 pivot 时为...
  • Justme0
  • Justme0
  • 2014-04-26 22:23:39
  • 3884

图像处理滤波器(二)——中值滤波器(Median Filter)

描述:中值滤波器也是为了减少噪声,跟均值滤波器差不多,但是它比均值滤波器保留更多的细节。 什么是中值滤波器? 中值滤波器也是模板滤波器,不过此处的模板只是一个模板,里面没有数字而已...
  • lj695242104
  • lj695242104
  • 2012-02-27 16:50:24
  • 10329

你需知道的MFI:mean, Median,Mode及Geometric Mean 之比较

MFI也许是最经常问到的一个问题。你知道什么时候用Mean,Median, Mode 及Geometry Mean吗?我们来就其数学定义及流式里的应用做一个简单的介绍: Mean(平均数...
  • zhaozhn5
  • zhaozhn5
  • 2017-09-14 23:11:40
  • 487

leetcode之 median of two sorted arrays

这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉。后来搜了一下leetcode的难度分布表(leetc...
  • yutianzuijin
  • yutianzuijin
  • 2013-09-10 00:15:50
  • 80289
收藏助手
不良信息举报
您举报文章:PAT.A1029. Median
举报原因:
原因补充:

(最多只允许输入30个字)