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. 鲜贝7.3--mysql安装

    1.安装包下载 首先是下载 mysql-installer-community-5.6.14.0.msi ,大家可以到 mysql 官方网去下载. win10的安全机制比较严格,安装前最好到<设 ...

  2. https://ggaaooppeenngg.github.io/

    https://ggaaooppeenngg.github.io/

  3. ESP8266 LUA脚本语言开发: 外设篇-GPIO输出高低电平

    前言 所有的LUA开发API参考 https://nodemcu.readthedocs.io/en/master/en/modules/gpio/ 原理图 让GPIO2输出高电平只需 gpio.mo ...

  4. 【day08】PHP

    一. 函数 1.函数:封装的,可以重复使用的完成特定功能的代码段. 2.函数分类:   (1)系统函数   (2)自定义函数 3.自定义函数   (1)格式   function 函数名称([参数[= ...

  5. 支付宝AopSdk在dotnet core下的实现

    随着项目都迁移到了dotnet core下,阿里的支付宝也需要随着项目迁移.之前在.Net Framework下用到了阿里提供的AopSdk和F2FPay两个程序集,支付宝官方提供的只支持Framew ...

  6. oracle简单查询单词

    单词 释义 select 查询 from 从... where 条件查询 as 别名 not 取反 and 和 or 或者 between...and... 范围取值,包含边界 like 模糊查询 e ...

  7. 快速缓存刷新CDN节点的方法

    缓存刷新方式有 URL 刷新.目录刷新和 URL 预热.URL 刷新是以文件为单位进行缓存刷新.目录刷新是以目录为单位,将目录下的所有文件进行缓存刷新.URL 预热是以文件为单位进行资源预热. 刷新后 ...

  8. 阿里云 .NET SDK Roa 和 Rpc 风格签名

    阿里云 .NET SDK Roa 和 Rpc 风格的签名 Demo,适用于自己不想用其提供的SDK,想用自己组装 Roa 和 Rpc 的签名方式. Roa 和 Rpc 的签名方式主要有以下几个不同点: ...

  9. mybaties报错:There is no getter for property named 'table' in 'class java.lang.String'

    报错是由于xml里获取不到这个table参数 package com.xxx.mapper; import java.util.List; import org.apache.ibatis.annot ...

  10. 【LOJ#3146】[APIO2019]路灯(树套树)

    [LOJ#3146][APIO2019]路灯(树套树) 题面 LOJ 题解 考场上因为\(\text{bridge}\)某个\(\text{subtask}\)没有判\(n=1\)的情况导致我卡了\( ...