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的更多相关文章

  1. POJ 3784 Running Median【维护动态中位数】

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

  2. POJ 3784 Running Median(动态维护中位数)

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

  3. POJ 3784 Running Median (动态中位数)

    题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...

  4. POJ 3784 Running Median (模拟水过带翻译)

    Description Moscow is hosting a major international conference, which is attended by n scientists fr ...

  5. 【POJ 3784】 Running Median (对顶堆)

    Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...

  6. hdu 3282 Running Median

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...

  7. HDU 3282 Running Median 动态中位数,可惜数据范围太小

    Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. 【POJ3784】Running Median

    Running Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3406   Accepted: 1576 De ...

  9. 【POJ 3784】 Running Median

    [题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...

随机推荐

  1. 关于httpservletrequest的获取真实的ip

    via 值为: 下面是一些DemoWTP/1.1 GDSZ-PS-GW010-WAP05.gd.chinamobile.com (Nokia WAP Gateway 4.0 CD3/ECD13_C/N ...

  2. javascript函数的基础功能

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. leetcode_question_85 Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. #include <string>

    1 append(string T&);字符串拼接 2 c_str string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址. 3 empty() ...

  5. SRM 584 div2

    早早地水完了三道题,pt1000用的是dfs,开始做的时候误认为复杂度最多就O(2^25),结果被一组O(2*3^16)的数据接近1e8给cha了.继续努力. pt250:求两个串的前缀组成的不同串数 ...

  6. Canvas路径、描边、填充

    <script> var context = document.getElementById('canvas').getContext('2d'); context.font = '48p ...

  7. Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字

    本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...

  8. C和C++运算符 (转)

    这里是C和C++语言的运算符列表.所有列出的运算符皆含纳于C++:第三个栏目里的内容也使用C来描述.应当注意的是C不支持运算符重载. 下列运算符在两个语言中都是顺序点(运算符未重载时): && ...

  9. mysql 的not null 与 null的区别(转,恍然大悟)

    相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.我字段类型是not null,为什么我可以插入空值 2.为毛not null的效率比null高 3.判断字段 ...

  10. jquery validationEngine 使用ajax验证不通过也提交表单

    转自 http://mylfd.iteye.com/blog/2007227 validationEngine给我们为前端的表单验证减少了很大的工作量.大部分情况我们使用validationEngin ...