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

Description

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,我们始终保持:
1、序列中从小到大排名为1~M/2的整数存储在大根堆中:
2、序列中从小到大排名为M/2+1~M的整数存储在小根堆中。
任何时候,如果某一个堆中的元素过多,打破了这个性质,就取出该堆的堆顶插入另一个堆。这样一来,序列的中位数就是小根堆的堆顶。
每次新读入一个数值X后,若X比中位数小,则插入大根堆,否则插入小根堆,在插入之后检查并维护上述性质即可。这就是“对顶堆”算法。
(本题对格式要求严格)
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int T,n,m,a[]; priority_queue<int,vector<int>, greater<int> > q;//从小到大输出:小顶堆 priority_queue<int> p;//从大到小输出 :大顶堆 int main()
{
scanf("%d",&T);
while(T--)
{
while(!q.empty())q.pop();
while(!p.empty())p.pop();
scanf("%d%d",&m,&n);
printf("%d %d\n",m,(n+)/);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
q.push(a[]);
printf("%d",a[]);
int cnt=;
for(int i=;i<=n;i++)
{
if(a[i]>q.top()) q.push(a[i]);
else p.push(a[i]);
if(i%!=){
while(p.size()>(i/))
{
q.push(p.top());
p.pop();
}
while(q.size()>(i-(i/)))
{
p.push(q.top());
q.pop();
}
cnt++;
if(cnt%==) printf("\n%d",q.top());
else printf(" %d",q.top());
}
}
puts("");//换行坑人......
}
}

【POJ3784】Running Median的更多相关文章

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

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

  2. 【POJ 3784】 Running Median

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

  3. 【LeetCode】4. Median of Two Sorted Arrays(思维)

    [题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...

  4. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  5. 【leetcode】4. Median of Two Sorted Arrays

    题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...

  6. 【LeeetCode】4. Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. 【AtCoder】【DP】【思维】Prefix Median(AGC012)

    模的是这位神犇的代码:Atcoder AGC012F : Prefix Median 题意: 在动态中位数那道题上做了一些改动.给你一个序列a,可以将a重新任意排序,然后对于a序列构造出b序列. 假设 ...

  9. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

随机推荐

  1. RabbitMQ的消息传输保障三个层级

    这里只简单介绍一下三个层级,笔记摘录自<RabbitMQ实战指南>朱忠华作者 消息可靠传输一般是业务系统接入消息中间件时候首要考虑的问题,一般消息中间件的消息传输保障分为三个层级 1  A ...

  2. 【oracle】update

  3. Forethought Future Cup - Elimination Round C 二分 + 交互(求树的直径)

    https://codeforces.com/contest/1146/problem/C 题意 一颗大小为n的树,每次可以询问两个集合,返回两个集合中的点的最大距离,9次询问之内得出树的直径 题解 ...

  4. powersploit使用实例

    一.AntivirusBypass(绕过杀毒) Find-AVSignature  发现杀软的签名 1.先在靶机(windows 2008)上远程加载位于win7的Invoke-Shellcode.p ...

  5. sql语句优化的30种方法

    转载于:https://www.cnblogs.com/Little-Li/p/8031295.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的 ...

  6. python中pkl用法

    原文连接:https://www.jianshu.com/p/2ecadebe6d13 python中pkl用法 经常遇到在Python程序运行得到了一些字符串.列表.字典等数据,想要长久的保存下来, ...

  7. 转 OpenCV Mat 数据读写

    转:https://blog.csdn.net/u011520181/article/details/83831866 1.创建 Mat 对象: // 创建一个 320x240 的 8 位无符号型 4 ...

  8. [.NET] 控制只启动单个指定外部程序

    独立观察员 2019 年 6 月 12 日 有的时候我们程序需要启动外部程序来配合实现某些功能,比如启动一个 Cef 相关程序来承载网页.那么如果那个外部程序并没有实现单例启动,我们程序去启动它的时候 ...

  9. .net怎么使用Swagger

    目录导航 一.安装 二.配置 三.调用 四.错误记录 一.安装 新建一个没有身份验证的mvc项目 - SwaggerMvc5Demo,然后添加一个名为Remote(自定义)且包含基础读写(不想手写)的 ...

  10. java中级,知识点归纳(一)

    一.接口和抽象类的区别 抽象类中可以含有构造方法,而接口内不能有. 抽象类中可以有普通成员变量,而接口中不能有. 抽象类中可以包含非抽象的普通方法,而接口中所有方法必须是抽象的,不能有非抽象的普通方法 ...