POJ 3784.Running Median
2015-07-16
问题简述:
动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数。
原题链接:http://poj.org/problem?id=3784
解题思路:
求取中位数的方法常常想到使用堆来实现:取一个大顶堆,一个小顶堆,使大顶堆的堆顶记录中位数,因此,要时刻保持大顶堆堆顶元素小于小顶堆堆顶元素,且大顶堆元素个数等于小顶堆元素个数或等于小顶堆元素个数加一。
以下有两种堆得实现方法:
一:直接使用STL中的函数(make_heap,push_heap,pop_heap,sort_heap)实现堆;
二:使用优先队列(priority_queue)实现堆;
方法一源码:
/*
OJ: POJ
ID: 3013216109
TASK: 3784.Running Median
LANG: C++
NOTE: 堆(STL)
*/
#include <cstdio>
#include <algorithm>
using namespace std; const int MAX=;
int a[MAX],b[MAX],c[MAX]; bool cmp(int a,int b) {
return a>b;
} int main()
{
int t,n,m,x;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
int a_len=,b_len=,k=,i=;
while(m--) {
i++;
scanf("%d",&x);
if(a_len==) {
a[]=x;
c[k++]=x;
a_len++;
continue;
} if(x<=a[]) {
a[a_len++]=x;
push_heap(a,a+a_len);
}
else {
b[b_len++]=x;
push_heap(b,b+b_len,cmp);
} while(a_len>b_len+) {
b[b_len++]=a[];
pop_heap(a,a+a_len);
a_len--;
push_heap(b,b+b_len,cmp);
}
while(a_len<b_len) {
a[a_len++]=b[];
pop_heap(b,b+b_len,cmp);
b_len--;
push_heap(a,a+a_len);
} if(i%==)
c[k++]=a[];
} printf("%d %d\n",n,k);
for(int i=;i<k;i++) {
if(i>&&i%==) putchar('\n');
if(i%) putchar(' ');
printf("%d", c[i]);
}
printf("\n");
}
return ;
}
方法二源码:
/*
OJ: POJ
ID: 3013216109
TASK: 3784.Running Median
LANG: C++
NOTE: 堆(优先队列)
*/
#include <cstdio>
#include <queue>
#define MAX 10005
using namespace std; priority_queue<int,vector<int>,less<int> > a; //大顶堆
priority_queue<int,vector<int>,greater<int> > b; //小顶堆
vector<int> c; int main()
{
int t,n,m,x;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
c.clear();
for(int i=;i<m;i++) {
scanf("%d",&x);
if(a.empty()) {
a.push(x);
c.push_back(x);
continue;
}
if(x<=a.top())
a.push(x);
else
b.push(x); while(a.size()>b.size()+) {
b.push(a.top());
a.pop();
}
while(a.size()<b.size()) {
a.push(b.top());
b.pop();
} if(i%==&&i!=)
c.push_back(a.top());
} printf("%d %d\n",n,(m+)/);
for(int i=;i<c.size();i++) {
if(i>&&i%==) putchar('\n');
if(i%) putchar(' ');
printf("%d", c[i]);
}
printf("\n");
}
return ;
}
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(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- 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 ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
随机推荐
- git搭建服务器
搭建Git服务器 在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商 ...
- poj2140---herd sums
#include<stdio.h> #include<stdlib.h> int main() { ,i,j; scanf("%d",&n); ;i ...
- mysql 语句练习
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- Web页面引入文档编辑器报风险
Web页面引入文档编辑器会报风险,则需要以下操作: <system.web> <httpRuntime requestValidationMode="2.0" / ...
- android入门——UI(1)
一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...
- java正则表达式,将字符串中\后的第一个字母变成大写
java正则表达式,将字符串中\后的第一个字母变成大写 例子是比较简单,注意的是java中的“\\”意义是:我要插入一个正则表达式的反斜线,所以其后面的字符有特殊有意义.所以普通反斜线应该是" ...
- Objective-C的hook方案(一): Method Swizzling
Objective-C的hook方案(一): Method Swizzling 转自:http://blog.csdn.net/yiyaaixuexi/article/details/9374411 ...
- php ZIP压缩类实例分享
php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...
- python读取中文文件编码问题
python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...
- UVALive 6709 - Mosaic 二维线段树
题目链接 给一个n*n的方格, 每个方格有值. 每次询问, 给出三个数x, y, l, 求出以x, y为中心的边长为l的正方形内的最大值与最小值, 输出(maxx+minn)/2, 并将x, y这个格 ...