学习笔记---堆

堆(英语:heap)是计算机科学中一类特殊的数据结构的统称。堆通常是一个可以被看做一棵树的数组对象。堆总是满足下列性质:

堆中某个节点的值总是不大于或不小于其父节点的值;

堆总是一棵完全二叉树。

将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。常见的堆有二叉堆、斐波那契堆等。

// h[N]存储堆中的值, h[1]是堆顶,x的左儿子是2x, 右儿子是2x + 1
// ph[k]存储第k个插入的点在堆中的位置
// hp[k]存储堆中下标是k的点是第几个插入的
int h[N], ph[N], hp[N], size;

// 交换两个点,及其映射关系
void heap_swap(int a, int b)
{
    swap(ph[hp[a]],ph[hp[b]]);
    swap(hp[a], hp[b]);
    swap(h[a], h[b]);
}

void down(int u)
{
    int t = u;
    if (u * 2 <= size && h[u * 2] < h[t]) t = u * 2;
    if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
    if (u != t)
    {
        heap_swap(u, t);
        down(t);
    }
}

void up(int u)
{
    while (u / 2 && h[u] < h[u / 2])
    {
        heap_swap(u, u / 2);
        u >>= 1;
    }
}

// O(n)建堆
for (int i = n / 2; i; i -- ) down(i);
//实例测试,堆排序
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 1e5+10;
int a[N];
int sizea;

void down(int x){
    int u = x;
    if(x * 2<=sizea&&a[x*2]<a[u]) u = x*2;
    if(x * 2 + 1<=sizea&&a[x*2+1]<a[u]) u = x*2+1;
    if(u!=x){
        swap(a[u],a[x]);
        down(u);
    }
}

int main(){
    int n,m; 
    scanf("%d%d",&n,&m);
    sizea = n;
    
    for(int i = 1;i<=n;i++) scanf("%d",&a[i]);
    
    for(int i = n/2;i>0;i--) down(i);
    
    while(m--){
        printf("%d ",a[1]);
        a[1] = a[sizea];
        sizea--;
        down(1);
    }
    return 0;
}
//测试实例,模拟堆
#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;

const int N = 1e5+10;
int h[N],ph[N],hp[N],sizeh;

void heap_swap(int a,int b){
    swap(ph[hp[a]],ph[hp[b]]);
    swap(hp[a],hp[b]);
    swap(h[a],h[b]);
}

void down(int x){
    int t = x;
    if(x*2<=sizeh&&h[x*2]<h[t]) t = x*2;
    if(x*2+1<=sizeh&&h[x*2+1]<h[t]) t = x*2+1;
    if(t!=x){
        heap_swap(t,x);
        down(t);
    }
    
}

void up(int x){
    if(x/2>0&&h[x/2]>h[x]){
        heap_swap(x,x/2);
        up(x>>1);
    }
}


int main(){
    int num;
    int m = 0;
    scanf("%d",&num);
    
    while(num--){
        char s[10];
        int x,y;
        scanf("%s",s);
        if(!strcmp(s,"I")){
            scanf("%d",&x);
            m++;
            h[++sizeh] = x;
            ph[m] = sizeh;
            hp[sizeh] = m;
            up(sizeh);
        }
        if(!strcmp(s,"PM")){
            printf("%d\n",h[1]);
        }
        if(!strcmp(s,"DM")){
            heap_swap(1,sizeh);
            sizeh--;
            down(1);
        }
        if(!strcmp(s,"D")){
            scanf("%d",&x);
            int u = ph[x];
            heap_swap(u,sizeh);
            sizeh--;
            up(u);
            down(u);
        }
        if(!strcmp(s,"C")){
            scanf("%d%d",&x,&y);
            h[ph[x]] = y;
            down(ph[x]);
            up(ph[x]);
        }
    }
    return 0;
}

vscode整理

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值