善用轮子,拒绝做工具人
我再写手写堆我就是狗
O 万能头
#include<bits/stdc++.h>
O 优先队列
库:queue vector(记得using namespace std;)
声明:
priority_queue<int> h;
不加参数时堆顶是最大的元素
但是重载运算符只能重载小于号,很奇怪
重载运算符可以这么写
friend bool operator<(nds x,nds y){
return x.z>y.z;
}
这样写出来堆顶的元素是z最小的
priority_queue<int,vector<int>,greater<int> >h;
这个跟默认没啥区别
priority_queue<int,vector<int>,less<int> > h;
这个和默认反过来
priority_queue<int,vector<int>,cmp> h;
cmp是比较器,可以这么写
struct cmp{
bool operator()(int x,int y){
return x>y;
}
};
注意operator后边有一对括号(),据说默认比较器就是这玩意
它相当于小于号,需要注意比较器是小于号,而堆顶是最大的元素
操作:
empty() 如果队列为空返回true
push() pop() size() 不说了
top() 返回堆顶元素
O map
库:map
声明:
map<int,int> f;
第一个是索引(即下标),第二个是里边装的东西的类型
map<int,map<int,int> > f;
这个可以当二维数组
操作:
直接当数组用就vans了
O next_permutation
库:algorithm
操作:next_permutation(a+1,a+n+1);