PAT甲级——A1029 Median
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 Specification:
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 (≤) 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 Specification:
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
这道题,我要说道说道;
这道题和考究内存的使用,如果你的代码会去保存输入的两组数据,我相信,测试会提示“超内存”的错误;
所以,就保存一组数据,与另一组数据进行比较就行,不用保存;
一般简单的int数组远比任何容器都省内存!!!
#include <iostream>
using namespace std;
int main()
{
int res[], m, n, i, j, a, index = ;
cin >> m;
for (i = ; i <= m; ++i)
cin >> res[i];
cin >> n;
for (i = ,j=; i <= n; ++i)
{
cin >> a;
while (j <= m && a > res[j])
{
++index;
if (index == (m + n + ) / )
{
cout << res[j] << endl;
return ;
}
++j;
}
++index;
if (index == (m + n + ) / )
{
cout << a << endl;
return ;
}
}
while(j <= m)
{
++index;
if (index == (m + n + ) / )
{
cout << res[j] << endl;
return ;
}
++j;
}
return ;
}
PAT甲级——A1029 Median的更多相关文章
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- PAT甲级题分类汇编——理论
本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
随机推荐
- fatal error U1087: cannot have : and :: dependents for same target Stop.
转自VC错误:http://www.vcerror.com/?p=72 问题描述: 完成后编译,发现有错误 D:\WinDDK\7600.16385.1\bin\makefile.new(7117) ...
- 全局CSS设置
全局CSS设置 1.清除所有的标记的内外边距 body,ul,li,a,img,p,input{ margin:0; padding:0; } 2.去除项目符号或编号前面的符号 ul,ol,li{ l ...
- C++开发系列-友元函数 友元类
友元函数 默认一个类的私有属性只能在该类的内部可以直接访问.友元函数申明在内的内部,实现在类的外部可以直接访问类的私有属性. class A1 { public: A1() { a1 = 100; a ...
- CF755F PolandBalls and Gifts
题意:给你一个礼物的置换.有k个人忘带了礼物.一个人无法获得礼物为他自己没有带礼物或给他带礼物的那个人没有带礼物.求选择k个人,没有获得礼物的人数的最小值和最大值. n,k<=1e6. 标程: ...
- python csv write 乱码
参考 : https://www.zhihu.com/question/34201726 1.使用utf_8_sig with open('d:/file.csv', 'w', encoding='u ...
- 深入理解Java虚拟机(类文件结构)
深入理解Java虚拟机(类文件结构) 欢迎关注微信公众号:BaronTalk,获取更多精彩好文! 之前在阅读 ASM 文档时,对于已编译类的结构.方法描述符.访问标志.ACC_PUBLIC.ACC_P ...
- angular组件之间的通信
一.组件创建 直接使用 ng g component 的命令创建组件,会自动生成组件所需的文件,方便快捷. // 父组件 ng g component parent // 子组件 ng g compo ...
- JDK源码阅读--Object
在java.lang包下 Object类:是所有类的基类(父类) public final native Class<?> getClass(); 返回这个Object所代表的的运行时类 ...
- HttpServletRequest request 获取当前登录的用户-获取当前用户
有的业务需要知道当前登录的用户 当然需要引用这个啦 import javax.servlet.http.HttpServletRequest; 然后 HttpSession session = req ...
- 如何学习AxureRP Axure学习方法
从作者最初接触的5.5版本,到5.6版本,到后来6.0的多个迭代版本,直到现在的6.5版本,AxureRP每次的版本升级都伴随着新功能的增 加,也解决了原型设计上的一些难题.这也从另一个方面诠释了“学 ...