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. 22 Specifications动态查询

    Specifications动态查询 有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationE ...

  2. nes 红白机模拟器 第4篇 linux 手柄驱动支持

    小霸王学习机的真实手柄,实测CPU 占用 80% 接线图: 手柄读时序: joypad.c 驱动: 普通的字符设备驱动. #include <linux/module.h> #includ ...

  3. python基础学习day4

    列表的初识 why:int bool str str: 存储少量的数据. str:切片还是对其进行任何操作,获取的内容全都是str类型.存储的数据单一. what:list list = [66, ' ...

  4. 《自拍教程45》Python_adb实时监控Logcat日志

    接上一篇:adb命令_一键截取logcat日志, 有一天, 系统稳定性开发负责人找到我,希望我能在跑android 系统monkey的时候, 实时监控logcat的输出,如果一旦发现"jav ...

  5. 浅谈 HTTP中Get与Post的区别

    浅谈 HTTP中Get与Post的区别 存在的误区 有人说 HTTP 协议下的 Get 请求参数长度是有大小限制的,最大不能超过XX,而 Post 是无限制的,看到这里,我想他们定是看多了一些以讹传讹 ...

  6. 关于ATL生成COM注册失败解决方法

    最近搞C++封装研究了下COM 做最后整理打包的时候发现各种问题引发的注册失败,so整理下备忘. 1.因引用其它动态连接库与你注册的dll不在同一目录下引起的异常.(解决方法将依赖dll放置与注册dl ...

  7. ReentrantReadWriteLock源码探究

    ReentrantReadWriteLock实现了可重入的读锁和写锁,其中读锁是共享锁,写锁是互斥锁.与ReentrantLock类似,ReentrantReadWriteLock也提供了公平锁和非公 ...

  8. redis 持久化RDB、AOF

    1.redis持久化简介 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集合和有序集合.支持在服务器端计算集合 ...

  9. Natas14 Writeup(sql注入、sql万能密码)

    Natas14: 是一个登录页面.源码如下. if(array_key_exists("username", $_REQUEST)) { $link = mysql_connect ...

  10. for循环与串行化、并行化Stream流性能对比

    第四章 并行化Stream流 关注公众号(CoderBuff)回复"stream"获取<Java8 Stream编码实战>PDF完整版. <Java8 Strea ...