Question summary-median

Question summary-median

topic
Median question

Problem analysis:

The meaning of this question is actually not understood. It is equivalent to dynamically finding the median in the process of inserting data. Whenever an odd number of data is inserted, the median of all odd numbers of data is found.

Question ideas:

  • When I started to see this question, the most violent idea is definitely to sort the odd data whenever an odd number of data is inserted, so as to be able to find the median (but it is conceivable that such an approach will definitely time out). I still don't know anything, and I don't have any ideas. Later, through the analysis of the Great God, I found that there are many different approaches to this topic, using different data structures.

  • The method I use here is the idea of ​​heap. A large top heap and a small top heap are created. Mid represents the comparison variable (it is the first data element at the beginning and will be updated later). If the inserted element is less than mid, then the element is inserted into the big top pile, otherwise the element is inserted into the small top pile. According to the respective properties of the big top pile and the small top pile, it can be known that each of the big top piles The elements are all smaller than the elements in the small top pile. Whenever an odd number of elements are inserted, determine whether the number of elements in the large top heap is equal to the number of elements in the small top heap (this is because for odd numbers of data, the number of elements greater than the median = less than the median If the number of elements in the large top pile is more than that in the small top pile, press mid into the small top pile. The element of the root node in the big top pile becomes the new mid. Repeat the process until two piles The number of elements in the middle is equal; conversely, if the number of elements in the small top pile is more than that in the large top pile, mid is pressed into the large top pile, and the element at the root node in the small top pile becomes the new mid. Repeat the process until The number of elements in the two heaps is equal, and the mid value obtained at the end is the desired median.

Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n,a[200000],mid;
	cin>>n;
	//利用优先队列实现堆的结构
	priority_queue<int,vector<int>,less<int> > lar;            //建立一个大根堆
	priority_queue<int,vector<int>,greater<int> > sma;    //建立一个小根堆
	cin>>a[1];
	mid=a[1];           //开始时mid时第一个数据元素
	cout<<a[1]<<endl;
	for(int i=2;i<=n;i++)
	{
    
    
		cin>>a[i];
		if(a[i]>mid)
		{
    
    
			sma.push(a[i]);
		}
		else
		{
    
    
			lar.push(a[i]);
		}
		if(i&1)                           //看网上的代码看到一位大神这样写的,其实相当于判断i是否是奇数
		{
    
    
			while(sma.size()!=lar.size())              //当两个堆的元素个数不相等时
			{
    
    
				if(sma.size()>lar.size())
				{
    
    
					lar.push(mid);
					mid=sma.top();
					sma.pop();
				}
				else
				{
    
    
					sma.push(mid);
					mid=lar.top();
					lar.pop();
				}
			}
			cout<<mid<<endl;
		}
	}
	return 0;
}
```**加粗样式**

Guess you like

Origin blog.csdn.net/m0_46772594/article/details/108112628