九度OJ-1004:Median

  本题一开始采用了错误的算法,错误代码如下:

#include <cstdio>
#define MAXSIZE 1000010
using namespace std;
long long buf1[MAXSIZE],buf2[MAXSIZE];

int main(){
	int n1,n2;
	int midIndex;
	long long mid;
	while (scanf("%d",&n1)!=EOF){
		//initiate
		//input
		for (int i=0;i<n1;i++){
			scanf("%lld",&buf1[i]);
		}
		scanf("%d",&n2);
		for (int i=0;i<n2;i++){
			scanf("%lld",&buf2[i]);
		}
		midIndex=(n1+n2)/2+(n1+n2)%2;
		//search
		if (n1==0){
			mid=buf2[midIndex-1];
		}
		else if (n2==0){
			mid=buf1[midIndex-1];
		}
		else{
			int p=0,q=0;
			while (p+q!=midIndex){
				//被比过,游标增加,就是刚才的那个数被扫描过了 
				if (buf1[p]<buf2[q]){
					mid=buf1[p];
					p++;
				}
				else{
					mid=buf2[q];
					q++;
				}
			}
		}
		printf("%lld\n",mid);
	}
	return true;
} 

该算法错在没有考虑到两个数列的长度相差悬殊的情况:遍历还在继续,而较短序列已经遍历完毕了。


算法分析:

①若使用排序,O(n*logn)的时间复杂度直逼千万级,一定会TLE,排除。

②若使用hash,题目给的元素范围是“可以用long存储”,那么就是需要10,0000,0000左右的int空间,直接爆内存,排除。

③使用“有序序列归并模型”。先归并,再读取总序列的中间数。复杂度为O(n1+n2),线性级别,完全可以接受。(亦可使其归并到中值时停止,进一步优化效率)

  做完此题后已整理了“有序序列归并模型”的模板。


题目描述:

    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 non-decreasing 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.

输入:

    Each input file may contain more than one test case.
    Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) 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.

输出:

    For each test case you should output the median of the two given sequences in a line.

样例输入:
4 11 12 13 14
5 9 10 15 16 17
样例输出:
13
来源:
2011年浙江大学计算机及软件工程研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-7728-1-1.html
#include <cstdio>
#define MAXSIZE 1000010
using namespace std;
long buf1[MAXSIZE],buf2[MAXSIZE],buf3[MAXSIZE*2];
int main(){
	int n1,n2;
	int p,q,r;
	while (scanf("%d",&n1)!=EOF){
		//input
		for (int i=0;i<n1;i++){
			scanf("%ld",&buf1[i]);
		}
		scanf("%d",&n2);
		for (int i=0;i<n2;i++){
			scanf("%ld",&buf2[i]);
		}
		//merge
		//每次做完一次循环后,pqr都停留在下一个要录入数据的位置 
		p=q=r=0;
		while (p<n1&&q<n2){
			if (buf1[p]<buf2[q]){
				buf3[r]=buf1[p];
				r++;p++;
			}
			else{
				buf3[r]=buf2[q];
				r++;q++;
			}
		}
		while (p<n1){
			buf3[r]=buf1[p];
			r++;p++;
		}
		while (q<n2){
			buf3[r]=buf2[q];
			r++;q++;
		}
		//output
		printf("%ld\n",buf3[(r-1)/2]); 
	}
	return true;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值