已知有兩個等長的非降序序列S1, S2, 設計函數求S1與S2并集的中位數。有序序列A_0, A_1, /cdots, A_{N-1}A?0??,A?1??,?,A?N?1??的中位數指A_{(N-1)/2}A?(N?1)/2??的值,即第/lfloor(N+1)/2/rfloor?(N+1)/2?個數(A_0A?0??為第1個數)。
輸入分三行。第一行給出序列的公共長度N(0<<N/le≤100000),隨后每行輸入一個序列的信息,即N個非降序排列的整數。數字用空格間隔。
在一行中輸出兩個輸入序列的并集序列的中位數。
51 3 5 7 92 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>#define max 100005int main(){ int n; int a[max],b[max]; scanf("%d",&n); int i; for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=0;i<n;i++){ scanf("%d",&b[i]); } int c[max*2]; i=0; int j=0,count=0; while(i<n&&j<n){ if(a[i]<b[j]) c[count++]=a[i++]; else c[count++]=b[j++]; } PRintf("%d",c[(2*n-1)/2]); return 0;}
新聞熱點
疑難解答