学习笔记
Priority_queue<>
优先队列
怎么说,在2021noi被dalao虐完 之后,决定要发奋图强,2022华二的noi不能丢脸,这也算是复习笔记的第一篇,stl神奇————优先队列
现总结常用的函数
Priority_queueq;//定义一个q的堆 默认是大根堆
Priority_queue<int,vector,greater >q//定义一个小根堆
如果你想带上什么数组编号,深度等等,可以带上pair函数
Priority_queue<pair(int,int)>//大根堆
Priority_queue<pair(int ,int),vector<pair(int ,int)>,greater<pair(int,int)> >q
哈哈哈是不是好长啊,记得这个是以第一个int作为排序标准的
q.top();//返回堆顶的值
q.pop();//弹出堆顶
q.push();//加东西
q.empty();//判读是否为空,空返回true
接下来是pair的操作
q.top().first;//就是返回第一个值
q.push(pair(a,b));//加入也要带pair函数
然后呢我们有pair其实还是不够的,万一我们要带上三个变量你呢
所以就有了结构体堆
Struct node{
Int a;
Int b;
Bool operator <(node x)const{
Return a<x.a;
}
};
Priority_queue<node>q;//这里是大根堆
Struct node{
Int a;
Int b;
Bool operator <(node x)const{
Return a<x.a;
}
}
Priority_queue<node,vector<node>,greater<node> >;//这里是小根堆
注意push进结构体堆的量也必须是结构体
其实主要还是方便了运用,可以在洛谷上找“钓鱼”,“合并果子”,“合并数组”来练习练习
写的不太好,下一篇见.byebye!!