POJ 3784 Running Median【维护动态中位数】
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
传送门:http://poj.org/problem?id=3784
题意:每次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数
思路:建立两个二叉堆:一个小根堆,一个大根堆。每次读入一个数X,若X比中位数小,则放入大根堆中,若X比中位数大,则放入小根堆中。如果某个时候,堆中的元素个数之差为2,则取出元素个数较多的那个堆的堆顶元素,放入另一个堆中,同时更新中位数。
代码:
#include<bits/stdc++.h> using namespace std; int ans[];
int main() {
int T;
scanf("%d", &T);
while(T--) { int t;
int n;
int mid; scanf("%d%d%d", &t, &n, &mid); int cnt = ;
ans[++cnt] = mid; priority_queue<int, vector<int>, greater<int> >s;//小根堆 priority_queue<int, vector<int>, less<int> >b;//大根堆
for(int i = ; i <= n; i++)
{
int temp;
scanf("%d", &temp); if(temp > mid)
{
s.push(temp);
if(s.size() - b.size() == )
{
b.push(mid);
mid = s.top();
s.pop();
}
}
else
{
b.push(temp);
if(b.size() - s.size() == )
{
s.push(mid);
mid = b.top();
b.pop();
}
}
if(i % )
ans[++cnt] = mid;
} printf("%d %d\n", t, n / + );
printf("%d", ans[] ); for(int i = ; i <= cnt; i++)
{
printf(" %d", ans[i]);
if(i % == )
{
printf("\n");
if(i != cnt)
{
printf("%d", ans[i + ]);
i++;
}
}
}
printf("\n");
}
}
POJ 3784 Running Median【维护动态中位数】的更多相关文章
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- POJ 3784 Running Median (模拟水过带翻译)
Description Moscow is hosting a major international conference, which is attended by n scientists fr ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
随机推荐
- 在 Linux 上实现一段时间后自动登出非活动用户
参考 编辑 ~/.bashrc 或 ~/.bash_profile 文件: $ vi ~/.bashrc 或, $ vi ~/.bash_profile 将下面行加入其中: TMOUT=100 这会让 ...
- 15.Pythonic与python杂记
switcher ={ :'sunday', :'monday', :'thuesday' } day = day_name=switcher.get(day,'Unknow') print(day_ ...
- 1.HDFS分布式文件系统
HDFS概述及设计目标 如果让我们自己设计一个分布式文件存储系统,怎么做? HDFS设计目标 非常巨大的分布式文件系统 运行在普通廉价的硬件上 易扩展,为用户提供性能不错的文件存储系统 HDFS架构 ...
- 【剑指Offer】面试题32 - I. 从上到下打印二叉树
题目 从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印. 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回: [3 ...
- 如何禁用AD OU 下面的邮箱用户的Exchange ActiveSync 和 适用于设备的OWA
Get-Mailbox -OrganizationalUnit QQ禁用名单 | Set-CASMailbox -ActiveSyncEnabled $false -OWAforDevicesEna ...
- Win10下用selenium、Image.crop() 截图时、坐标不准确的问题
截百度按钮的图 先将整个窗口的图保存下来 from selenium import webdriver driver = webdriver.Chrome() driver.get('https:// ...
- 七十三、SAP中清空内表的三种方式
一.上代码 二.需要注意的是 * CLEAR 只能清空不带WITH HEADER LINE的内表* REFRESH 能清空内表,但是不回收内存* FREE 能清空内表并回收内存,但是此内表还能继续使用
- jQuery判断输入法和非输入法输入
需求背景: 页面需要输入完成后自动查询. 解决方案: $('input').on('input', function() { if ($(this).prop('comStart')) return; ...
- JAVA作用域和排序算法介绍
一.作用域 1.作用域的概念 所谓的作用域是指引用可以作用到的范围. 一个引用的作用域是从引用定义位置到包裹它的最近的大括号的结束位置.只有在作用域范围内才可以访问到引用,超出作用域无法访问引用. 定 ...
- java List的用法
List的用法List包括List接口以及List接口的所有实现类.因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表 ...