For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.

Output

For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the 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 题意:每组M个数,然后对于每组数读入的时候,只要读入了奇数个的数,就求出先前读入数的中位数,然后输出 思路:可以采用两个优先队列的做法,如果当前读入的数>当前中位数,插入小根堆,否则插入大根堆,这样实际上就维护了中位数相邻两侧的值。
然后维护 num【小根堆】 - num【大根堆】 <= 1,就是维护中位数两侧的数量应当均分,这样小根堆的top,即是中位数
 #include<cstdio>
#include<iostream>
#include<queue>
using namespace std; int t;
int cas,n;
const int maxn = 1e4+;
int ans[maxn];
int main()
{
scanf("%d",&t);
while(t--)
{
priority_queue<int,vector<int>,greater<int> >que1;
priority_queue<int,vector<int>,less<int> >que2; scanf("%d%d",&cas,&n);
printf("%d %d\n",cas,n/+);
int tmp;
int l=,r=,num=;
int cnt = ;
for(int i=;i<=n;i++)
{
scanf("%d",&tmp);
if(tmp > num)
{
que1.push(tmp);
r++;
}
else
{
que2.push(tmp);
l++;
}
if(r < l)
{
int f = que2.top();
que2.pop();
que1.push(f);
r++,l--;
}
else if(r > l + )
{
r--,l++;
int f = que1.top();
que1.pop();
que2.push(f);
}
num = que1.top();
if(i&)
{
ans[++cnt] = num;
}
}
for(int i=;i<=cnt;i++)
{
printf("%d",ans[i]);
if(i != cnt && i%!=)printf(" ");
else printf("\n");
}
}
}
链表:
链表主要是个离线处理,先将所有的数据读入,然后记录下其下标。
然后对其排序,记录下在有序序列下,原来下标的数在哪出现。
然后从n~1进行处理,因为n永远是该序列剩余的数中最后出现的,也就是说不会将该数后未输入的数进行计算。 (关于中位数下标pos的移动那,按照写法是有问题的,但是这题上适用)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; int t;
int cas,n;
const int maxn = 1e4+;
struct Node
{
int val;
int next;
int pre;
int index;
}node[maxn];
int p[maxn]; bool cmp(Node a,Node b)
{
return a.val < b.val;
}
int ans[maxn];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&cas,&n);
printf("%d %d\n",cas,(n+)>>);
for(int i=;i<=n;i++)
{
scanf("%d",&node[i].val);
node[i].index = i;
}
sort(node+,node++n,cmp);
for(int i=;i<=n;i++)
{
node[i].next = i+;
node[i].pre = i-;
}
node[].pre = node[n].next = -;
for(int i=;i<=n;i++)
{
p[node[i].index] = i;
}
int pos = (n+)>>;
int tot = ;
int l=,r=;
for(int i=n;i>;i--)
{ if(i & )
{
if(l > r)pos = node[pos].next;
else if(l < r)pos = node[pos].pre;
ans[++tot] = node[pos].val;
l=r=;
}
if(p[i] >= pos)r++;
if(p[i] <= pos)l++;
if(node[p[i]].pre != -)
{
node[node[p[i]].pre].next = node[p[i]].next;
}
if(node[p[i]].next != -)
{
node[node[p[i]].next].pre = node[p[i]].pre;
}
}
for(int i=;i<=tot;i++)
{
printf("%d",ans[tot-i+]);
if(i% != && i != tot)printf(" ");
else printf("\n");
}
}
}
												

Running Median POJ - 3784 (对顶堆/优先队列 | 链表)的更多相关文章

  1. Running Median POJ - 3784

    本题使用对顶堆做法. 为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆. 用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfr ...

  2. POJ 2442 Sequence堆 优先队列

    题目描述 给定m个序列,每个序列包含n个非负整数.现在我们可以从每个序列中选择一个数字以形成一个具有m个整数的序列.显然,我们可以得到n ^ m种这种序列.然后,我们可以计算每个序列中的数字总和,并获 ...

  3. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

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

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

  5. 【POJ 3784】 Running Median

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

  6. poj3784 Running Median[对顶堆]

    由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...

  7. hdu3282 链表或者对顶堆

    维护序列的动态中位数 第一次用链表做题..感觉指针指来指去也挺麻烦的.. 本题链表解法就是用数组模拟出一个链表,然后离线输入所有数,排序,按照输入顺序在链表里删除元素,一次性删掉两个,然后中位数指针对 ...

  8. POJ3784:Running Median

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...

  9. 【POJ3784】Running Median

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

随机推荐

  1. swift 学习- 27 -- 访问控制

    // 访问控制 可以限定其源文件 或模块中的代码对你的代码的访问级别, 这个特性可以让我们隐藏代码的一些实现细节, 并且可以为其他人可以访问和使用的代码提供接口 // 你可以明确地给某个类型 (类, ...

  2. Confluence 6 避免和清理垃圾

    如果你的 Confluence 是允许公众访问的话,你可能会遇到垃圾内容的骚扰. 阻止垃圾发布者 希望阻止垃圾发布者: 启用验证码(Captcha),请参考页面 Configuring Captcha ...

  3. 动态获取后台传过来的值作为select选项

    问题描述:点击左侧菜单项,进入对应的具体页面a.html,页面上方有个select框,点击框后,会浮现选择项. 解决思路:对左侧菜单项添加一个onclick事件,进入后台做具体的查询,将查询到的lis ...

  4. HTML&javaSkcript&CSS&jQuery&ajax-Css

    CSS 1 .eg <head> <style> body{ background-color:#d0e4fe;} h1{ color:orange; text-alin:ce ...

  5. CF451E

    一道不错的题,对排列组合能力的要求较高 题意:给定s个相同的小球放在n个不同的盒子里,可以不放,每个盒子有一个放的上限,求一共有多少种放法 解析:首先考虑没有上限的情况,这里比较好解决,采用隔板法,可 ...

  6. JS中----this的指向和如何修改this的指向

    this this是js中的一个关键字,函数运行时自动生成的一个内部对象,只能在函数内部使用.我们要讨论的是 this 的指向. this就是函数运行时自动生成的一个内部对象 下面介绍一下几种情况下, ...

  7. iOS 测试之非代码获取 iPhone 型号及其他信息

    首先 安装libimobiledevice和ideviceinstaller $ brew uninstall ideviceinstaller $ brew uninstall libimobile ...

  8. NPOI操作Excel(二)--创建Excel并设置样式

    由于XSSF中的XSSFWorkbook和HSSF中的HSSFWorkbook拥有的属性.方法等都是一样的,故下面就已一个为例做为展示,他们都继承与一个接口:IWorkbook(命名空间:using  ...

  9. 关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法

    关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法 1. 找到以下路径 ...\Kingdee\K3ERP\KDHR\SITEFILE\WEBUI\ 找到“Login.aspx” ...

  10. SQL Server控制执行计划

    为了提高性能,可以使用提示(hints)特性,包含以下三类: 查询提示:(query hints)告知优化器在整个查询过程中都应用某个提示 关联提示:(join hints)告知优化器在查询的特定部分 ...