PAT Advanced 1029 Median (25) [two pointers]
题目
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
题目分析
已知两个有序序列S1,S2,求有序序列S中间位置的元素(S=S1+S2)
解题思路
- 输入S1,S2并合并,打印中间位置元素
- two pointer思想:两个指针,分别指向S1,S2当前元素,比较大小,先将小的合并,并向前移动一位
- 合并时优化
- 1 只要到达中间位置即可打印退出,不需要处理后续数据的合并;
- 2 哨兵简化合并操作
易错点
- S长度分别为偶数和奇数时,中间位置计算为(N1+N2-1)>>1
知识点
- 利用哨兵简化合并操作
- two pointer思想合并两个有序集合为一个有序集合
Code
Code 01(最优、哨兵简化合并、中间位置后数据不处理)
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char * argv[]) {
// 1 输入数据
int N1,N2;
scanf("%d",&N1);
int S1[N1+1]= {0};
for(int i=0; i<N1; i++)scanf("%d",&S1[i]);
scanf("%d",&N2);
int S2[N2+1]= {0};
for(int i=0; i<N2; i++)scanf("%d",&S2[i]);
S1[N1]=S2[N2]=0x7fffffff;// 哨兵
// 2 合并序列--哨兵+若索引到达midpos,直接打印S[midpost],后面的数据不需要处理
int i=0,j=0,index=0,cnt=0,S[N1+N2]= {0};
int midpos = (N1+N2-1)>>1;
while(index<N1+N2) {
if(S1[i]<=S2[j]) S[index++]=S1[i++];
else S[index++]=S2[j++];
if(index-1==midpos) break;
}
printf("%d",S[midpos]);
return 0;
}
Code 02(哨兵简化合并、中间位置后续数据处理)
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char * argv[]) {
// 1 输入数据
int N1,N2;
scanf("%d",&N1);
int S1[N1+1]= {0};
for(int i=0; i<N1; i++)scanf("%d",&S1[i]);
scanf("%d",&N2);
int S2[N2+1]= {0};
for(int i=0; i<N2; i++)scanf("%d",&S2[i]);
S1[N1]=S2[N2]=0x7fffffff;// 哨兵
// 2 合并序列--哨兵
int i=0,j=0,index=0,S[N1+N2]= {0};
while(index<N1+N2) {
if(S1[i]<=S2[j]) {
S[index++]=S1[i++];
} else {
S[index++]=S2[j++];
}
}
printf("%d",S[((N1+N2-1)>>1)]);
return 0;
}
Code 03(无哨兵简化合并、中间位置后续数据处理)
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char * argv[]) {
// 1 输入数据
int N1,N2;
scanf("%d",&N1);
int S1[N1]= {0};
for(int i=0; i<N1; i++)scanf("%d",&S1[i]);
scanf("%d",&N2);
int S2[N2]= {0};
for(int i=0; i<N2; i++)scanf("%d",&S2[i]);
// 2 合并序列
int i=0,j=0,index=0,S[N1+N2]= {0};
while(i<N1&&j<N2) {
if(S1[i]<=S2[j]) {
S[index++]=S1[i++];
} else {
S[index++]=S2[j++];
}
}
while(i<N1)S[index++]=S1[i++];
while(j<N2)S[index++]=S2[j++];
printf("%d",S[((N1+N2-1)>>1)]);
return 0;
}
PAT Advanced 1029 Median (25) [two pointers]的更多相关文章
- 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 ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- 1029 Median (25 分)
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT (Advanced Level) 1029. Median (25)
scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- PAT甲题题解-1029. Median (25)-求两序列的中位数,题目更新了之后不水了
这个是原先AC的代码,但是目前最后一个样例会超内存,也就是开不了两个数组来保存两个序列了,意味着我们只能开一个数组来存,这就需要利用到两个数组都有序的性质了. #include <iostrea ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
随机推荐
- XTU 1205 Range
还是五月湘潭赛的题目,当时就是因为我坑...连个银牌都没拿到,擦. 这个题目枚举区间是不可能的,明显是要考虑每个数对全局的影响,即找到每个数最左和最右能满足是最大的位置 以及 最小的时候,相乘即为该数 ...
- spring第9天(事务)
依赖:spring-context,spring-jdbc(这个本身有依赖spring-tx,关于事务的),druid,mysql-connector-java,aspectjweaver五个 由于我 ...
- CSS实现背景图片透明和文字不透明效果
1.毛玻璃效果:背景图 + 伪类 + flite:blur(3px) width: 500px; height: 300px; line-height: 50px; text-align: cente ...
- html使用aes进行加密
1.导入 aes.js 文件 !function(t,n){*t.length},toString:function(t){);o<r;o++){]>>>-o%*&;n ...
- android:padding和android:margin的区别 android:gravity和 android:layout_gravity 区别
Android的Margin和Padding跟Html的是一样的.如下图所示:橙色边框(一个RelativeLayout或者LinearLayout)为例,最外层灰色为屏幕边框,黄色部分为Paddin ...
- React yarn安装umi后 umi -v查询版本失败
采坑描述:yarn全局安装模块后但仍提示无法找到 解决: 1.先查看一下yarn的bin目录,输入yarn global bin 2.然后将该路径加入到path中,对于windows中直接将该目录加入 ...
- 二十五、CI框架URL辅助函数之base_url函数
一.在UI根目录新建一个pic目录,里面放一个图片,如下 二.在我们打View里面写入一下代码,base_ur函数返回的是网站根目录,代码见附图: 三.两种写法,显示效果如下: 四.我们查看浏览器的源 ...
- 美素数(HDU 4548)(打表,简化时间复杂度)
相信大家都喜欢美的东西,让我们一起来看看美素数吧. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数",如29,本身是素数,而且2+9 = 11 ...
- 广义高斯分布(GGD)和非对称广义高斯分布(AGGD)
<No-Reference Image Quality Assessment in the Spatial Domain>,BRISQUE. 1. 广义高斯分布,generalized G ...
- 201771010123汪慧和《面向对象程序设计Java》第十三周实验总结
一.理论部分 1.GUI为用户提供交互式的图形化操作界面. (1)提供了程序的外观和感觉.(2)程序利用图形用户界面接受用户的输入,向用户输出程序运行的结果. 2.Java有专门的类库生成各种标准图 ...