PAT甲级 1029 Median(25 分)

113 篇文章 9 订阅

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值