<算法笔记>堆

        堆是一种特殊的数据结构,通常用于实现优先队列和排序算法。堆通常被描述为一棵完全二叉树,其中每个节点都比它的子节点大(或者小)。

        一般情况下,有两种类型的堆:最大堆和最小堆。在最大堆中,每个节点的值都大于或等于它的子节点的值,而在最小堆中,每个节点的值都小于或等于它的子节点的值。

        堆的操作有两种:上浮和下沉,上浮对应优先队列的插入操作push(),下沉对应优先队列的删除队头的操作()。

        接下来以洛谷p3378堆为例,实现手写堆和优先队列:

        手写堆:

#include<bits/stdc++.h>
#define N 3000000
#define ll long long
using namespace std;
int heap[N],len = 0; 
void push(int x) {
	heap[++len] = x;
	int i = len;
	while( i > 1 && heap[i] < heap[i/2]){
		swap(heap[i],heap[i/2]);
		i = i/2;
	}
}
void pop() {
	heap[1] = heap[len--];
	int i = 1;
	while(2*i <= len) {
		int lson = 2 * i;
		if(lson < len && heap[lson + 1] < heap[lson]) lson++;
		if(heap[lson] < heap[i])
		{
			swap(heap[lson],heap[i]);
			i = lson;
		}
		else break;
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		int op;
		scanf("%d",&op);
		if(op == 1) {
			int x;
			scanf("%d",&x);
			push(x);
		}
		else if(op == 2) printf("%d\n",heap[1]);
		else pop();
	}
}

STL:

#include<bits/stdc++.h>
#define N 3000000
#define ll long long
using namespace std;
priority_queue< int , vector<int> , greater<int> >q;

int main()
{
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		int op;
		scanf("%d",&op);
		if(op == 1) {
			int x;
			scanf("%d",&x);
			q.push(x);
		}
		else if(op == 2) printf("%d\n",q.top());
		else q.pop();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值