PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
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 (≤) 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
题意:
给出两个已排序序列,求这两个序列合并后的中间数
思路:
开一个数组,在线处理第二个数组。 第一二个数组(下标从1开始)分别有n,m个元素,中间数在(n + m + 1) / 2的位置。所以只要从小到大数到(n + m + 1) / 2的位置就行了~ count计总个数 ,给第一个数组设置指针i,每次从第二个数组中读入temp,检查第一个数组中前几个数是不是比temp小,小就count+1并判断是否到数了中间数,到了就输出。 如果数完比temp小的数还没到中间数,count+1,检查temp是不是中间数,是就输出。循环上述过程。如果第二个数组读取完了,还没数到中间数,说明中间数在剩下的第一个数组中,就在剩下的数组中数到中间数位置即可
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[];
int main()
{
int n,m;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
}
a[n + ] = 0x7fffffff;
cin>>m;
int mid=(n+m+)/;
int k=,count=;
for(int i=;i<=m;i++)
{
ll x;
cin>>x;
while(a[k]<x){
count++;
if(count==mid){
cout<<a[k];
}
k++;
}
count++;
if(count==mid){
cout<<x;
}
}
//如果第二个数组读取完了,还没数到中间数,
//说明中间数在剩下的第一个数组中,就在剩下的数组中数到中间数位置即可
while(count<=mid){
count++;
if(count==mid){
cout<<a[k];
}
k++;
}
return ;
}
PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*的更多相关文章
- 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 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
随机推荐
- Java字节码常量池深入剖析
继续来分析Java字节码,上一节分析了魔数的规则,接下来继续往下分析,其上次总结的规则也一起贴出来: 1.使用javap -verbose命令分析一个字节码文件时,将会分析该字节码文件的魔数.版本号. ...
- 更改jupyter notebook的单元格宽度和主题颜色(theme)
一.单元格宽度 这个命令: jt -t gruvboxl -f roboto -fs 12 -cellw 100% -T -N 它将宽度设置为100% 二.主题颜色 在用jupyter noteboo ...
- python 打印 str 字符串的实际内容 repr(str)
python 打印 str 字符串的实际内容 repr(str) s = 'aa' print(repr(s))
- javascript如何处理字符串中的\u
问题: 字符串在页面显示的时候,有\u,如:Tesla\u8fc1\u79fb ,想要显示它的原文 let a = 'Tesla\u8fc1\u79fb' //显示 Tesla迁移 alert( un ...
- 我是如何理解Android的Handler模型_1
Handler Message类似于旧时的电话系统,对应关系如下: 电话局->Handler 电话机->Message 接线员->handlerMessage 接线员的工作-> ...
- 一步一步学会preload和prefetch
preload和prefetch是什么? 我们常说的preload和prefetch,是link标签rel里新增的两种值,用于让浏览器提前加载指定的资源,它们会先被缓存(属于http cache缓存) ...
- BZOJ 2095 [Poi2010]Bridges (二分+最大流判断混合图的欧拉回路)
题面 nnn个点,mmm条双向边(正向与反向权值不同),求经过最大边权最小的欧拉回路的权值 分析 见 commonc大佬博客 精髓就是通过最大流调整无向边的方向使得所有点的入度等于出度 CODE #i ...
- BZOJ 5494: [2019省队联测]春节十二响 (左偏树 可并堆)
题意 略 分析 稍微yy一下可以感觉就是一个不同子树合并堆,然后考场上写了一发左偏树,以为100分美滋滋.然而发现自己傻逼了,两个堆一一对应合并后剩下的一坨直接一次合并进去就行了.然鹅我这个sb把所有 ...
- 钉钉中设置代码提交提醒--Github机器人(转)
生成GitHub机器人webhook 从PC端或者手机端的群机器人入口进入到机器人管理页面,选择“GitHub机器人”,按照设置流程生成GitHub机器人,即可获取到相应群的webhook,其格式如下 ...
- 通过AOP拦截打印日志,出入参数
import java.lang.reflect.Modifier; import javassist.ClassClassPath; import javassist.ClassPool; impo ...