HDU 4006 The kth great number(优先队列、堆实现)

/*
题意:"I"表示输入数据,"Q"表示输出第k大数据

题解:优先队列,从大到小,队列中只需要k个元素。每次输出最小的即可
我用小顶堆实现,如果插入元素比tree[1]小,则直接舍掉。否则,更新。
*/

#include <iostream>
using namespace std;
const int nMax = 1000010;
int tree[nMax];
int n, k;

void build(int pos)//建树
{
	int p = pos;
	while(p != 1)
	{
		if(tree[p] < tree[p / 2])
		{
			int temp = tree[p];
			tree[p] = tree[p / 2];
			tree[p / 2] = temp;
		}
		p = p / 2;
	}
}

void update(int a)//替换顶点后的更新
{
	int p = 1;
	int son;
	while(2 * p <= k)
	{
		if(2 * p == k || tree[2 * p] < tree[2 * p + 1])
			son = 2 * p;
		else
			son = 2 * p + 1;
		if(tree[p] <= tree[son])
			break;
		else
		{
			int temp = tree[p];
			tree[p] = tree[son];
			tree[son] = temp;
			p = son;
		}
	}
}

int main()
{
	//freopen("e://data.in", "r", stdin);
	while(scanf("%d%d", &n, &k) != EOF)
	{
		char opr[10];
		int a;
		int i;
		for(i = 1; i <= n; ++ i)
		{
			scanf("%s", opr);
			if(opr[0] == 'I')
			{
				scanf("%d", &a);
				if(i <= k)
				{
					tree[i] = a;
					build(i);
				}
				else if(a > tree[1])
				{
					tree[1] = a;
					update(a);
				}
			}
			else
				printf("%d\n", tree[1]);
		}
	}
	return 0;
}

STL实现:

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, k;
int main()
{
	while(scanf("%d%d", &n, &k) != EOF)
	{
		//priority_queue<int, vector<int>, greater<int>> q;
		priority_queue<int, vector<int>, greater<int> > q;//仔细比对与上面程序有什么不一样,两个>>不能连到一起
		int i;
		char opr[10];
		int a;
		for(i = 0; i < n; ++ i)
		{
			scanf("%s", opr);
			if(opr[0] == 'I')
			{
				scanf("%d", &a);
				/*
				if(q.size() < k)
					q.push(a);
				else if(a > q.top())
				{
					q.pop();
					q.push(a);
				}*/
				q.push(a);//程序这样写更好一些
				if(q.size() > k)
					q.pop();
			}
			else if(opr[0] == 'Q')
			{
				printf("%d\n", q.top());
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值