本文共 787 字,大约阅读时间需要 2 分钟。
已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0,A1,⋯,AN−1的中位数指A(N−1)/2的值,即第⌊(N+1)/2⌋个数(A0为第1个数)。
输入分三行。第一行给出序列的公共长度N(0<N≤100000),随后每行输入一个序列的信息,即N个非降序排列的整数。数字用空格间隔。
在一行中输出两个输入序列的并集序列的中位数。
51 3 5 7 92 3 4 5 6
4
6-100 -10 1 1 1 1-50 0 2 3 4 5
1
#include#define LIST_SIZE 100000int S1[LIST_SIZE],S2[LIST_SIZE];//直接求索引int Dealwith(n){ int i = 0,j = 0; int m = (2*n-1)/2; while(i + j < m) { if(S1[i] >= S2[j]) { j++; } else i++; } return S1[i]>S2[j]?S2[j]:S1[i];}int main(){ int n; scanf("%d",&n); int i; for(i = 0;i < n;i++) { scanf("%d",&S1[i]); } for(i = 0;i < n;i++) { scanf("%d",&S2[i]); } printf("%d\n",Dealwith(n)); return 0;}
转载地址:http://bkbwk.baihongyu.com/