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 ...
随机推荐
- php请求API接口方法
thinkphp下直接放入公共函数即可. /** * 通过URL获取页面信息 * @param string $url 地址 * @return string 返回页面信息 */ function g ...
- Leetcode——64. 最小路径和
题目描述:题目链接 同样对于这个问题,我们可以考虑用动态规划来解决. 解决动态规划常见的三个步骤: 1:问题的归纳.对于 i,j 位置上的最短路径可以用d[ i ][ j ]表示. 2:归纳递推式:d ...
- memset()初始化为1的那些事
问题代码: #include <stdio.h> #include <string.h> int main() { ]; int a; while(~scanf("% ...
- 利用备份技术获取apk本地存储数据
即使设备没有root,我们也可以通过物理访问设备来获取应用程序的数据,我们还可以通过此方法改变一个应用程序的数据.如果一个应用程序将数据存储在客户端, 使用简单的密码或pin检查,攻击者有可能使用这种 ...
- go语言之行--接口(interface)、反射(reflect)详解
一.interface简介 interface(接口)是golang最重要的特性之一,Interface类型可以定义一组方法,但是这些不需要实现.并且interface不能包含任何变量. 简单的说: ...
- 20155206赵飞 Exp1PC平台逆向破解及Bof基础实践
实验一 逆向及Bof基础 1.掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP汇编指令的机器码是"90" JNE汇编指令的机器码是"75" ...
- 20155325 Exp6 信息搜集与漏洞扫描
实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的 ...
- flaskr 报错及其修改
作者:hhh5460 官网有一个flaskr的例子,按照其8个步骤(包括测试),一步一步照着做,有3个地方报错. 究其原因,可能是flaskr这个例子年代比较久远,而现在python以及flask都有 ...
- spfa 单源最短路究极算法
学习博客链接:SPFA 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. SPFA算法是西南交通大学段凡丁于1994年发表的. 从名字我 ...
- JS设置状态栏
JS设置状态栏可通过window.status = str来设置,在后台可通过 ClientScript.RegisterStartupScript( this.GetType(), "12 ...