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 ...
随机推荐
- mac开发环境配置
折腾了好几天了,终于安装一部分了,mac装的win10,太占空间了,看到学习资源使用的工具,自己搜了一下安装了,在学习使用git的时候,都说mac比win好用多了,我tm为啥抱着mac装win费劲呢! ...
- ASP.NET Core:CMD命令行+记事本 创建Console程序和Web Application
今天看了Scott关于ASP.NET Core的介绍视频,发现用命令行一步一步新建项目.添加Package.Restore.Build.Run 执行的实现方式,更让容易让我们了解.NET Core的运 ...
- java封装的方法
java封装是由Java是面向对象程序设计语言的性质决定的,面向对象程序设计语言的三大特性之一就是封装.封装其实就是包装的意思,从专业的角度来看,就是把对象的所有组成部分组合在一起,保护私有属性. 如 ...
- ajax VS websocket
一. ajax VS websocket总结 http://blog.csdn.net/qiuhuanmin/article/details/50719114 二.用Websocket代替Ajax来开 ...
- jdk源码剖析:Synchronized
开启正文之前,先说一下源码剖析这一系列,就以"死磕到底"的精神贯彻始终,最少追踪到JVM指令(再往下C语言实现了). =========正文分割线=========== Sync ...
- 深度学习实践系列(3)- 使用Keras搭建notMNIST的神经网络
前期回顾: 深度学习实践系列(1)- 从零搭建notMNIST逻辑回归模型 深度学习实践系列(2)- 搭建notMNIST的深度神经网络 在第二篇系列中,我们使用了TensorFlow搭建了第一个深度 ...
- C++11 新特性总结
前言 转载请注明出处,感谢! C++11 的新特性 1 变量和基本类型 1.1 long long 类型 扩展精度浮点数,10位有效数字 1.2 列表初始化 初始化的几种不同形式,其中用花括号来初始化 ...
- python css概述
1. 概述 css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:style ...
- STM32固件库文件分析
STM32固件库文件分析 1.汇编编写的启动文件 startup/stm32f10x.hd.s:设置堆栈指针,设置pc指针,初始化中断向量,配置系统时钟,对用c库函数_main最后去c语言世界里. 2 ...
- Python标准模块--importlib
作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 1 模块简介 Python提供了importlib包作为标准库的一 ...