PAT 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

解答:真的要哭炸了,搞了一天终于AC,一直内存受限,后来自己想到可以不存第二行数据,直接用第二行新读到数据和第一行比较。但是还是内存超了,然后我看了其他博主的代码,真的cry,因为PAT的编译器对long 和int的size大小设定不同,好吧,我一直都是long类型保存数据,使得总是超。教训啊!! 

借鉴了其他博主的代码,主要有两种思路,一是用队列保存第一行数据,再读入第二行数据,一个个比较,每弹出1个,count++,当count = (N1 + N2 - 1) / 2时,两个队列头部的最小值就是要找的值。当然,为了更好地处理,可以在队列尾部加上INT_MAX(定义在#include<climits>中)。二是先保存第一行数据,然后通过移动vector的下标来比较判定,本质上和第一种方法一致。

方法1:AC代码如下:

#include<iostream>
#include<queue>
#include<climits>

using namespace std;

int main()
{
	int N1, N2;
	queue<int> que, que2;
	int val;
	//int int num;
	
	scanf("%d", &N1);
	for(int i = 0; i < N1; ++i){
		scanf("%d", &val);
		que.push(val);	
	}
	que.push(INT_MAX);
	
	scanf("%d", &N2);
	int pops = (N1 + N2 - 1) / 2;
	int count = 0;
	
	for(int i = 0; i < N2; ++i){
		scanf("%d", &val);
		que2.push(val);
		if(count == pops){
			printf("%d\n", min(que.front(), que2.front())); 
			return 0;
		}
		if(que.front() < que2.front()) que.pop();
		else que2.pop();
		count++;
	}
	que2.push(INT_MAX);
	
	while(count < pops){
		if(que.front() < que2.front()) que.pop();
		else que2.pop();
		count++;
	}
	printf("%d\n", min(que.front(), que2.front())); 
	return 0;
}

方法2:AC代码如下:

#include<cstdio>
#include<cstdlib>
#include<climits>
#include<iostream>
using namespace std;

int main()
{
	int N1, N2;
	int* ptr;
	int val;
	
	scanf("%d", &N1);
	ptr = (int*)malloc(sizeof(int)*(N1+1));
	int i;
	for(i = 0; i < N1; ++i){
		scanf("%d", &val);
		*(ptr+i) = val;
	}
	*(ptr+i) = INT_MAX;
	
	scanf("%d", &N2);
	scanf("%d", &val);
	int j = 1, count = 0; i = 0;
	int pops = (N1 + N2 - 1) / 2;
	while(count < pops){
		if(val > *(ptr+i)){
			i++;
		}else{
			if(j < N2){
				scanf("%d", &val);
				j++;
			}else if(j == N2){
				val = INT_MAX;
			}else{
				break;
			}
		}
		count++;
	}
	printf("%d\n", min(val, *(ptr+i)));
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值