PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968
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 Specification:
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 (≤2×105) 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 Specification:
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
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + 10;
int N, M;
int a[maxn]; int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i ++)
scanf("%d", &a[i]);
a[N + 1] = INT_MAX;
int cnt = 0;
int temp;
scanf("%d", &M);
int mid = (N + M + 1) / 2;
int i = 1;
for(int j = 1; j <= M; j ++) {
scanf("%d", &temp);
while(a[i] < temp) {
cnt ++;
if(cnt == mid) printf("%d", a[i]);
i ++;
}
cnt ++;
if(cnt == mid) printf("%d", temp);
}
while(i < N) {
cnt ++;
if(cnt == mid) printf("%d", a[i]);
i ++;
}
return 0;
}
不能把两个数组合并起来再排序求中位数 会内存超限 先输入第一个数组之后 算出中位数是第几个 然后输入第二个数组 如果输入当前比第一个数组的起始的大 那么 cnt ++ 然后第一个数组向后挪一位 如果一直没有比第一个数组大的那么在第二个数组里继续计数 如果还不到 mid 那么回第一个数组中继续
PAT 甲级 1029 Median的更多相关文章
- 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) 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)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT甲级——A1029 Median
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- 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 ...
- 图论 - PAT乙级 1029 旧键盘 (C++ python3)
PAT甲级 1029 旧键盘 (C++ python3) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. ...
- PAT 1029 Median[求中位数][难]
1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
随机推荐
- 使用docker搭建hadoop环境,并配置伪分布式模式
docker 1.下载docker镜像 docker pull registry.cn-hangzhou.aliyuncs.com/kaibb/hadoop:latest 注:此镜像为阿里云个人上传镜 ...
- CSS2.0实现面包屑
CSS2.0实现面包屑 面包屑这样的 我们以前都是用背景图片做这块工作,但是直到大概2个星期之前在新浪微博上看到用css3.0实现这样的面包屑 但是目前情况下IE6-8并不支持css3.0 只有标准游 ...
- jquery另外一种类似tab切换效果
简要:最近做项目一些效果不能用淘宝kissy框架 所以代码得自己写啊 网上当然有很多组件 但是用他们的代码很多(有的是我不需要的代码) 且还要看API 还不如自己动手写个简单一个,是这么一种简单的效果 ...
- JavaScript模块化思想之入门篇
在写正文之前先写一点废话,从我大三下学期正式接触前端到现在,已经六个月了.自己从HTML,CSS,简单的JS验证开始,一点点开始走入前端的世界.越发的感觉前端这一领域散发着无穷的魅力,也许这和我真心喜 ...
- C++之C++的词法单位
C++的字符集 ASCII码字符集是计算机中的常用字符集.它包括英文字母及阿拉伯数字等128个字符,存储一个ASCII码占用一个字节单元. 由于汉字处理的需要,又出现了汉字国标码等对应于不同语言的字符 ...
- C#数组、js数组、json
C#数组 参考地址C#之数组 什么是数组?数组是一种数据结构,包含同一个类型的多个元素.数组的声明:int[] myIntArray; 注:声明数组时,方括号 [] 必须跟在类型后面,而不是变量名后面 ...
- JQuery radio单选框应用
转载:JQuery判断radio(单选框)是否选中和获取选中值方法总结 一.利用获取选中值判断选中 直接上代码,别忘记引用JQuery包 复制代码 代码如下: < !DOCTYPE html P ...
- 参照示例搭建一个Quertz + Topshelf的一个作业调度服务(基础)
学习网址:Quartz.NET 入门.使用Topshelf创建Windows服务 来自七七资料 1.直接下载源码 2.配置完成后,安装服务测试应用. 以下是遇到情况和加入的一些内容 1.在进行服务安装 ...
- group by having 和where区别联系
原文参考:http://www.51ou.com/browse/msyql/43081.html having 和 where 参考 http://blog.csdn.net/yexudengzhid ...
- 所做更改会影响共用模板Normal.dotm。是否保存此更改
最近安装了Office 2010版本,但是发现个问题,每次在关闭word 2010时,都会提示所做更改会影响共用模板Normal.dotm …… 确实是烦恼,每次都需要点击是否保存,于是我在仔细研究了 ...