Each input file may contain more than one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) 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.
For each test case you should output the median of the two given sequences in a line.
4 11 12 13 14 5 9 10 15 16 17
13
def getMedianNum(array1, array2):
array_len_1 = len(array1)
array_len_2 = len(array2)
if array_len_1 > array_len_2: #保持array1为数组长度小的那个
array_len_1, array_len_2, array1, array2= array_len_2, array_len_1, array2, array1
indexMin = 0 #在array1中查找的左下标
indexMax = array_len_1 #在array1中查找的右下标
mid_len = (array_len_1 + array_len_2 + 1) // 2 #在这里把数组个数平均分,如果为奇数,则左边多一个
while indexMin <= indexMax:
leftIndex = (indexMax + indexMin) // 2 #这个是array1分到左边元素的个数,在此一直二分查找
rightIndex = mid_len - leftIndex #这个是array2分到左边元素的个数,所以这里一直保持着左右平均分数组
#leftIndex+rightIndex一直等于mid_len
#如果第二个数组分到左边的最大数比第一个数组分到右边的最小数要大
if leftIndex < array_len_1 and array2[rightIndex - 1] > array1[leftIndex]:
#则分割数组,array要从左边拿回来一点元素(上面有保持平分数组)
#分割leftIndex都不满足了,那么就在leftIndex+1的右边继续查找
indexMin = leftIndex + 1
#如果第一个数组在左边的最大数比第二个数组在左边的最小数要大
elif leftIndex > 0 and array1[leftIndex - 1] > array2[rightIndex]:
#则分割数组,array1要给多一点右边元素
#leftIndex都不满足了,那么只能在leftIndex-1的左边继续查找
indexMax = leftIndex - 1
else:
if leftIndex == 0:
max_of_left = array2[rightIndex - 1]
elif rightIndex == 0:
max_of_left = array1[leftIndex - 1]
else:
max_of_left = max(array1[leftIndex - 1], array2[rightIndex - 1])
return max_of_left
try:
while True:
array1 = list(map(int,input().split()))
array2 = list(map(int,input().split()))
print(getMedianNum(array1[1:],array2[1:]))
except Exception:
pass