题目描述:
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.
样例输入:
4 11 12 13 14
5 9 10 15 16 17
样例输出:

13

此题也可以用sort直接排序,不过时间复杂度就上去了。当然,这题也可以用list容器,最后合并的时候调用成员函数merge()。不过输出中位数的时候只能老老实实遍历。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>using namespace std;int main()
{vector<int> ivec1,ivec2,ivec3;int m,n;while(cin >> m){int tmp;for(int i = 0; i < m; ++i){cin >> tmp;ivec1.push_back(tmp);}cin >> n;for(int i = 0; i < n; ++i){cin >> tmp;ivec2.push_back(tmp);}merge(ivec1.begin(),ivec1.end(),ivec2.begin(),ivec2.end(),back_inserter(ivec3));cout << ivec3[(ivec3.size() - 1)/2] << endl;ivec1.clear();ivec2.clear();ivec3.clear();}return 0;
}

1004.Median相关推荐

  1. 九度OJ : 1004 Median

    这题只有一个测试用例,只要过了我给的这个,应该就没问题 1 1 5 5 6 7 8 9 代码逻辑有点乱... code: #include <bits/stdc++.h> using na ...

  2. 九度OnlineJudge之1004:Median

    题目描述: Given an increasing sequence S of N integers, the median is the number at the middle position. ...

  3. PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)

    1. Tensor 归并运算函数 此类操作会使输出形状小于输入形状,并可以沿着某一维度进行指定操作,如加法, 既可以计算整个 tensor 的和,也可以计算 tensor 每一行或者 每一列的和, 常 ...

  4. 【ACM】杭电OJ 1004

     题目链接:杭电OJ 1004 运行环境:Dev-C++ 5.11 思路: 先把先把num数组全部赋值为1:第一个颜色单独输入,从第二个开始,需要与前面的进行比较,如果前面有相同的颜色,则在目前的nu ...

  5. sicily 1004 I Conduit!

    2019独角兽企业重金招聘Python工程师标准>>> Description Irv Kenneth Diggit works for a company that excavat ...

  6. 【Math Time 】Mean, Median, Mode 之间的区别

    写在前面的话 我们的世界可以说是由各种各样的数据组成的,不同人的身高的集合值,宇宙中星星的亮度值,或者说一个人脸上的青春痘的个数... 有的数值很简单,有的数据集又很盘大,我们为了理解一些数据的规律提 ...

  7. R语言编写自定义函数基于ggsumarystats函数计算每个分组的统计值、自定义可视化分组分面条形图,并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr)

    R语言编写自定义函数基于ggsumarystats函数计算每个分组的统计值.自定义可视化分组分面条形图,并在X轴标签下方添加分组对应的统计值(样本数N.中位数median.四分位数的间距iqr) 目录

  8. R语言编写自定义分组统计函数(customize statistics function)可视化分组箱图并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr)

    R语言编写自定义分组统计函数(customize statistics function)可视化分组箱图并在X轴标签下方添加分组对应的统计值(样本数N.中位数median.四分位数的间距iqr) 目录

  9. seaborn可视化displot绘制直方图(histogram)并通过axvline函数在直方图中添加中位数(median)竖线(自定义中位数竖线的线条形式)

    seaborn可视化displot绘制直方图(histogram)并通过axvline函数在直方图中添加中位数(median)竖线(自定义中位数竖线的线条形式) 目录

最新文章

  1. WinCE Heartbeat Message的实现
  2. ps 替换文字_这可能是PS最难用的功能!解决PS字体列表硬伤的利器FonTags
  3. Python绘制雷达图展示学生各科考试成绩
  4. 关于如何在ASP.NET 2.0中定制Expression Builders
  5. WebService高级,可靠消息
  6. 怒了,以后有文件保存到网上
  7. 2008秋季-计算机软件基础- vc6 wintc 编译器
  8. Windows\Ubuntu修复UEFI引导
  9. Godaddy、Lunarpages、IXwebhosting国外三大主机点评
  10. 程序员小故事-战术上的勤奋和战略上的懒惰
  11. 最小生成树算法 | Kruskal’s Minimum Spanning Tree Algorithm
  12. Kotlin - 面向对象之抽象类与接口
  13. 高端进销存管理系统源码+微信小程序,
  14. 覃卫平java,湖南省职业教育“十一五”省级重点建设项目验收通过项目.doc
  15. 一个三线程序员的2020年,CSDN 10 万粉里程碑达成,SpringBoot项目瘦身指南
  16. 从nmn临床研究最新消息背后,我认识到了w+nmn和nmn的区别重要性
  17. 【技术分享】什么是K折交叉验证?
  18. 两个月的面试真实经历,告诉大家如何能够进入大厂工作?
  19. React 超详细入门教程
  20. Hibernate官方下载

热门文章

  1. JZOJ 4826. 【NOIP2016提高A组集训第2场10.30】小澳的葫芦
  2. 将一段话换一种方式的软件v.1.2.3
  3. 手机屏幕分辨率、PPI像素密度科普知识大全
  4. golang 数据类型
  5. 历史渊源:闲话操作系统
  6. 22考研一战上岸上海交通大学经验贴
  7. uni-app 小说滑动阅读功能实现
  8. 联想Y470P硬件升级SSD+DDR全攻略
  9. linux的虚拟文件系统二(数据结构)
  10. R语言学习由浅入深路线