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 ...
随机推荐
- Redis未授权漏洞利用方式
总结一下redis未授权拿shell: redis-cli -h ip >info 查看系统类型 Windows: 1.找网站绝对路径 2.config set dir 绝对路径根路径 conf ...
- eclipse导出可执行jar包步骤
按步骤图文说明 第一步:选择要导出的工程,右键[export] 第二步:双击Java文件夹下的[Runnable Jar File] 第三步:该步骤分4步走 3.1 从下拉框选择该jar的入口文件,即 ...
- python+requests实现接口测试 - get与post请求使用(转载)
转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...
- ASP.NET Core2.1 你不得不了解的GDPR(Cookie处理) - (转载)
前言 时间一晃 ASP.NET Core已经迭代到2.1版本了. 迫不及待的的下载了最新的版本,然后生成了一个模版项目来试试水. ...然后就碰到问题了... 我发现..cookie竟然存不进去了.. ...
- Python3入门(八)——面向对象OOP
一.概述 老生常谈了,万物皆对象.Python作为一门面向对象的语言,也不例外 直接看一个简单的类定义和实例化类的示例: class Student: pass stu = Student() // ...
- 20155302《网络对抗》Exp6 信息收集与漏洞扫描
20155302<网络对抗>Exp6 信息收集与漏洞扫描 实验内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测 ...
- 20155302《网络对抗》Exp5 MSF基础应用
20155302<网络对抗>Exp5 MSF基础应用 实验内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如 ...
- 20155331《网路对抗》Exp8 WEB基础实践
20155331<网路对抗>Exp8 WEB基础实践 基础问题回答 什么是表单 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签,这里面包含了处理表单数据所用CGI ...
- Git配置用户名与邮箱
1.用户名和邮箱地址的作用 用户名和邮箱地址是本地git客户端的一个变量 每次commit都会用用户名和邮箱纪录. github的contributions统计就是按邮箱来统计的. 2.查看用户名和邮 ...
- oracle10g安装在裸设备上
参考了百度文库上的 <在裸设备上面安装oracle10g> 一文. 不过为了简单起见,我选择OS 为 Redhat AS 4.8 32位. 准备安装数据库为 ...