您的位置:首页 > 其它

1029. Median (25)

2015-08-12 09:30 561 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: