HackerRank Find Median(中位数)

该博客讨论了如何在数据流中高效地找到迄今为止读取的整数的中位数。中位数定义为将数字从小到大排列后位于中间位置的数,当数字个数为偶数时,中位数是中间两个数的平均值。博客通过HackerRank上的一个问题,解释了如何处理实时数据流中的中位数计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

Problem Statement

The median of a finite list of numbers can be found by arranging all the integers from lowest to highest value and picking the middle one. For example, the median of {3,3,5,9,11} is 5 . If there is an even number of integers, then there is no single middle value, and the median is then usually defined to be the mean of the two middle values. For examples, the median of {3,5,7,9} is (5+7)2=6 .

Given that integers are read from a data stream, find the median of elements read so far in an efficient way.

Input Format

The first line of input will contain integer N , i.e. the number of integers in the data stream.
Each of the next N lines will contain an integer ai .

Constraints
1N105
0ai105

Output Format

Print N integers, i.e. the median after each of the input. Report it with precision up to 101 .

Sample Input

10
1
2
3
4
5
6
7
8
9
10

Sample Output

1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5

Explanation

See the sorted list after each input.


#include<stdio.h>			//双堆求中位数(一个最大堆,一个最小堆)
#include<stdlib.h>
#define MAX 100001
/*
算法思想:
1、创建两个堆,一个最大堆,一个最小堆,堆的大小至少为给定元素个数的一半,即(size+1)/2,向上取整
2、假设用mid保存中位数,将第一个元素赋值给mid,作为中位数的初始值
3、依次遍历后面的每一个数据,若比mid小,则插入到最大堆中(最大堆中的元素都比mid小),否则插入到最小堆(元素都比mid大)
4、如果最大堆的元素个数与最小堆的元素个数差值大于1,则将mid插入到个数较少的堆中,然后将个数较多的堆的堆顶元素赋值给mid,
	同时,删除该堆顶元素
5、重复3,4,直至元素遍历结束

如果两个堆的元素个数相同,则mid即为中位数(因为最大堆中的元素都比mid小,最小堆中的元素都比mid大,且两个队元素个数相同,
则mid刚好处于中间)
否则,元素较多的堆的堆顶与mid求平均值,即为中位数(假设最小堆的元素较多,则最小堆的堆顶和mid这两个元素的左右的元素个数
相同,即堆顶和mid恰为中间位置的数,中位数即为二者的平均值)
*/

int MaxHeapNum = 0, MinHeapNum = 0;
int MaxHeap[MAX / 2], MinHeap[MAX / 2], a[MAX];
float mid;

void MaxHeapIns(int num)		//MaxHeap Insert
{
	int i, j, temp;
	MaxHeap[MaxHeapNum] = num;
	temp = MaxHeap[MaxHeapNum];
	i = MaxHeapNum;
	j = (i - 1) / 2;
	while (j >= 0)		//SiftUp
	{
		if (MaxHeap[j] < temp)
		{
			MaxHeap[i] = MaxHeap[j];
			i = j;
			if (i == 0) break;			//!!重要!!当i=0时,j=(-1)/2等于0,死循环
			j = (i - 1) / 2;
		}
		else
			break;
	}
	MaxHeap[i] = temp;
	MaxHeapNum++;
}

void MinHeapIns(int num)		//MinHeap Insert
{
	int i, j, temp;
	MinHeap[MinHeapNum] = num;
	temp = MinHeap[MinHeapNum];
	i = MinHeapNum;
	j = (i - 1) / 2;
	while (j >= 0)		//SiftUp
	{
		if (MinHeap[j] > temp)
		{
			MinHeap[i] = MinHeap[j];
			i = j; 
			if (i == 0) break;
			j = (i - 1) / 2;
		}
		else
			break;
	}
	MinHeap[i] = temp;
	MinHeapNum++;
}

void MaxHeapDel()				//Delete the top of the MaxHeap
{
	int i, j, temp;
	MaxHeap[0] = MaxHeap[MaxHeapNum - 1];
	temp = MaxHeap[0];
	i = 0;
	j = 2 * i + 1;
	while (j < MaxHeapNum - 1)		//SiftDown
	{
		if (j + 1 < MaxHeapNum - 1 && MaxHeap[j] < MaxHeap[j + 1])
			j++;
		if (MaxHeap[j] > temp)
		{
			MaxHeap[i] = MaxHeap[j];
			i = j;
			j = 2 * i + 1;
		}
		else
			break;
	}
	MaxHeap[i] = temp;
	MaxHeapNum--;
}

void MinHeapDel()				//Delete the top of the MinHeap
{
	int i, j, temp;
	MinHeap[0] = MinHeap[MinHeapNum - 1];
	temp = MinHeap[0];
	i = 0;
	j = 2 * i + 1;
	while (j < MinHeapNum - 1)		//SiftDown
	{
		if (j + 1 < MinHeapNum - 1 && MinHeap[j] > MinHeap[j + 1])
			j++;
		if (MinHeap[j] < temp)
		{
			MinHeap[i] = MinHeap[j];
			i = j;
			j = 2 * i + 1;
		}
		else
			break;
	}
	MinHeap[i] = temp;
	MinHeapNum--;
}


int main()
{
	int n, i;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%d", a + i);
		if (i == 0)
		{
			mid = a[0]*1.0;			//中位数初始值
			printf("%.1f\n", mid);
		}
		else
		{
			if (a[i] < mid)
			{
				MaxHeapIns(a[i]);
			}
			else
			{
				MinHeapIns(a[i]);
			}
			if (MinHeapNum - MaxHeapNum > 1)
			{
				MaxHeapIns(mid);
				mid = MinHeap[0]; 
				MinHeapDel();
			}
			if (MaxHeapNum - MinHeapNum > 1)
			{
				MinHeapIns(mid);
				mid = MaxHeap[0];
				MaxHeapDel();
			}
			if (MinHeapNum != MaxHeapNum)
			{
				printf("%.1f\n", (mid + (MinHeapNum > MaxHeapNum ? MinHeap[0] : MaxHeap[0])) / 2.0);
			}
			else
			{
				printf("%.1f\n", mid);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值