(一)题目

题目链接: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. oracle学习 笔记(2)

    题记:在使用Oracle数据库的时候,发现Oracle是没有自动增长列来实现主键的,所以在此记录学习.(PS:如果哪里有错误或者不足的地方还请大家帮忙指出来) 二.序列(自动增长列) 为此问题博主也是 ...

  2. vue简易轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. css的存在方式和选择器

    css的存在方式 元素内联 页面嵌入 外部引入 元素内联 直接在html的标签中定义样式,类似于: <div style="属性1;属性2;属性3"><div&g ...

  4. 如何记录selenium自动化测试过程中接口的调用信息

    上一篇博客,我写了python自动化框架的一些知识和粗浅的看法,在上一篇中我也给自己提出一个需求:如果记录在测试过程中接口的调用情况?提出这个需求,我觉得是有意义的.你在测试过程中肯定会遇到一些莫名其 ...

  5. 工作中的趣事:聊聊ref/out和方法参数的传递机制

    0x00 前言 我在之前的游戏公司工作的时候,常常是作为一只埋头实现业务逻辑的码农.在工作之中不常有同事会对关于编程的话题进行交流,而工作之余也没有专门的时间进行技术分享.所以对我而言上家虽然是一家游 ...

  6. MySQL---事务知识,你搞明白没有?

    MySQL - 事务 在学习事务这一概念前,我们需要需要构思一个场景 场景构思 假设该场景发生于一个银行转账背景下,月中,又到了发工资的日子.潭州教育科技集团打算给Tuple老师发放一个月的工资.(此 ...

  7. h5开发app之在线生成二维码

    h5通过jquery和qrcode在线生成二维码 首先我们需要下载一个qrcode.js文件,然后依次引入jquery和qrcode文件. 1.创建一个输入框以便做演示使用: <input id ...

  8. 使用VSCode 断点调试 js项目,html页面

    一.效果目的 1.在VSCode里,直接F5打开html页面,并且可以在编辑器里,进行断点调试js代码: 二.工具准备 1.VSCode 软件 2.一个js项目 3.VSCode 上装一个插件:Deb ...

  9. 微信小程序首页总结

      效果图如下 首先从大的方面来讲,就是设置了window的属性 "navigationBarBackgroundColor": "#AFE2E6",//bar ...

  10. 这个demo是为解决IQKeyboardManager和Masonry同时使用时,导航栏上移和make.right失效的问题

    原文链接在我的个人博客主页 (一).引言: 在 IQKeyboardManager 和 Masonry 同时使用时,导航栏上移和make.right失效等问题多多. 其实我们完美的效果应该是这样的:* ...