题目链接在此。
题意
给出两个递增的序列a,b,长度分别为n,m,要求将它们合并成一个新的递增序列后输出中位数。(如果个数为偶数,输出左半部分最后一个数)
思路
首先将两个有序序列合并成一个新的有序序列,之后输出中位数。
AC代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
long long a[1000010], b[1000010], product[2000010];
int main(){
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%lld",&a[i]);
}
scanf("%d",&m);
for(int i = 0; i < m; i++){
scanf("%lld",&b[i]);
}
int i = 0,j = 0;
int index = 0;
while(i < n && j < m){
while( i < n && a[i] < b[j]) product[index++] = a[i++];
while( j < m && a[i] >= b[j]) product[index++] = b[j++];
}
while( i < n) product[index++] = a[i++];
while( j < m) product[index++] = b[j++];
printf("%lld\n",product[(0+index-1)/2]);
return 0;
}