PAT甲级A1029 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

题意:给定两个有序序列长度分别为n,m,让你求他们的中位数下标为(n+m-1)/2,奇数个中位数在中间,偶数个是前一半的最后一个。

思路:

1.输入两个序列并合直接取中,但会超时超内存。

2.使用两个队列q1,q2分别读取这两个序列,然后计算出中位数是第(n+m-1)/2个(下标从0开始),逐个比较q1中队首元素和    q2中队首元素,并将小的出队,最后输出中位数,为了简化代码,将int型最大数inf(=1(<<31)-1)分别入队,这样队列永不为        空,方便比较。新版PAT会导致最后一个案例超内存。

3.优化2的方法,对于序列2,直接边读取,边和q1队首元素比较。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf=(1<<31)-1;
queue<int> q1;
int main()
{
	int n,m,t,t1,t2;
	scanf("%d",&n);
	for(int i=0;i<n;i++) {
		scanf("%d",&t);
		q1.push(t);
	}
	q1.push(inf);
	scanf("%d",&m);
	int cnt=0,mid=(n+m-1)/2;
	for(int i=0;i<m;i++) {
		scanf("%d",&t);
		while(q1.front()<=t&&cnt<mid){			
			cnt++;
			q1.pop();
		}
		if(cnt>=mid) break;			//注意这句不能放在最后,否则下例会输出12
		if(q1.front()>t&&cnt<mid)	//5 9 10 15 16 17
			cnt++;					//4 11 12 13 14
	}
	while(cnt<mid) {
		q1.pop();
		cnt++;
		t=inf;
	}
	printf("%d\n",min(q1.front(),t));
	return 0;
}

 

### 关于PTA甲级1029题目的解析 #### 题目描述 PTA甲级1029题目名为 **"Median"**,其主要目标是计算一组数的中位数。具体来说,给定一系列整数,要求按照从小到大的顺序排列这些整数,并返回其中位数。 - 如果序列长度为奇数,则中位数为中间位置的那个数; - 如果序列长度为偶数,则中位数为中间两个数的平均值。 以下是更详细的输入输出说明: #### 输入规格 输入的第一行为一个正整数 \(N\) (\(1 \leq N \leq 10^5\)),表示后续待处理的数据数量。 第二行为 \(N\) 个由空格隔的整数,范围在 \([-10^6, 10^6]\) 内。 #### 输出规格 输出一行,包含一个浮点数,保留一位小数,表示该组数据的中位数。 --- #### 解决方案 为了高效求解此问题,可以采用以下方法: 1. 使用 C++ 的 `std::vector` 或 Python 的列表来存储输入数据。 2. 对数组进行排序操作,时间复杂度为 \(O(N \log N)\),这是必要的步骤以便找到中位数的位置。 3. 判断数组长度的奇偶性: - 若为奇数,直接取中间索引对应的数值作为中位数。 - 若为偶数,取中间两个数的均值作为中位数。 下面是基于上述逻辑实现的具体代码示例: ```cpp #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> nums(N); for (int i = 0; i < N; ++i) { cin >> nums[i]; } sort(nums.begin(), nums.end()); double median; if (N % 2 == 1) { // 奇数情况 median = static_cast<double>(nums[N / 2]); } else { // 偶数情况 median = (static_cast<double>(nums[(N / 2) - 1]) + static_cast<double>(nums[N / 2])) / 2.0; } printf("%.1f\n", median); // 控制输出精度至小数点后一位 return 0; } ``` 对于 Python 实现版本,也可以非常简洁地完成相同功能: ```python import sys def find_median(): input_data = sys.stdin.read() lines = input_data.splitlines() n = int(lines[0]) numbers = list(map(int, lines[1].split())) numbers.sort() if n % 2 == 1: median = float(numbers[n // 2]) else: mid1 = numbers[n // 2 - 1] mid2 = numbers[n // 2] median = (mid1 + mid2) / 2 print(f"{median:.1f}") find_median() ``` 以上两种语言的解决方案都能满足题目需求,并且具有较高的效率[^1]。 --- #### 测试案例 假设输入如下: ``` 5 1 3 5 2 4 ``` 经过排序后的序列为 `[1, 2, 3, 4, 5]`,因此中位数为第 3 个元素即 `3.0`。 如果输入改为: ``` 6 1 3 5 2 4 6 ``` 则排序后得到 `[1, 2, 3, 4, 5, 6]`,此时中位数为 `(3 + 4) / 2 = 3.5`。 --- #### 性能优化提示 尽管标准算法已经足够快速,但在极端情况下(如接近最大输入规模),仍需注意内存配和 I/O 效率。例如,在 C++ 中可以通过提前预留向量大小减少动态扩展开销;而在 Python 中可考虑使用更快的文件读写方式替代默认的标准输入流[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值