5-7 两个有序序列的中位数 (25分)

xiaoxiao2021-02-28  66

已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A_0, A_1, \cdots, A_{N-1}A0,A1,,AN1的中位数指A_{(N-1)/2}A(N1)/2的值,即第\lfloor(N+1)/2\rfloor(N+1)/2个数(A_0A0为第1个数)。

输入格式:

输入分三行。第一行给出序列的公共长度N(0<<N\le100000),随后每行输入一个序列的信息,即N个非降序排列的整数。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的并集序列的中位数。

输入样例1:

5 1 3 5 7 9 2 3 4 5 6

输出样例1:

4

输入样例2:

6 -100 -10 1 1 1 1 -50 0 2 3 4 5

输出样例2:

1

#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; struct mdian{     int num;//定义存放元素的数 }; struct mdian str[200005];//定义数组用来存放元素 bool compBySize(struct mdian a,struct mdian b){     return a.num<b.num;//按照升序排序 } int main(){     int n;//记录数据     scanf("%d",&n);     int i;//定义外层的计数器     for(i=0;i<2*n;i++){         scanf("%d",&str[i].num);     }     sort(str,str+2*n,compBySize);     printf("%d",str[n-1]); //    for(i=0;i<2*n;i++){ //        printf("%d ",str[i].num);//输出数据有木有问题 //    }     return 0; }

转载请注明原文地址: https://www.6miu.com/read-48397.html

最新回复(0)