首页 > 试题广场 >

Median

[编程题]Median
  • 热度指数:3132 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.     Given two increasing sequences of integers, you are asked to find their median.

输入描述:
    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.
示例1

输入

4 11 12 13 14
5 9 10 15 16 17

输出

13
头像 wbc990512
发表于 2021-01-26 21:45:47
题意就是找两个升序数组合并后的中位数, 思路:假设a和b数组的大小分别是n1和n2,不进行排序,因为a和b数组本身就是排好序的,直接从小到大读a数组和b数组的元素,第(n1+n2)/2(向上取整)个元素即为所求。 #include<stdio.h> int main() { in 展开全文
头像 bigbigcake
发表于 2024-03-23 09:22:36
#include <bits/stdc++.h> using namespace std; //0 1 2 3 int main() { int n; vector<int>v; while(cin>>n){ while( 展开全文
头像 牛客142529159号
发表于 2023-03-19 00:30:49
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v; int n, x; 展开全文
头像 Moyoj
发表于 2024-03-07 11:26:45
#include <iostream> #include <vector> using namespace std; vector<int> vec1; vector<int> vec2; int solution(int begin1,int end 展开全文

问题信息

难度:
29条回答 4029浏览

热门推荐

通过挑战的用户

查看代码