POJ3784 Running Median 题解

POJ 3784 Running Median 动态求中位数 堆 题意。 1000个case 每个case   输入若干个数,对第k个输入,如果k为奇数,则输出前k个数的中位数 那么这就是动态求中位数了 实现的思路也比较简洁 用两个堆, 大顶堆和小顶堆 每次输入一个数,如果这个数比当前的中位数大,就存入小顶堆中,  否则就存入大顶堆。 然后调整, 小顶堆元素的个数要等于大顶堆的元素个数,或者比其多1。  如果小顶堆的元素太多,就 阅读详情

题目描述
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.
输入
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.
输出
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.
样例输入

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

样例输出

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

来源
Greater New York Regional 2009

题目请自己前往谷歌生草机翻译

题目解析:一道动态中位数,就是输出比较玄学。

方法一:平衡树
这里不做介绍,因为就是板子。

方法二:堆
建立两个堆,对这两个堆进行操作。一个堆负责大于等于中位数的数的存贮,使用小根堆;另一个堆负责小于等于中位数的数的存贮,使用大根堆。
无疑,每次插入的时候,都要和中位数比较,比中位数大的进入小根堆,比中位数小的进入大根堆。
如果大根堆的数字比小根堆的数字多 2 2 2 那么将大根堆的一个数字进入小根堆,反之亦然。
查询的时候只要比较哪个堆的数字多,中位数就是数字多的堆的堆顶。
如果数字为偶数,我们就默认中位数为两个堆的中位数的平均值。

代码

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
inline int read(){
	char c=getchar();
	int sum=0,flag=0;
	while((c<'0'||c>'9')&&c!='-') c=getchar();
	if(c=='-')
		c=getchar(),flag=1;
	while('0'<=c&&c<='9'){
		sum=(sum<<1)+(sum<<3)+(c^48);
		c=getchar();
	}
	if(flag) return -sum;
	return sum;
}
priority_queue<int> big;
priority_queue< int , vector<int> , greater<int> > small;
int n,x,T,mid,k,a;
int main(){
	//freopen("1.in","r",stdin);
    T=read();
    while(T--){
    	while(!big.empty()) big.pop();
    	while(!small.empty()) small.pop();
    	k=read();
		n=read();
    	x=read();
    	mid=x;
    	printf("%d %d\n",k,n+1>>1);
    	big.push(x);
		printf("%d ",x);
		a=1;
		for(int i=2;i<=n;i++){
    		x=read();
    		if(x>mid) small.push(x);
    		else big.push(x);
    		if(big.size()>small.size()+1){
    			small.push(big.top());
    			big.pop();
			}
			else if(big.size()+1<small.size()){
				big.push(small.top());
				small.pop();
			}
			if(i&1){
				if(big.size()>small.size()){
					mid=big.top();
					a++;
					if(a%10==0) printf("%d\n",mid);
					else printf("%d ",mid);
				}
				else{
					a++;
					mid=small.top();
					if(a%10==0) printf("%d\n",mid);
					else printf("%d ",mid);
				}
			}
			else{
				mid=big.top()+small.top()>>1;
			}
		}
		if(T) printf("\n");
	}
	return 0;
}

深入解析Panfrost驱动:Mali-GPU开源驱动的架构与实现 本文深入解析了Panfrost驱动作为Mali-GPU开源驱动的架构与实现。从诞生背景到硬件架构演进,详细介绍了Panfrost如何突破ARM闭源策略,实现全栈支持。文章还探讨了驱动设计中的关键技术挑战与性能优化策略,为开发者提供了实用的编译部署指南和性能调优方法。 阅读详情

相关推荐

后向投影算法(BPA)-SAR成像算法系列(二)

SAR成像算法:后向投影算法(BPA)原理介绍及实现步骤解析。

yjh_2019的博客 1万+

POJ-3784题解

POJ-3784 题目描述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 ...

cpongo11的博客 603

Abaqus CEL分析必备:Hypermesh网格导出与inp文件合并技巧

本文详细介绍了在Abaqus CEL分析中使用Hypermesh生成六面体网格并合并多组件inp文件的完整流程。从单元类型设置(EC3D8R)、网格质量优化到inp文件合并技巧,提供了一套解决流固耦合仿真前处理痛点的实战方案,帮助工程师高效完成复杂CEL分析任务。

weixin_29279101的博客 221

hdu3282 Running Median(动态中位数 树状数组+二分)

Problem 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 element...

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 266

BitLocker恢复密钥丢失?从原理到实战的完整解锁与系统重生指南

磁盘加密技术是数据安全的核心防线,它通过对存储设备上的数据进行编码,确保未经授权的用户无法访问。其原理主要基于加密算法(如AES)与密钥管理机制,在操作系统启动前或数据读写时进行实时加解密。这项技术的核心价值在于防止设备丢失或被盗后的数据泄露,尤其对于笔记本电脑和移动设备至关重要。典型的应用场景包括企业办公电脑、存有敏感信息的个人设备以及符合合规性要求的行业。当使用Windows系统自带的BitLocker加密功能时,若因硬件改动、系统更新等触发保护机制,用户可能会面临需要输入恢复密钥才能启动的困境。本文聚

weixin_34006965的博客 501

AMD Ryzen AI Max+ 395四机集群实战:开源工具链与大模型推理优化全解析

本文详细解析了基于AMD Ryzen AI Max+ 395处理器的四机集群搭建与优化实践,涵盖硬件配置、开源工具链评测及大模型推理优化技巧。通过深度测试,揭示了集群在AI推理中的性能瓶颈与解决方案,特别适合需要扩展显存的大模型实验场景。文章还提供了成本效益分析,帮助开发者做出更明智的选型决策。

weixin_29041767的博客 1554

[题解]Running Median

题目描述 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. 输入 The first line of input contains a single integer

wangshengjun33的博客 751

YOLOv11 Neck革新:当SDI遇上BiFPN,带你打造精度飙升的特征融合网络

对于科研:这是一个新颖的特征融合思路,能做出差异化的实验,发论文、做项目都很有价值;对于工程:它能解决小目标漏检、大目标定位不准的痛点,让你的检测系统在工业质检、智能安防等场景下更可靠;对于技术成长:掌握了SDI和BiFPN的设计逻辑,你再去看其他特征融合方法(比如FPN、PAN),会有“一通百通”的感觉,以后优化任何检测模型都能举一反三。如果你之前在目标检测的精度上遇到了瓶颈,不妨试试SDI+BiFPN——它就像给YOLOv11装上了“透视眼”,让模型能看清每一个细节。

FJN110的博客 310

【每日一题】4月9日题目精讲 Running Median

文章目录题目:题意:题解一:题解二: 题目: –>链接<— 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 65536K,其他语言131072K 64bit IO Format:%lld 题目描述 For this problem, you will write a program that reads in a sequence of 32-bit signe...

qq_35975367的博客 581

[PCIE5.0] 2.2.8.3 Error Signaling Messages

错误信号消息用于在 PCI Express 设备之间传递错误信息,帮助系统发现并报告错误。每当发生某种错误时,检测到该错误的设备会通过发送这些消息来通知系统。• 没有数据负载:这些错误信号消息不包含任何数据(即 TLP Type 是 Msg),它们仅用于错误报告。• 长度字段保留:在这些消息中,长度字段是保留的,不使用。• 功能号(Function Number)字段:这个字段标识哪个设备发生了错误。

系统固件工程师 296

新手必看!手把手教你在群晖 NAS 上玩转 Docker 容器(Container Manager)!(群晖 NAS 如何创建 Docker 容器以及 Compose 部署项目)

新手必看!手把手教你在群晖 NAS 上玩转 Docker 容器(Container Manager)!(群晖 NAS 如何创建 Docker 容器以及 Compose 部署项目)

XiaoqiangClub的博客 5177

poj 3784 Running Median

Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 742 Accepted: 354 Description For this problem, you will write a program that reads in a seque...

weixin_30652271的博客 300

YOLOv8-Pose 姿态识别 RK3588 实战:从模型训练到 RKNN 部署,精度与推理速度双提升

本教程从YOLOv8-pose模型的姿态识别训练,到RK3588的边缘量化部署,为你打造了一套“高精度+高实时性+低成本”的人体姿态分析解决方案。无论是健身动作纠正、智能安防行为检测,还是工业人员姿态监控,这套技术都能直接复用——你只需替换数据集与业务逻辑,即可快速落地专属的边缘AI姿态识别系统。如果你在实践中遇到任何卡点,欢迎随时交流。记住,人体姿态识别的价值在于“关键点准、推理快、部署易”,而这三者的平衡,正是你通过本教程能掌握的核心能力。

FJN110的博客 373

POJ3784 Running Median 题解(作业)

题面 题目描述 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. 输入 The first line of input contains a single inte

cbw3402271334a的博客 274

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

poj3784 Running Median 题解报告(对顶堆)

题目传送门 【题目大意】 读入一个整数序列,每当已经读入的整数个数为奇数时,输出已经读入的整数构成的序列的中位数。 【思路分析】 这题可以使用“对顶堆”的在线做法。为了动态维护中位数,我们可以建立两个二叉堆,一个小根堆、一个大根堆。在以此读入序列的过程中,设当前读入的序列长度为$L$,我们始终保持: 1.序列中从小到大排名为$1~\frac{M}{2}$的整数储存在大根堆中 2.序列中...

weixin_30932215的博客 91

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

POJ 3784 Running Median

POJ 3784 Running Median POJ传送门 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 (...

anhaoti3785的博客 146

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【multiset】POJ 3784 Running Median【补充】

前言 之前用对顶堆做过,现在用multisetmultisetmultiset又做了一遍,到时候还会补发链表的做法 链接 http://poj.org/problem?id=3784 大意 输入nnn个数,要求第奇数次输入时,输出该序列的中位数 思路 我们可以用STLSTLSTL中的multisetmultisetmultiset,每次放入都自动排序,在利用迭代器找到其的中间...

Ajwallet 367

POJ 3784 Running Median G++ 堆巧妙 求动态中位数 背

#include <iostream> #include <queue> #include <cstdio> #include <vector> #include <functional> using namespace std; //英语 抄博友程序 堆巧妙 求动态中位数 背 int main() { int T...

woniupengpeng的专栏 189

POJ 3784 Running Median(中位数)

                                      Running MedianDescriptionFor this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, outp...

sortmin的博客 295

poj3784 Running Median 链表

题目 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 fa...

戒毒戒赌戒网瘾,学习学问学做人——好好学习 432

Poj3784 Running Median(蓝书刷题记录)

**问题描述:**依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数。 (输出格式卡了好久) **题解:**建一个大根堆,一个小根堆,小根堆存序列的后半段,大根堆存前半段。新的数进来比中位数小放在大根堆,否则放在小根堆。 #include&lt;bits/stdc++.h&gt; using namespace std; const int MAXN=999...

J_lxj 的博客 253

Running Median(UVALive 4555/poj 3784,堆)

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 rece...

Baiyi_destroyer的博客 241

poj3784 Running Median(对顶堆)

题意动态维护中位数问题:依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数。我的想法中位数是第(n+1)/2大的数,也可以看成是排完序后中间位置的数。再确定了一个中位数后,每加入一个新数,就用一个二分插入排序将其放入中位数左或右,这样会涉及到链表上的二分查找,我不会。其实可以用vector容器代替链表的,可以一试。题解对顶堆聪明的解题人,在中位数这里把序列折成...

A_Bright_CH的博客 424

Running Median POJ - 3784(动态维护中位数问题:”对顶堆“算法/链表+hash)

题意:传送门 题解:动态维护中位数问题,两种解法,解法一是使用对顶堆,建立两个二叉堆,一个小根堆,一个大根堆,依次读入的过程中,设当前长度为MMM,始终保持: 序列中从小到大排名为1∼M21 \sim \frac{M}{2}1∼2M​的整数存储在大根堆priority_queue<int> max_heap中。 序列中从小到大排名为M2∼M\frac{M}{2}\sim M2M​∼M...

肘子的博客 359

poj3784(对顶堆在线找中位数)

Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3200 Accepted: 1492 Description For this problem, you will write a program that reads in a sequence of 32-bit signed integer...

越看越喜欢啊 800

组队赛(第七周)

A - Nth Largest Value 4552 Nth Largest Value For this problem, you will write a program that prints the N-th largest value in a fixed sized array of integers. To make things simple, N will be 3 and th...

莉莉莉的博客 343
上一篇: 博弈论入门
下一篇: USACO16DEC LuoguP3405 Cities and States S 题解
jiangtaizhe
博客等级 码龄7年 7粉丝 65原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值