题解
题意就是给你两个排好序的增序序列,求两个序列合并后的中位数。
Input Specification:
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 (≤2×10^5 ) 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 Specification:
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
这个题我感觉有点坑,题目指明整数大小不超过long int,但是我在两台电脑跑了一下sizeof(long int)发现一个是4,另一个是8,实际上这题使用long int会内存超限错误,使用int可以AC。所以这个long int到底是什么意思?我觉得有点误导人。
这题解题思路是参考别人的,普通方法最后一个会爆内存。
参考链接
#include <iostream>
#include <cstdio>
#define maxn 200001
using namespace std;
int main()
{
int N, M;
int a[maxn], tmp;
scanf("%d", &N);
for (int i=1; i<=N; i++)
scanf("%d", &a[i]);
scanf("%d", &M);
int cnt=0, mid, i, k=1;
mid = (N+M+1)/2;
for (i=0; i<M; i++)
{
scanf("%d", &tmp);
while(a[k]<=tmp && k<=N)
{
k++;
cnt++;
if (cnt == mid)
printf("%d", a[k-1]);
}
cnt++;
if (cnt == mid)
printf("%d", tmp);
}
if (cnt < mid)
printf("%d", a[k+mid-cnt-1]);
return 0;
}