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. 添加外键式异常 1215-cannot add foreign key constranint

    添加外键时报错,原因是添加外键的表的字段的字段类型不一致 比如我的第一张表id是int类型,添加约束的dep_id是bigint类型,所以报错,只要把两张表添加约束的字段类型改成统一的即可 本人大学生 ...

  2. eclipse导出可执行jar包步骤

    按步骤图文说明 第一步:选择要导出的工程,右键[export] 第二步:双击Java文件夹下的[Runnable Jar File] 第三步:该步骤分4步走 3.1 从下拉框选择该jar的入口文件,即 ...

  3. Only one complex type allowed as argument to a web api controller action.

    错误内容: message":"An error has occurred.","exceptionMessage":"Only one c ...

  4. Hadoop集群nodes unhealthy解决方法

    在搭建好Hadoop集群之后,所有服务均可正常启动,但是在运行MapReduce程序的时候,发现任务卡在7/09/07 22:28:14 INFO mapreduce.Job: Running job ...

  5. English_word_learning

    这次报名参加了学院的21天打卡活动,说实话,也是想给自己一个积累的平台. 毕竟,真的有时候感觉挺弱的 有的人用了一年考完了四六级,而有人却用四年还未考完. 听到有一位学长因为自己的四级成绩没有达到48 ...

  6. 20155210 Exp5 MSF基础应用

    Exp5 MSF基础应用 一个主动攻击实践,MS08-067 首先利用msfconsole启用msf终端 然后利用search MS08-067搜索漏洞,会显示相应漏洞模块 如图: 根据上图,我们输入 ...

  7. 20155304田宜楠《网络对抗技术》Exp1 PC平台逆向破解(5)M

    Exp1 PC平台逆向破解(5)M 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序 ...

  8. 20155337《网络对抗》Exp5 MSF基础应用

    20155337<网络对抗>Exp5 MSF基础应用 实践目标 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如 ...

  9. lm393

    电压比较芯片,供电电压和输出电压一致.

  10. QT要点

    1. QT设计器最终会被解释为ui_**.h. 2. QString与init之间的转换: QString转int: bool bIsOk; int a = str.toInt( &bIsOk ...