PAT甲级 1029 Median(25 分)

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

第一种方法:内存超,只能得16分。采用vector存储所有元素并sort,找到中间元素的方法。

#include<iostream>
#include<vector> 
#include<algorithm>
using namespace std;

int cmp(int a,int b){
	return a < b;
}

int main(){
	int n,m;
	cin>>n;
	vector<long long> arr(n);
	for(int i=0;i<n;i++)
		cin>>arr[i];
	cin>>m;
	for(int i=0;i<m;i++){
		int a;
		cin>>a;
		arr.push_back(a);
	}
	sort(arr.begin(),arr.end(),cmp);

	if((n+m)&0x1 == 1)//奇数 
		cout<<arr[(n+m)/2];
	else
		cout<<arr[(n+m)/2 - 1];
	
	return 0;
}

 第二种方法:用两个队列,先将第一行数据输入a队列,第二行在边输入数据的过程中边丢弃数据。

#include <iostream>
#include <queue>
#include <stdio.h>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
	int n, m;
	cin >> n;
	queue<int> a, b;
	for (int i = 0; i<n; i++)
	{
		long long num;
		scanf("%lld",&num);
		num = min((long long)INT_MAX, num);
		a.push(num);
	}
	a.push(INT_MAX);//防止a为空,这样做仍可与b进行比较 
	cin >> m;
	int cnt = 0;
	for (int i = 0; i<m; i++)
	{
		long long num;
		scanf("%lld",&num);//不通过的原因是前面用cin 
		num = min((long long)INT_MAX, num);
		b.push(num);
		if (cnt == (n + m - 1) / 2)
		{
			cout << min(a.front(), b.front());
			return 0;
		}
			
		if (a.front() < b.front()) {
			a.pop();
		}	
		else {
			b.pop();
		}
		cnt++;		
	}
	b.push(INT_MAX); 

	for (; cnt < (n + m - 1) / 2; cnt++)
	{
		if (a.front() < b.front())
			a.pop();
		else
			b.pop();
	}
	cout << min(a.front(), b.front());

	return 0;
}

解法三:开一个数组,在线处理第二个数组。 第一二个数组分别有n,m个元素,需要丢掉前(n + m - 1) / 2个小的数,接着输出答案。所以只要从小到大数到(n + m - 1) / 2的位置就行了~ count计总个数 ,给第一个数组设置指针index,每次从第二个数组中读入temp,检查第一个数组中前几个数是不是比temp小,小就先判断是否到(n + m -1) / 2,到了就输出,否则index+1和count+1。 如果数完比temp小的数还没到中间数,检查是否到(n + m -1) / 2,是就输出,否则count+1。循环上述过程。如果第二个数组读取完了,还没数到中间数,说明中间数在剩下的第一个数组中,就在剩下的数组中依次判断是否到(n + m -1) / 2,再count+1即可。

#include<iostream>
#include<stdio.h>
#include<vector> 
using namespace std;

int main(){
	int n,m;
	cin>>n;
	vector<int> arr(n+1);
	for(int i=0;i<n;i++)
		scanf("%d",&arr[i]);
	arr[n] = 0x7fffffff;//防止越出 
	cin>>m;
	
	int temp,index = 0,count = 0;
	for(int i=0;i<m;i++){
		scanf("%d",&temp);
		while(arr[index] < temp){
			if(count == (n+m-1)/2){
				printf("%d\n",arr[index]);
				return 0;
			}
			count++;
			index++;
		}
		
		if(count == (n+m-1)/2){
			printf("%d\n",temp);
			return 0;
		}
		count++;	
	} 
	
	for(int i=index;i<n;i++){
		if(count == (n+m-1)/2)
			printf("%d\n",arr[i]);
		count++;
	}
	
	return 0;
}

 

### PAT 甲级 真题 1057 解析 #### 题目描述 题目要求实现一个特殊的栈结构,除了基本的 `Push` 和 `Pop` 操作外,还需要支持一个新的操作 `PeekMedian`。该操作返回当前栈中所有元素的中位数值。对于偶数个元素的情况,定义中位数为第 \( \frac{N}{2} \) 小的元素;对于奇数个元素,则取第 \( \left(\frac{N+1}{2}\right) \) 小的元素[^2]。 #### 实现方法一:基于树状数组的方法 为了高效处理大量的查询请求而不至于超时,可以采用树状数组来追踪栈内的元素布情况,并通过二法定位所需的中位数位置。这种方法的时间复杂度大约为 \(O(n\log n)\),能够满足性能需求[^1]。 ```cpp // 示例代码片段展示如何利用树状数组解决此问题 #include <iostream> using namespace std; const int MAX_N = 1e5 + 5; int c[MAX_N], a[MAX_N]; void update(int i, int delta){ while(i<MAX_N){ c[i]+=delta; i+=i&(-i); } } int sum(int i){ int s=0; while(i>0){ s+=c[i]; i-=i&(-i); } return s; } ``` #### 实现方法二:使用 multiset 维护中位数指针的方式 另一种解决方案涉及到了 C++ STL 中的容器——`multiset`。借助于这个有序集合的数据特性以及额外设置的一个指向中位数位置的迭代器,在执行插入或删除动作的同时调整迭代器的位置即可快速获取最新的中位数。 ```cpp // 使用 multiset 的C++实现方式 #include <set> typedef long long LL; #define PB push_back #define MP make_pair #define FI first #define SE second #define PII pair<int,int> class MedianFinder { public: /** initialize your data structure here. */ set<pair<int ,LL>> S; // 存储数据及其版本号 set<pair<int ,LL>>::iterator it; // 记录中位数所在位置 void addNum(int num) { static LL ver = 0; ++ver; auto p = MP(num, ver); if(S.size() == 0 || S.size()%2==0){ S.insert(p),it=S.find(p); }else{ bool flag=false; if(it->FI<num){ if(next(it)!=S.end()){ if((next(it)->FI)>num){ --it,S.insert(p); flag=true; } } }else{ if(it!=S.begin()){ if(prev(it)->FI<=num){ ++it,S.insert(p); flag=true; } } } if(!flag)S.insert(p); } } double findMedian(){ int sz=(int)(S).size(); if(sz%2==0)return (*prev(it)).FI*0.5+(*it).FI*0.5; else return *it->FI; } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值