跳到正文

算法·理论

堆-大+小-01笔记(额记不到复制就好了)

yanyu289约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月7日
篇幅
约 2 分钟
获赞
1

版本

  1. v1当前

根堆笔记

C++
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>//优先队列头文件
#define ll long long
using namespace std;
//priority_queue<ll>q;//大根堆

//下面的一堆是结构体的大根堆(一直到bool那一坨)
/*struct bingren{
     string name;
     int yanzhong;
 }a;
 priority_queue<bingren> q; // 优先队列类型改为结构体的类型
 bool operator < (bingren r1, bingren r2) { //重载<符号(<符号无论大小根堆都不变)
      return r1.yanzhong < r2.yanzhong; // 大根堆 (小根堆把这里的改>就行了)
 }
 */

//下面的一堆是结构体的大根堆的另一种写法
 /*struct bingren{
     string name;
     int yanzhong;
     bool operator < (const bingren &r1) const { //重载<符号(注意const和取址符&)
      return r1.yanzhong < yanzhong; // 大根堆
     }
};
 */

/*
结构体根堆的输入:q.push((bingren){x,y})//x,y:变量名(自己定义的)
输出:q.top().name; //先top()再跟类型
*/

priority_queue<ll,vector<ll>,greater<ll> >q;//有空格.小根堆
ll n,x;
bool a[5000005];

void go(ll t){
   if(a[t]){
      cout<<t<<" ";
      a[t]=0;
   }else return;
   go(t*2);
   go(t*2+1);
}

int main(){
   cin>>n;
   for(int i=1;i<=n;i++){
      cin>>x;
      a[x]=1;
   }
   go(1);
   return 0;
}
原文