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

传送门http://poj.org/problem?id=3784

题意:每次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数

思路:建立两个二叉堆:一个小根堆,一个大根堆。每次读入一个数X,若X比中位数小,则放入大根堆中,若X比中位数大,则放入小根堆中。如果某个时候,堆中的元素个数之差为2,则取出元素个数较多的那个堆的堆顶元素,放入另一个堆中,同时更新中位数。

代码:

 #include<bits/stdc++.h>

 using namespace std;

 int ans[];
int main() {
int T;
scanf("%d", &T);
while(T--) { int t;
int n;
int mid; scanf("%d%d%d", &t, &n, &mid); int cnt = ;
ans[++cnt] = mid; priority_queue<int, vector<int>, greater<int> >s;//小根堆 priority_queue<int, vector<int>, less<int> >b;//大根堆
for(int i = ; i <= n; i++)
{
int temp;
scanf("%d", &temp); if(temp > mid)
{
s.push(temp);
if(s.size() - b.size() == )
{
b.push(mid);
mid = s.top();
s.pop();
}
}
else
{
b.push(temp);
if(b.size() - s.size() == )
{
s.push(mid);
mid = b.top();
b.pop();
}
}
if(i % )
ans[++cnt] = mid;
} printf("%d %d\n", t, n / + );
printf("%d", ans[] ); for(int i = ; i <= cnt; i++)
{
printf(" %d", ans[i]);
if(i % == )
{
printf("\n");
if(i != cnt)
{
printf("%d", ans[i + ]);
i++;
}
}
}
printf("\n");
}
}

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

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

  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. HDU 3282 Running Median 动态中位数,可惜数据范围太小

    Running Median Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

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

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

  7. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

  8. 【POJ3784】Running Median

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

  9. hdu 3282 Running Median

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...

随机推荐

  1. 主机ping虚拟机失败。虚拟机ping主机,可以ping通。

    原文:https://blog.csdn.net/ww1473345713/article/details/51490525 今天打开虚拟机,然后用Xshell远程连接,发现连接不上.按照以下顺序检查 ...

  2. Day5 - C - Agri-Net POJ - 1258

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  3. java核心-多线程(4)-线程类基础知识

    1.并发 <1>使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU时间.所以在单处理器的机器上也应该使用并发. <2>为了实现并发,操作系 ...

  4. leetcode 正则表达式 (动态规划)

    给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符'*' 匹配零个或多个前面的那一个元素所谓匹配,是要涵盖 整个 字符串 s的 ...

  5. c++程序—三目运算符

    #include<iostream> using namespace std; #include<string> int main() { //三目运算符 ; ; ; c = ...

  6. 【剑指Offer】面试题18. 删除链表的节点

    题目 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点. 返回删除后的链表的头节点. 注意:此题对比原题有改动 示例 1: 输入: head = [4,5,1,9], val = 5 ...

  7. java嵌套循环练习

    打印一个等腰三角形 package com.lv.jj; import java.util.Scanner; public class DemoDy { public static void main ...

  8. 面向对象第二个特征-继承(Inheritance)

    面向对象第二个特征-继承(Inheritance) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.java中的继承概述 1>.继承概述 多个类种存在相同属性和行为时,讲这 ...

  9. 【Android】家庭记账本手机版开发报告四

    一.说在前面 昨天 对界面显示和逻辑结构进行完善 今天 1.添加菜单(查询.清除所有等) 2.使用滑动删除 问题 1.在做查询时获取SearchView时引 入包错误经过长时间的尝试后才修正 2.滑动 ...

  10. 每天一点点之 taro 框架开发 - taro调用组件传值

    1.调用组件 组件文件 import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' ...