【模板】优先队列

简介: 优先队列的实现形式之——大根堆 #include #include #include #include #include using namespace std; struct data{ int pq[100010]; int size=0; boo...

优先队列的实现形式之——大根堆

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
struct data{
    int pq[100010];
    int size=0;
    bool ins[100010];
    void ih()
    {
        pq[0]=0x3f3f3f3f;
        for(int i=1;i<=10010;i++)
        pq[i]=0;
        return ;
    }
    void ip()
    {
        for(int i=1;i<=size;i++)
        {
            printf("%d",pq[i]);
        }
        printf("\n");
        return ;
    }
    void iq(int x)
    {
        if(ins[x]==true) printf("%d is in the queue\n",&x);
        else printf("%d is not in the queue\n",&x);
        return ;
    }
    void top()
    {
        printf("%d ",pq[1]);
        return ;
    }
    void push(int x)
    {
        pq[++size]=x;
        ins[x]=true;
        int n=size;
        while(pq[n/2]<=pq[n])
        {
            swap(pq[n/2],pq[n]);
            n=n/2;
        }
        return ;
    }
    void pop()
    {
        this->top();
        ins[pq[1]]=false;
        swap(pq[1],pq[size]);
        size--;
        int n=1;
        while(n*2+1<=size)
        {
            int now;
            if(pq[n*2]>pq[n*2+1]) now=n*2;
            else now=n*2+1;
            if(pq[n]<pq[now])
            {
                swap(pq[n],pq[now]);
                n=now;
            }
            else break;
        }
        return ;
    }
}pq;

 


主函数自己写233....

相关文章
|
3月前
树状数组模板
树状数组模板
17 0
|
3月前
线段树模板
线段树模板
22 0
|
3月前
|
存储 索引 NoSQL
二叉堆与自定义优先队列实现删除任意元素
二叉堆与自定义优先队列实现删除任意元素
|
10月前
|
算法 UED
【算法入门】设计模板队列|循环队列(上)
【算法入门】设计模板队列|循环队列
61 0
|
10月前
|
算法
【算法入门】设计模板队列|循环队列(下)
【算法入门】设计模板队列|循环队列
73 0
|
11月前
|
人工智能
|
11月前
二分搜索的三种模板
二分搜索的三种模板
45 0
|
11月前
|
人工智能
|
11月前
|
存储 算法 C++
单调栈模板总结及应用
单调栈模板总结及应用
76 0