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 ...
随机推荐
- PHP-WebShell-Bypass-WAF
PHP-WebShell-Bypass-WAF PHP WebShell 一句话的结构是:输入和执行,这是经典的PHP 一句话代码: <?php eval($_GET['test']); ?&g ...
- 三十二、CI框架之配置域名和设置默认登陆网站
一.打开routes.php文件,将$route['default_controller'] = 'login'; 修改成我们需要的内容. 二.修改config.php中的base_url数据 三.L ...
- angularJS MVC及$scope作用域
- mysql 索引入门
创建索引的语法结构:
- C++学习记录——(queue的清空)
c++自带的queue并没有clear这个方法:所以只能自己写了. 一共三种(其实我决得就是两种): 第一种: 直接赋值 queue<int> MyQue; /* …… */ MyQue ...
- POJ 2796:Feel Good 单调栈经典题
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11626 Accepted: 3212 Case T ...
- Python 操作 sqlite
代码示例 #导入sqlite3库 import sqlite3 #连接数据库 conn = sqlite3.connect("customers.db") #创建表,主键id自增 ...
- T_SQL 将一列多行数据合并为一行
SQL Server在进行数据迁移和报表处理的时候会遇到将一列多行数据拼接为一个字符串的情况,为了处理这个问题,在网上找了一些相关的资料,提供两种方法,供遇到类似问题的朋友们参考,也借此加深自己的印象 ...
- php srand()和rand()
1.rand()函数 作用:返回随机整数 用法:rand(min,max) min和max规定随机数产生的范围,可以省略不写,不写时rand() 返回 0 到 RAND_MAX 之间的伪随机整数. ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-flag
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...