【POJ3784】Running Median
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3406 | Accepted: 1576 |
Description
Input
Output
Sample Input
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
Sample Output
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
解析:
动态维护中位数
方法:
建立两个二叉堆:一个小根堆,一个大根堆。在依次读入这个整数序列的过程中,设当前序列长度为M,我们始终保持:
1、序列中从小到大排名为1~M/2的整数存储在大根堆中:
2、序列中从小到大排名为M/2+1~M的整数存储在小根堆中。
任何时候,如果某一个堆中的元素过多,打破了这个性质,就取出该堆的堆顶插入另一个堆。这样一来,序列的中位数就是小根堆的堆顶。
每次新读入一个数值X后,若X比中位数小,则插入大根堆,否则插入小根堆,在插入之后检查并维护上述性质即可。这就是“对顶堆”算法。
(本题对格式要求严格)
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int T,n,m,a[]; priority_queue<int,vector<int>, greater<int> > q;//从小到大输出:小顶堆 priority_queue<int> p;//从大到小输出 :大顶堆 int main()
{
scanf("%d",&T);
while(T--)
{
while(!q.empty())q.pop();
while(!p.empty())p.pop();
scanf("%d%d",&m,&n);
printf("%d %d\n",m,(n+)/);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
q.push(a[]);
printf("%d",a[]);
int cnt=;
for(int i=;i<=n;i++)
{
if(a[i]>q.top()) q.push(a[i]);
else p.push(a[i]);
if(i%!=){
while(p.size()>(i/))
{
q.push(p.top());
p.pop();
}
while(q.size()>(i-(i/)))
{
p.push(q.top());
q.pop();
}
cnt++;
if(cnt%==) printf("\n%d",q.top());
else printf(" %d",q.top());
}
}
puts("");//换行坑人......
}
}
【POJ3784】Running Median的更多相关文章
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- 【leetcode】4. Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- 【LeeetCode】4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【AtCoder】【DP】【思维】Prefix Median(AGC012)
模的是这位神犇的代码:Atcoder AGC012F : Prefix Median 题意: 在动态中位数那道题上做了一些改动.给你一个序列a,可以将a重新任意排序,然后对于a序列构造出b序列. 假设 ...
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
随机推荐
- Mybatis-plus中的常用注解
@TableName:数据库表相关 @TableId:表主键标识 @TableField:表字段标识 @TableLogic:表字段逻辑处理注解(逻辑删除) @TableId(type= IdType ...
- SpringCloud学习笔记(三、SpringCloud Netflix Eureka)
目录: 服务发现简介 SpringCloud Netflix Eureka应用 Eureka高可用 Eureka源码分析 >>> Eureka Client初始化(客户端定时获取服务 ...
- JavaScript 看不见的类型转换
本章是我阅读JavaScript权威指南时着重留意的内容,同时也推荐正在学习前端的小伙伴可以留意一下这本书<JavaScript权威指南> JavaScript可以很灵活的将一种类型的值转 ...
- 洛谷 P1381 单词背诵
洛谷 P1381 单词背诵 洛谷传送门 题目描述 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的 ...
- 安装QTP之后造成环境变量java冲突问题的解决方案
参考:http://www.cnblogs.com/yhcreak/p/6340125.html
- vue v-show的使用
v-show的功能和v-if基本一样,但是v-if有衍生的v-else-if和v-else,v-show没有 v-show的性能比v-if要好,能用v-show就不要用v-if v-if是删除dom节 ...
- hdu 6495 dp
http://acm.hdu.edu.cn/showproblem.php?pid=6495 题意 有n个挑战(1e3),假如接受,在挑战之前体力x会变成min(x,\(b[i]\)),然后会减去a[ ...
- AGC008E Next or Nextnext(组合计数,神奇思路)
神仙题. 排列计数,一种常见的做法是 \(i\) 向 \(p_i\) 连边. 然而这里这个就逼迫我们只能从 \(i\) 向 \(a_i\) 连边. 不过没关系,考虑从 \(i\) 向 \(p_i\) ...
- Windows Terminal (Preview)治好了cmd,powershell的癌症
前言 话说n年前,我想开发一款powershell麻将游戏,但是发现命令行下无法显示麻将牌这种特殊符号. 经过研究发现,这是4字节的utf16le字符串.而powershell依赖的渲染引擎,只能渲染 ...
- 【前端知识体系-JS相关】ES6专题系列总结
1.如何搭建ES6的webpack开发环境? 安装Node环境 node -v // 10.14.1 安装NPM环境 npm -v // 6.4.1 安装babel npm install @babe ...