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
题目分析:利用2个队列 对第二个队列进行在线处理 每次都输出一个最小的值 并且记录此时已经输出值的个数 直到找到中间的值
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int main()
{
queue<int>a, b;
int N, M,num;
cin >> N;
for(int i=;i<N;i++)
{
scanf("%d", &num);
a.push(num);
}
a.push(INT_MAX);
cin >> M;
int count = , mid = (N + M-) / ;
for (int i = ; i < M; i++)
{
scanf("%d", &num);
b.push(num);
if (count == mid)
{
printf("%d", min(a.front(), b.front()));
return ;
}
else
{
if (a.front() < b.front())
a.pop();
else
b.pop();
count++;
}
}
b.push(INT_MAX);
for (int i = count; i < mid; i++)
{
if (a.front() < b.front())
a.pop();
else
b.pop();
}
printf("%d", min(a.front(), b.front()));
return ;
}
1029 Median (25分)的更多相关文章
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 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分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- 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 ...
- A1029 Median (25 分)
一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...
- 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 ...
- 1029. Median (25)
分析: 考察归并排序,用简单的快排会超时. #include <iostream> #include <stdio.h> #include <algorithm> ...
随机推荐
- 结题报告--P2441角色属性树
题目:点此 题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长……. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素 ...
- 等宽字体的妙用-loading 点点点动画
原理 ch等宽字体 + text-indent 动画负缩进 显示效果如 loading . loading .. loading ... loading . loading .. loading .. ...
- Python数据科学手册(1) IPython:超越Python
1.1 shell还是Notebook Jupyter Notebook 是 IPython shell 基于浏览器的图形界面,提供了一系列丰富的动态展示功能.Jupyter Notebook 不仅可 ...
- js中 navigator 对象
Navigator 对象包含有关浏览器的信息. 很多时候我们需要在判断网页所处的浏览器和平台,Navigator为我们提供了便利 Navigator常见的对象属性如下: 属性 描述 appCodeNa ...
- oracle使用expdp定时备份数据库
目录 oracle使用expdp备份数据库 备份shell脚本 创建定时任务 oracle使用expdp备份数据库 备份shell脚本 #!/bin/sh #获取当前时间 BACKUPTIME=$(d ...
- 解决mongo单文档超过16M
mongodb导入大文件的数据时,导入一小部分后,提示lost connect,失去连接.mongo文件有6.3G,网上查了一下,原来Mongo对单次处理好像有大小限制(16m),所以大文件会出问题, ...
- (转)协议森林14 逆袭 (CIDR与NAT)
协议森林14 逆袭 (CIDR与NAT) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! IPv4由于最初的设计原因,长度只有32 ...
- Bugku流量分析题目总结
flag被盗 题目链接:https://ctf.bugku.com/files/e0b57d15b3f8e6190e72987177da1ffd/key.pcapng 解题思路: 这个题目是比较基本的 ...
- Spring框架——IOC 工厂方法
IoC 是典型的⼯厂模式,如何使⽤用⼯厂模式创建 bean, IoC 通过⼯厂模式创建 bean 有以下两种⽅式 xml <?xml version="1.0" encodi ...
- 测试工程师需要了解的shell变量知识
欢迎访问个人博客 什么是变量 本地变量:手动定义的,在当前系统的某个环境下才能生效,作用范围小 普通变量: 单引号:原字符输出,变量名='变量值' ➜ shell name='tom' ➜ shell ...