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. 在 Windows Azure 虚拟机中如何备份和还原 Windows 系统磁盘

    备份和还原对于操作真实的系统来说至关重要.对于 Windows Azure 虚拟机环境中的 Windows Server,可以根据自身的需求选择多种不同的工具或将这些工具结合使用来实现备份.下面将对这 ...

  2. nova的 microversion 实现

    之前想写nova的policy的实现, 但是发现网上,有人写的很不错了. 但是个人认为存在一些问题. ref: http://www.cnblogs.com/shaohef/p/4527436.htm ...

  3. sql优化-总结

    1.尽量缩小数据范围. 2.能一个sql解决的,坚决不用两条sql.利用case when或decode. select month_id, corppkno, sum(exportSum_new) ...

  4. (十一年)unity4.6得知Ugui中国文献-------参考-UGUI Visual Components

     大家好,我是太阳广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unity ...

  5. JQuery 操作input

    获取选中的值 获取一组radio被选中项的值 var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本 var item ...

  6. js 全局函数

    全局函数与内置对象的属性或方法不是一个概念. 全局函数它不属于任何一个内置对象. JS中有14个全局函数 函数 描述 decodeURI() 解码某个编码的 URI. decodeURICompone ...

  7. iframe自适应高度代码

    var adjustIframe = function (id) { var iframe = document.getElementById(id) var idoc = iframe.conten ...

  8. 使用XCODE 的SOURCE CONTROL 做版本控制 (1)

    http://it.zhaozhao.info/archives/60469   这是一篇关于 开发者在修改代码中非常常用的一个功能: 应用场景: 当你将代码该的面目全非,还不如从其那,这时候又想回到 ...

  9. 最小生成树Jungle Roads

    这道题一定要注意录入方式,我用的解法是prime算法 因为单个字符的录入会涉及到缓冲区遗留的空格问题,我原本是采用c语言的输入方法录入数据的,结果对了,但是提交却一直wrong,后来改成了c++的ci ...

  10. STL模板_智能指针概念

    一.智能指针1.类类型对象,在其内部封装了一个普通指针.当智能指针对象因离开作用域而被析构时,其析构函数被执行,通过其内部封装的普通指针,销毁该指针的目标对象,避免内存泄露.2.为了表现出和普通指针一 ...