PAT1029.Median (25)
(一)题目
题目链接: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)的更多相关文章
- PAT1029:Median
1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...
- 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 ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 1029 Median (25 分)
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- A1029 Median (25 分)
一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...
- 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 ...
- 1029 Median (25分)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
随机推荐
- iOS开发之Copy & MutableCopy及深复制 & 浅复制
1.使用copy或mutableCopy方法可以创建一个对象的副本. copy: (1)需要实现NSCoppying协议 (2)创建的是不可变副本(如NSString.NSArray.NSDictio ...
- OpenStack及其构成简介
新的一年新的开始,突然想学习下Openstack,之前了解过很多,但是想系统的学习一下,第一次写博客,只想把学到的东西记录下来加深印象,如有写的不好的地方请多多见谅.下面开门见山. 1.What is ...
- jemeter正则表达式
- Linux下搭建mpi集群(ubuntu下用虚拟机测试)
一 建立SSH连接(无密码登陆) 1 SSH连接的简单介绍 SSH 为 Secure Shell 的缩写,中文翻译为安全外壳协议,建立在应用层,是一种远程连接安全协议.传统的telnet,pop,ft ...
- VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug
今天试着用VS 2017去开发一个.net core项目,想着看看.net core的开发和MVC5开发有什么区别,然后从中发现了一个VS2017的Bug. 首先,我们新建项目,ASP.NET Cor ...
- day001-html知识点总结(二)不常见但很重要的元素汇总
一..vertical-align:设置垂直对齐方式,主要用于: 1.单元格内容的垂直对齐 2.对于行内块级元素,如<img>,设置行内元素的基线相对于该行内块级元素的所在行的基线对齐,例 ...
- ecshop3.6商品如何按照销量排序
ecshop订单状态对应值:order_status有5中状态,并且当客户确认收货后,order_status的数值不一定是1也有可能是5.order_status = 0表示订单未确认order_s ...
- 浅谈访问控制列表(ACL)
1.ACL简介2.前期准备3.ACL的基本操作:添加和修改4.ACL的其他功能:删除和覆盖5.目录的默认ACL6.备份和恢复ACL7.结束语 1.ACL简介 用户权限管理始终是Linux系统管理中最重 ...
- 学习react,动手实现一个小demo(仿知乎问答)
学习react也有一周的时间,最近自己做了个仿知乎问答的小demo,项目源码在github上:https://github.com/yang302/reactQa 使用技术:bower+gulp+re ...
- vue实现简单表格组件
本来想这一周做一个关于vuex的总结的,但是由于朋友反应说还不知道如何用vue去写一个组件,所以在此写写一篇文章来说明下如何去写vue页面或者组件.vue的核心思想就是组件,什么是组件呢?按照我的理解 ...