题目

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)

解题思路

  1. 输入S1,S2并合并,打印中间位置元素
  2. two pointer思想:两个指针,分别指向S1,S2当前元素,比较大小,先将小的合并,并向前移动一位
  3. 合并时优化
    • 1 只要到达中间位置即可打印退出,不需要处理后续数据的合并;
    • 2 哨兵简化合并操作

易错点

  1. S长度分别为偶数和奇数时,中间位置计算为(N1+N2-1)>>1

知识点

  1. 利用哨兵简化合并操作
  2. 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]的更多相关文章

  1. 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 ...

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

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

  3. 【PAT】1029. Median (25)

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

  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 (Advanced Level) 1029. Median (25)

    scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...

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

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

  7. PAT甲题题解-1029. Median (25)-求两序列的中位数,题目更新了之后不水了

    这个是原先AC的代码,但是目前最后一个样例会超内存,也就是开不了两个数组来保存两个序列了,意味着我们只能开一个数组来存,这就需要利用到两个数组都有序的性质了. #include <iostrea ...

  8. 【PAT甲级】1029 Median (25 分)

    题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...

  9. PAT 甲级 1029 Median

    https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...

随机推荐

  1. 十九、SAP查询所有数据库表的所有数据

    一.我们打款SAP自带的一个演示数据库SCARR 二.表结构如下 三.代码如下 四.执行结果如下 我们对比一下数据库的内容,很perfect 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微 ...

  2. 吴裕雄--天生自然C++语言学习笔记:C++ 多线程

    多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序.一般情况下,两种类型的多任务处理:基于进程和基于线程. 基于进程的多任务处理是程序的并发执行. 基于线程的多任务处理 ...

  3. Linux基础操作及概念

    Linux基础操作及概念 终端的概念 通常我们在使用 Linux 时,并不是直接与系统打交道,而是通过一个叫做 Shell 的中间程序来完成的,在图形界面下为了实现让我们在一个窗口中完成用户输入和显示 ...

  4. json - json对象和json字符串直接的相互转换

    一.json字符串转json对象 1.json字符串转json对象 var obj = JSON.parse(str); //由json字符串转换为json对象 2.获取对象的value consol ...

  5. Scrapy的学习和使用

    1.安装 在windows下安装是真心复杂,不搞了,直接在linux下研究吧! sudo pip install scrapy 2.安装遇到问题 2.1.bz2 module not availabl ...

  6. java百货中心供应链管理系统 源码

    开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 百货中心供应链管理系统主要用于实现了企业管理数据统计等.本系统结构如 ...

  7. RecyclerView+FloatingActionButton应用

    一.效果图 二.实现步骤 1.XML布局-添加依赖 <LinearLayout android:id="@+id/layout" android:layout_width=& ...

  8. 干货分享|Law Essay写作高分攻略

    很多法学院的留学生对于Law Essay写作不是特别擅长,理论知识都了解,但是写出来的essay分数就是不高.同学们要从哪些方面入手呢?Law Essay写作要怎么拿高分?具体就跟小编一起来看看吧! ...

  9. Sequence Models Week 1 Improvise a Jazz Solo with an LSTM Network

    Improvise a Jazz Solo with an LSTM Network Welcome to your final programming assignment of this week ...

  10. Java集合--list接口

    list是一个接口,实现类:Arraylist,Vector,Linkedlist list接口(有序): 常用方法 排除Collection中具有的之外的 添加功能 void add(int ind ...