1029 Median

1029 Median (25 分)

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 Specification:

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 (≤2×10​^5​​) 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 Specification:

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 <queue>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

queue<int> q1, q2;

int main(int argc, char** argv) {
	int N1, N2, temp;
	scanf("%d", &N1);
	for(int i = 0; i < N1; i++){
		scanf("%d", &temp);
		q1.push(temp);
	}
	q1.push(INF); //将INF进队,这样在下面的比较时,就不会出现空队的情况,方便处理
	scanf("%d", &N2);
	int cnt = (N1 + N2 - 1) / 2; //cnt表示中位数的位置
	for(int i = 0; i < N2; i++, cnt--){
		scanf("%d", &temp);
		q2.push(temp);
		if(cnt > 0){ //找到中位数,不用再比较了
			if(q1.front() < q2.front()) q1.pop();
			else q2.pop();
		}
	}
	q2.push(INF);
	for(; cnt > 0; cnt--){ //有可能q2全部出队以后还没有找到中位数
		if(q1.front() < q2.front()) q1.pop();
		else q2.pop();
	}
	printf("%d", min(q1.front(), q2.front()));
	return 0;
}

 

### Median 函数或方法的实现与使用 在编程中,`median` 方法或函数用于计算一组数据的中位数。以下是关于 `median` 函数或方法的详细解释和实现方式。 #### 1. 中位数的基本定义 中位数是将一组数据按大小顺序排列后处于中间位置的值。如果数据点的数量为奇数,则中位数为正中间的值;如果数据点的数量为偶数,则中位数为中间两个值的平均值[^1]。 #### 2. Python 中的 `median` 实现 以下是一个自定义的 `median` 函数实现,适用于 Python: ```python def median(ls): sorted_ls = sorted(ls) # 对列表进行排序 n = len(sorted_ls) if n % 2 == 0: # 如果列表长度为偶数 mid_idx = n // 2 median = (sorted_ls[mid_idx - 1] + sorted_ls[mid_idx]) / 2 else: # 如果列表长度为奇数 mid_idx = n // 2 median = sorted_ls[mid_idx] return median ``` 示例代码如下: ```python ls1 = [26, 92, -5, 13, 6] ls2 = ls1 + [117, -25, 73] print("列表ls1的中位数是{}".format(median(ls1))) # 输出:列表ls1的中位数是13 print("列表ls2的中位数是{}".format(median(ls2))) # 输出:列表ls2的中位数是26 ``` #### 3. 使用 NumPy 计算中位数 Python 的第三方库 NumPy 提供了内置的 `median` 函数,可以直接用于计算中位数: ```python import numpy as np ls1 = [26, 92, -5, 13, 6] ls2 = ls1 + [117, -25, 73] print("列表ls1的中位数是", np.median(ls1)) # 输出:列表ls1的中位数是13.0 print("列表ls2的中位数是", np.median(ls2)) # 输出:列表ls2的中位数是26.0 ``` #### 4. R 语言中的 `median` 函数 在 R 语言中,`median` 函数用于计算向量的中位数。例如: ```r y <- c(88, 5, 12, 13) median(y) # 输出:12.5 ``` R 语言提供了丰富的统计功能,因此其 `median` 函数可以直接应用于各种数据结构[^2]。 #### 5. 图像处理中的中值滤波 在图像处理领域,中值滤波是一种非线性滤波技术,用于去除噪声并保留图像边缘信息。OpenCV 库中的 `medianBlur` 函数实现了这一功能。以下是一个示例: ```python import cv2 import numpy as np # 创建一个包含噪声的图像 image = np.zeros((100, 100), dtype=np.uint8) image[40:60, 40:60] = 255 # 添加一个白色方块 image = cv2.randu(image, 0, 255) # 添加随机噪声 # 应用中值滤波 filtered_image = cv2.medianBlur(image, 5) cv2.imshow('Original Image', image) cv2.imshow('Filtered Image', filtered_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 中值滤波通过计算窗口内像素值的中位数来替代中心像素值,从而达到去噪的效果[^3]。 #### 6. 数据库查询中的中位数计算 在数据库查询中,可以结合 SQL 和窗口函数计算中位数。例如,在 Oracle 数据库中,可以通过以下查询计算员工工资的中位数[^4]: ```sql SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary FROM employees; ``` ### 注意事项 - 在计算中位数时,确保输入数据已正确排序。 - 对于大规模数据集,优先使用优化后的库函数(如 NumPy 或 Pandas)以提高性能。 - 在图像处理中,选择合适的窗口大小对结果至关重要。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值