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

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 (<=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.

Output

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

提交代码

——————————

在leetcode上能找到类似的题目,幸好自己曾经刷过。

找到第K小数。

假设AB中数大于K/2个,比较A[K/2-1]和B[K/2-1]的大小。

如果,A[k/2-1]<B[k/2-1],说明A[0]~A[k/2-1]在合并后都比前K小,可以舍弃。

同理。

下面的写法总是出现问题,主要的原因是因为数组分配的问题,换成指针之后就可以了。


#include <iostream>
#include <cstring>
#include <vector>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

long long int findkth(long long int a[], long long int m, long long int b[], long long int n, long long int k){
	if(m>n) return findkth(b,n,a,m,k);
	if(m==0) return b[k-1];
	if(k==1) return min(a[0],b[0]);
	long long int pa=min(k/2,m),pb=k-pa;
	if(a[pa-1]<b[pb-1]) return findkth(a+pa,m-pa,b,n,k-pa);
	else if(a[pa-1]>b[pb-1]) return findkth(a,m,b+pb,n-pb,k-pb);
	else return a[pa-1];
}

int main(int argc, char** argv) {
	long long int n1,n2;
	long long int a[10010]={0},b[10010]={0};
	
	scanf("%lld",&n1);
	for(long long int i=0; i<n1; i++){
		scanf("%lld",&a[i]);
	}
	scanf("%lld",&n2);
	for(long long int i=0; i<n2; i++){
		scanf("%lld",&b[i]);
	}
	long long int total=n1+n2;
	long long int res=0;
	if(total&0x1) res=findkth(a,n1,b,n2,total/2+1);
	else res=findkth(a,n1,b,n2,total/2);
	 
	printf("%lld\n",res);
	
	
	return 0;
}



AC————————

#include <iostream>
#include <cstring>
#include <vector>
#include <stdlib.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

long long int findkth(long long int a[], long long int m, long long int b[], long long int n, long long int k){
	if(m>n) return findkth(b,n,a,m,k);
	if(m==0) return b[k-1];
	if(k==1) return min(a[0],b[0]);
	long long int pa=min(k/2,m),pb=k-pa;
	if(a[pa-1]<b[pb-1]) return findkth(a+pa,m-pa,b,n,k-pa);
	else if(a[pa-1]>b[pb-1]) return findkth(a,m,b+pb,n-pb,k-pb);
	else return a[pa-1];
}

int main(int argc, char** argv) {
	long long int n1,n2;
	long long int *a,*b;
	
	
	scanf("%lld",&n1);
	a=(long long int *)malloc(sizeof(long long int)*n1);
	for(long long int i=0; i<n1; i++){
		scanf("%lld",&a[i]);
	}
	scanf("%lld",&n2);
	b=(long long int *)malloc(sizeof(long long int)*n2);
	for(long long int i=0; i<n2; i++){
		scanf("%lld",&b[i]);
	}
	long long int total=n1+n2;
	long long int res=0;
	if(total&0x1) res=findkth(a,n1,b,n2,total/2+1);
	else res=findkth(a,n1,b,n2,total/2);
	 
	printf("%lld\n",res);
	
	
	return 0;
}






1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值