(一)题目

题目链接:https://www.patest.cn/contests/pat-a-practise/1029

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

13
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
(二)题解
(1)题目意思已经很明确了,就是要寻找两个有序数组合并成一个有序数组后中间的那个数。如果合并后数组长度为偶数则输出中间两个数中左边的那个。
因而寻找的数在合并后的下标应该是target = n % 2 ? n / 2 : n / 2 - 1;
(2)我的做法是设置两个下标i,j分别对应s1数组和s2数组。i + j的值就很好的反映了合并后对应的下标。问题是如何标记当前位置是s1[i]还是s2[j]?
期初我是设置了一个flag标记,flag为0就认为是s1[i]否则就认为是s2[j]。测试发现这只能反映上一轮的情况,而不能决定最终的情况。尤其是当i或j走到头的时候。。。
(3)给一些测试用例吧
Input1
4 1 1 1 1
5 2 2 2 2 2
Output1
2 Input2
5 1 2 3 4 5
4 1 6 7 8
Output2
4 Input3
6 1 1 1 1 1 10
5 2 2 2 2 2
Output 3
2 Input4
5 1 2 3 4 5
5 1 2 3 4 5
Output 4
3
(三)AC源码
 #include <bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define For(I,A,B) for(int I = (A); I < (B); I++) long long s1[maxn],s2[maxn];
int n1,n2,n;
int main()
{
freopen("1029.in","r",stdin);
while(scanf("%d",&n1) != EOF)
{
For(i,,n1)
scanf("%lld",&s1[i]);
scanf("%d",&n2);
For(i,,n2)
scanf("%lld",&s2[i]);
n = n1 + n2;
int target = (n % ) ? n / : n / - ;
int i = ,j = ;
//bool flag = 0;
//cout<<target<<" !\n";
while(i < n1 && j < n2 && i + j < target)
{
if(s1[i] < s2[j])
{
i++;
//flag = 0;
}
else
{
j++;
//flag = 1;
}
//cout<<i<<" "<<j<<" "<<endl;
}
if(i + j < target)
{
while(i < n1 && i + j < target)
{
i++;
//flag = 0;
}
while(j < n2 && i + j < target)
{
j++;
//flag = 1;
}
}
//if(j == n2) j--;
//if(i == n1) i--;
if(j == n2 || (i != n1 && s1[i] < s2[j]))
{
printf("%lld\n",s1[i]);
}
else if(i == n1 || s1[i] >= s2[j])
{
printf("%lld\n",s2[j]);
}
}
return ;
}

PAT1029.Median (25)的更多相关文章

  1. PAT1029:Median

    1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...

  2. PAT甲 1029. Median (25) 2016-09-09 23:11 27人阅读 评论(0) 收藏

    1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...

  3. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  4. 1029 Median (25 分)

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  5. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  6. A1029 Median (25 分)

    一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...

  7. PAT Advanced 1029 Median (25) [two pointers]

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

  8. 1029 Median (25分)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  9. PAT 1029 Median (25分) 有序数组合并与防坑指南

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

随机推荐

  1. Windbg调试中遇到的问题

    1.找不到符号文件 抓取完Dump后,打开WinDbg,Ctrl+D找到刚才抓取的Dump文件,报如下异常: *** ERROR: Symbol file could not be found. De ...

  2. Intellij IDEA中文乱码解决

    界面乱码 原因:IDEA默认设置界面的字体不支持中文 解决:修改为支持中文的字体,建议字体Microsoft Yahei UI.大小11,具体操作File -> Setting -> Ap ...

  3. RunTime 给类添加属性

    RunTime网上有很多人都不知道Runtime到底是干嘛的?有很多博主都是长篇大论给他们讲这个讲那个,我感觉还不如实例来的实在.很简单的一个例子:我们都知道会有这样的需求,未读消息列表的图片上要有一 ...

  4. Python_入门

    本章内容: 1.Python的种类 2.Python的环境 3.Python入门(解释器.编码.pyc文件.脚步传入参数.变量.输入.流程控制与缩进.while循环) 4.练习题 Python的种类 ...

  5. js 操作属性

    操作属性: 对象.setAttribute('属性名','值'); - 添加属性 对象.getAttribute('属性名'); - 获取属性值,如无此属性,那么返回null 对象.removeAtt ...

  6. JavaWeb之HTTP协议

    一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...

  7. 性能测试培训:批量执行Jmeter脚本之ant调用

    性能测试培训:批量执行Jmeter脚本之ant调用   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的load ...

  8. [Python Web]部署完网站需要做的一些后续工作

    简述 今天上线了一个简单的 Page,没有什么功能就是一个展示页. 但是,我发现部署完,上线后,还要弄不少东西.下面就是我记录.整理的一些上线网站基本都会用到的网站和配置. 加入统计代码 这个是必做的 ...

  9. 在开源中国(oschina)上创建托管项目

    ***************************************************************** 目标: 1.能上传自己的项目到oschina上并且进行管理 2.能进 ...

  10. selenium实例:unittest框架+PO开发模式

    这是<selenium2+python学习总结>的升级版. 1.         项目结构 2.         项目代码 1)         globalparameter.py # ...