1029. Median (25)

时间限制
1000 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 nondecreasing 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.

Input

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 (<=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.

Output

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
 
思路
 水题,将两个数组合并后排序就行,注意偶数个数字的数组的median是靠左的数。
代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
vector<int> seq1(n);
for(int i = 0;i < n;i++)
{
cin >> seq1[i];
}
int m;
cin >> m;
vector<int> seq2(m);
for(int i = 0;i < m;i++)
{
cin >> seq2[i];
}
seq1.insert(seq1.end(),seq2.begin(),seq2.end());
sort(seq1.begin(),seq1.end());
cout << seq1[(m + n - 1)/2] << endl;
}
}

  

PAT1029:Median的更多相关文章

  1. PAT1029.Median (25)

    (一)题目 题目链接:https://www.patest.cn/contests/pat-a-practise/1029 1029. Median (25) Given an increasing ...

  2. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  3. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  4. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  5. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  6. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  7. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

    B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...

  8. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  9. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

随机推荐

  1. ceres-solver库使用示例

    上一篇博客大致说明了下ceres-solver库的编译,然后形成了一个二次开发的库,下面就是用这个二次开发库来写一个简单(其实不太简单)的DEMO来演示ceres-solver库的强大.我们以求解一个 ...

  2. Xcode中的全局异常断点

    一旦异常断点被添加,你可以鼠标右键选择 Edit Breakpoint 打开弹出菜单. 改变异常类型为Objective-C,这可以防止C++异常被捕获,你可能不想捕获这些. 因为通常情况下你的App ...

  3. 一个很不错的支持Ext JS 4的上传按钮

    以前经常使用的swfUpload,自从2010年开始到现在,很久没更新了.而这几年,flash版本已经换了好多个,所以决定抛弃swfupload,使用新找到的上传按钮. 新的上传按钮由harrydel ...

  4. Using PL/SQL APIs as Web Services

    Overview Oracle E-Business Suite Integrated SOA Gateway allows you to use PL/SQL application program ...

  5. Struts源码之ValueStack

    /** * ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated ag ...

  6. 用xml来编写动画

    我们可以使用代码来编写所有的动画功能,这也是最常用的一种做法.不过,过去的补间动画除了使用代码编写之外也是可以使用XML编写的,因此属性动画也提供了这一功能,即通过XML来完成和代码一样的属性动画功能 ...

  7. Android使用统计图AChartEngine 来展示数据

    本文采用的统计图参考:AChartEngine 访问地址 :http://code.google.com/p/achartengine/ 先给出效果图 本文的开发代码主要是这些:对一些代码进行修改 以 ...

  8. asp.net core上使用Redis探索(2)

    在<<asp.net core上使用Redis探索(1)>>中,我介绍了一个微软官方实现Microsoft.Extensions.Caching.Redis的类库,这次,我们使 ...

  9. 《MySQL必知必会》读书笔记_4

    PS:一个实际的存储过程案例 CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_delete_article_by_id`(IN `id` int) BE ...

  10. dsd

    nodeName属性 nodeName属性规定节点的名称 nodeName是只读的 元素节点的nodeName与属性名相同 属性节点的nodeName与属性名相同 文本节点nodeName始终是#te ...