PAT1029:Median
1029. Median (25)
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
#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的更多相关文章
- PAT1029.Median (25)
(一)题目 题目链接:https://www.patest.cn/contests/pat-a-practise/1029 1029. Median (25) Given an increasing ...
- 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 ...
- [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 ...
- [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 ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【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 ...
- 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 ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- Find Median from Data Stream
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
随机推荐
- JavaScript进阶(五)js中取小数整数部分函数
js中取小数整数部分函数 丢弃小数部分,保留整数部分 js:parseInt(7/2) 向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 四舍五入 js: Math.round(7 ...
- 09_EGIT插件的安装,Eclipse中克隆(clone),commit,push,pull操作演示
1 下载EGIT,下载地址:http://www.eclipse.org/egit/download/ 最终的下载地址: http://www.eclipse.org/downloads/dow ...
- 对Java配置文件中敏感信息进行加解密的工具类
在 JavaEE 配置文件中,例如 XML 或者 properties 文件,由于某些敏感信息不希望普通人员看见,则可以采用加密的方式存储,程序读取后进行解密. 常见的如: 数据库用户密码,短信平台用 ...
- Android群英传笔记——第六章:Android绘图机制与处理技巧
Android群英传笔记--第六章:Android绘图机制与处理技巧 一直在情调,时间都是可以自己调节的,不然世界上哪有这么多牛X的人 今天就开始读第六章了,算日子也刚好一个月了,一个月就读一半,这效 ...
- Android Data Binding高级用法-Observable、动态生成Binding Class(三)
设置View的id 虽然说Data Binding这种分层模式使得我们对数据的传递简单明了,一般情况下我们可以不设置View的id,不使用findViewById即可对View进行数据上一系列的操作, ...
- XMPP系列(二)----用户注册和用户登录功能
1.创建一个新工程 2.导入XMPP框架 最新的XMPP框架下载地址:https://github.com/robbiehanson/XMPPFramework 将XMPP的几个文件夹拖进工程中,需要 ...
- 30多种iOS常用动画
转自:http://blog.csdn.net/zhibudefeng/article/details/8691567 // // CoreAnimationEffect.h // CoreAni ...
- 面试之路(8)-BAT面试题之数组和链表的区别
两种数据结构都是线性表,在排序和查找等算法中都有广泛的应用 各自的特点: 数组: 数组是将元素在内存中连续存放,由于每个元素占用内存相同,可以通过下标迅速访问数组中任何元素.但是如果要在数组中增加一个 ...
- 解读Raft(三 安全性)
前言 之前的两篇文章更多的是在描述Raft算法的正常流程,没有过多的去讨论异常场景. 而实际在分布式系统中,我们更多的都是在应对网络不可用.机器故障等异常场景,所以本篇来讨论一下Raft协议的安全性, ...
- 二叉查找树之 Java的实现
参考:http://www.cnblogs.com/skywang12345/p/3576452.html 二叉查找树简介 二叉查找树(Binary Search Tree),又被称为二叉搜索树.它是 ...