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×10​5​​) 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的更多相关文章

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

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

  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)

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

  4. PAT甲级——A1029 Median

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

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

  6. 图论 - PAT乙级 1029 旧键盘 (C++ python3)

    PAT甲级 1029 旧键盘 (C++ python3) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. ...

  7. PAT 1029 Median[求中位数][难]

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

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. php实现链表的基本操作

    <?php class node{ private $value; private $next; public function __construct($value=0,$next=null) ...

  2. 【转】Android中通知的提示音、震动和LED灯效果小例子

    通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...

  3. jenkins发版脚本更新

    jenkins 项目名中明确了 是jar tar.gz war包研发需要提供  项目名-地点-环境(研发.测试.生产)-应用项目名称(admin)-包格式(jar\war\gz) deployment ...

  4. CPP2-基础部分(1)

    参考自<c++ primer 5th zh>,本系列将会接着将<The C++ Programming Language 4th Ed><c++ primer plus ...

  5. jqgrid 配置分页大小及下拉项

    如何配置分页大小的下拉项?显示效果如下: 通过 rowNum 来设置默认分页大小 通过 rowList 来设置分页下拉.   rowList 的值为一个数组,比如:[10,20,30] $(" ...

  6. odoo明细表汇总数据

    一.在主表中#改动地方 总结算金额 求和:def _get_subtotal2(self, cr, uid, ids, field_name, arg, context=None): # 初始化 re ...

  7. EF(EF Core)中的NotMappedAttribute(转载)

    NotMapped特性可以应用到EF实体类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列..NotManpped特性打破了这个约定,你可以使用NotM ...

  8. 基于Docker一键部署大规模Hadoop集群及设计思路

    一.背景: 随着互联网的发展.互联网用户的增加,互联网中的数据也急剧膨胀.每天产生的数据量数以万计,本地文件系统和单机CPU已无法满足存储和计算要求.Hadoop分布式文件系统(HDFS)是海量数据存 ...

  9. Hadoop日记Day11---主从节点接口分析

    一.NameNode 的接口分析 1. NameNode本质 经过前面的学习,可以知道NameNode 本身就是一个java 进程.观察RPC.getServer()方法的第一个参数,发现是this, ...

  10. centos 7 搭建开源堡垒机 Teleport 遇到的问题解决

    不得不说 centos7 的环境版本就是高,没有再出现6.5下那些依赖组件的版本支持过低的错误了,所以说现在个人用户还是推荐用7,企业生产环境的话,可能6.5适合一些,至少2年前是这样. 安装过程:  ...