poj3784Running Median——堆维护中间值
题目:http://poj.org/problem?id=3784
将较小的数放入大根堆,较大的数放入小根堆,控制较小数堆大小比较大数堆小1,则较大数堆堆顶即为中位数。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int p,bh,m,a[10005],hp1[10005],hp2[10005],ct1,ct2;
void pus1(int x)
{
ct1++;
hp1[ct1]=x;
int now=ct1;
while(now>1)
{
int tp=now/2;
if(hp1[now]>hp1[tp])
swap(hp1[now],hp1[tp]),now=tp;
else break;
}
}
void pus2(int x)
{
ct2++;
hp2[ct2]=x;
int now=ct2;
while(now>1)
{
int tp=now/2;
if(hp2[now]<hp2[tp])
swap(hp2[now],hp2[tp]),now=tp;
else break;
}
}
int del1()
{
int res=hp1[1];
swap(hp1[1],hp1[ct1]);
ct1--;
int now=1;
while(now*2<=ct1)
{
int tp=now*2;
if(tp<ct1&&hp1[tp]<hp1[tp+1])tp++;
if(hp1[now]<hp1[tp])
swap(hp1[now],hp1[tp]),now=tp;
else break;
}
return res;
}
int del2()
{
int res=hp2[1];
swap(hp2[1],hp2[ct2]);
ct2--;
int now=1;
while(now*2<=ct2)
{
int tp=now*2;
if(tp<ct2&&hp2[tp]>hp2[tp+1])tp++;
if(hp2[now]>hp2[tp])
swap(hp2[now],hp2[tp]),now=tp;
else break;
}
return res;
}
void mv1()
{
int k=del1();
pus2(k);
}
void mv2()
{
int k=del2();
pus1(k);
}
int main()
{
scanf("%d",&p);
while(p--)
{
scanf("%d%d",&bh,&m);
ct1=0;ct2=0;
memset(hp1,0,sizeof hp1);
memset(hp2,0,sizeof hp2);
int js=0;
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
if(a[i]<hp1[1])
pus1(a[i]);
else pus2(a[i]);
if(i%2)
{
while(ct1>ct2-1)mv1();
while(ct1<ct2-1)mv2();
js++;
if(js==1)printf("%d %d\n",bh,(m+1)/2);
printf("%d ",hp2[1]);
if(js%10==0)printf("\n");
}
}
if(js%10)printf("\n");
}
return 0;
}
poj3784Running Median——堆维护中间值的更多相关文章
- [UOJ#268]. 【清华集训2016】数据交互[动态dp+可删堆维护最长链]
题意 给出 \(n\) 个点的树,每个时刻可能出现一条路径 \(A_i\) 或者之前出现的某条路径 \(A_i\) 消失,每条路径有一个权值,求出在每个时刻过后能够找到的权值最大的路径(指所有和该路径 ...
- 【bzoj5210】最大连通子块和 树链剖分+线段树+可删除堆维护树形动态dp
题目描述 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块和. 其中,一棵子树的最大连通子块和指的是:该子树 ...
- POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)
按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和. 然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件.(stl的队列没有clear(),只能一个一个pop. ...
- C# 堆VS栈 值类型VS引用类型
最近博客园上连续出现了几篇关于堆VS栈 值类型VS引用类型的文章. 一个是关于C# 堆VS栈的,深入浅出,动图清晰明了,链接如下 C#堆栈对比(Part One) C#堆栈对比(Part Two) C ...
- bzoj4165 矩阵 堆维护多路归并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4165 题解 大概多路归并是最很重要的知识点了吧,近几年考察也挺多的(虽然都是作为签到题的). ...
- 洛谷 P3644 [APIO2015]八邻旁之桥(对顶堆维护中位数)
题面传送门 题意: 一条河将大地分为 \(A,B\) 两个部分.两部分均可视为一根数轴. 有 \(n\) 名工人,第 \(i\) 名的家在 \(x_i\) 区域的 \(a_i\) 位置,公司在 \(y ...
- MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值
F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...
- hdu 4742 Pinball Game 3D(三维LIS&cdq分治&BIT维护最值)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- bzoj 3784: 树上的路径 堆维护第k大
3784: 树上的路径 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 88 Solved: 27[Submit][Status][Discuss] ...
随机推荐
- ck-reset css(2016/5/13)
/**rest by 2016/05/04 */ * {box-sizing: border-box;} *:before,*:after {box-sizing: border-box;} body ...
- Java中的线程池ExecutorService
示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...
- 宜人贷PaaS数据服务平台Genie:技术架构及功能
上篇:架构及组件 一.数据平台的发展 1.1 背景介绍 随着数据时代的到来,数据量和数据复杂度的增加推动了数据工程领域的快速发展.为了满足各类数据获取/计算等需求,业内涌现出了诸多解决方案.但大部分方 ...
- PowerBuilder -- 日期
#PB自带日期相关函数 Date(...), DateTime(...), RelativeDate(...), Year(...), Month(...), Day(...), DaysAfter( ...
- html 锚点定位
在html中设置锚点定位我知道的有几种方法.在此和大家分享一下: 1.使用id定位: <a href="#1F" name="1F">锚点1< ...
- ubuntu 中文显示乱码问题 (转)
添加中文字符编码: $sudo vim /var/lib/locales/supported.d/local #添加下面的中文字符集 zh_CN.GBK GBK zh_CN.GB2312 GB2312 ...
- php部分:网页中报表的打印,并用CSS样式控制打印的部分;
网页中报表的打印,是通过调用window对象中的print()方法实现打印功能的: 调用浏览器本身的打印功能实现打印 <a href="#" onclick="wi ...
- POJ3182 The Grove[射线法+分层图最短路]
The Grove Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 904 Accepted: 444 Descripti ...
- formData.append("username", "Groucho"); input 文件大小
formData.append("username", "Groucho"); https://developer.mozilla.org/en-US/docs ...
- php.ini的几个关键配置
safe_mode = On safe_mode_gid = Off disable_functions = system,passthru,exec,shell_exec,popen,phpinfo ...