【对顶堆】 POJ3784 Running Median

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

Source

Greater New York Regional 2009 


 将两个堆想象成两个胖子来动态维护中位数,显然,两个堆的个数不能相差出1。

保证大顶堆的个数永远比小顶堆多一个,多出来的那一个是中位数


#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll maxn = 1e5+100;
const ll mod = 1e9+7;
const ld pi = acos(-1.0);
const ll inf = 1e18;
const ld eps = 1e-5;

ll T,len,ans[maxn];
priority_queue<ll>ma;  //大鼎堆 
priority_queue<ll,vector<ll>,greater<ll> >mi; //小顶堆 

void add(ll x)
{
	if(x >= ma.top())
	{
		mi.push(x);
	}
	else
	{
		ma.push(x);
	}
	
	while(ma.size() < mi.size()  )
	{
		ll tmp = mi.top();
		mi.pop();
		ma.push(tmp);
	}
	
	while(ma.size() - mi.size() > 1)
	{
		ll tmp = ma.top();
		ma.pop();
		mi.push(tmp);
	}
		
	return ;
}

int main()
{	
    ios::sync_with_stdio(false);
    //cin.tie(0),cout.tie(0);
		
	cin >> T;
	
	while(T--)
	{
		len = 0;
		while(ma.size() != 0)
			ma.pop();
		while(mi.size() != 0)
			mi.pop();
		
		ll a,b;
		cin >> a >> b;
		cout << a << " " << (b+1)/2 << endl;
		
		for(ll i = 1; i <= b; i++)
		{
			ll x;
			cin >> x;
			if(i == 1)
			{
				ma.push(x);
			}
			else
			{
				add(x);
			}
			
			if(i%2 != 0)
			{
				ans[++len] = ma.top();
			}	
		}
		
		for(ll i = 1; i <= len; i++)
		{
			if(i != len)
			{
				if(i%10 == 0)
					cout << ans[i] << endl;
				else
					cout << ans[i] << " ";
			}
			else
			{
				cout << ans[i] << endl;
			}
		}
		
			
	} 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/88649239
今日推荐