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分)的更多相关文章

  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. 1029 Median (25 分)

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  3. PAT 1029 Median (25分) 有序数组合并与防坑指南

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

  4. 【PAT甲级】1029 Median (25 分)

    题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...

  5. 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 ...

  6. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  7. A1029 Median (25 分)

    一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...

  8. 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 ...

  9. 1029. Median (25)

    分析: 考察归并排序,用简单的快排会超时. #include <iostream> #include <stdio.h> #include <algorithm> ...

随机推荐

  1. WEB渗透 - SQL注入(持续更新)

    SQL注入 按变量类型分:数字型和字符型 按HTTP提交方式分:POST注入.GET注入和Cookie注入 按注入方式分:布尔注入.联合注入.多语句注入.报错注入.延时注入.内联注入 按数据库类型分: ...

  2. JavaFX之FXML+CSS创建窗体以及透明窗体添加阴影

    前言 开通博客园有一段日子了,一直没空也没想好该写点什么.最近正好在做一个桌面程序,初次接触JavaFX,体验下来确实比swing好用不少.索性便记记学习笔记吧,虽然FX好像挺没存在感,没人用的感觉. ...

  3. Go语言转义字符

    \a 匹配响铃符 (相当于 \x07) 注意:正则表达式中不能使用 \b 匹配退格符,因为 \b 被用来匹配单词边界, 可以使用 \x08 表示退格符. \f 匹配换页符 (相当于 \x0C) \t ...

  4. go学习第五天、运算符

    算术运算符 下表列出了所有Go语言的算术运算符.假定 A 值为 10,B 值为 20 运算符 描述 实例 + 相加 A + B 输出结果 30 - 相减 A - B 输出结果 -10 * 相乘 A * ...

  5. IPv6 时代如何防御 DDoS 攻击?

    在互联网世界,每台联网的设备都被分配了一个用于标识和位置定义的 IP 地址.20 世纪 90 年代以来互联网的快速发展,联网设备所需的地址远远多于可用 IPv4 地址的数量,导致了 IPv4 地址耗尽 ...

  6. 项目中 关于localstorage、cookie的坑?明明设置了本地存储为什么没生效

    1.简单的介绍一下localStorage,sessionStorage,cookie localStorage:仅在客户端存储不参与服务器通信,存储大小一般为5M,如果不是人为清除,那么即使是关闭浏 ...

  7. 【Weiss】【第03章】栈例程

    写栈比队列更简单一些,毕竟只有一个数据出入口. 之前用C在程序里模拟栈代替递归的时候,直接搞个数组来实现都是非常轻松愉快的事情. 不多说,放代码. 测试代码 #include <iostream ...

  8. 11. python读写文件的多种方式

    一.txt文件 with open('users.txt','r') as user_file: data = user_file.readlines() users = [] for line in ...

  9. 关于PS切图

    现在前端项目中碰到越来越多的图片处理问题,虽然找自己公司UI小哥哥小姐姐可以解决,但是每次都找不仅要看别人有没有时间,更得看人家脸色 唉,自己摸索着来吧(多图,流量党请注意切换WiFi): 通常切图的 ...

  10. Splunk 基本使用

    Splunk 作为大数据搜索处理软件,作为行业的翘楚,绝对值得探索和学习,Splunk能实时对任何应用程序.服务器或者网络设备的数据和数据源进行搜索和索引,包括任何位置的日志.配置文件.信息.陷阱和预 ...