对于基本数据类型
priority_queue<int> q; //默认从大到小(大根堆)
priority_queue<int, vector<int>, less<int>> q; //从大到小排序(大根堆)
priority_queue<int, vector<int>, greater<int>> q; //从小到大排序(小根堆)
自定义优先级($struct$)
- 值小的优先级高(小根堆)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
struct Node
{
int x, y;
}nodes[N];
struct cmp1
{
bool operator () (const Node &u, const Node &v)
{
return u.x > v.x;
}
};
int main()
{
nodes[0] = {-3, -9};
nodes[1] = {1, 2};
nodes[2] = {9, 4};
nodes[3] = {8, 12};
nodes[4] = {-19, 23};
priority_queue<Node, vector<Node>, cmp1> q;
for (int i = 0; i <= 4; i ++)
q.push(nodes[i]);
while (q.size())
{
auto t = q.top();
q.pop();
printf("x: %d ---- y: %d\n", t.x, t.y);
}
system("pause");
return 0;
}
-
输出结果
-
值大的优先级高(大根堆)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
struct Node
{
int x, y;
}nodes[N];
struct cmp2
{
bool operator () (const Node &u, const Node &v)
{
return u.x < v.x;
}
};
int main()
{
nodes[0] = {-3, -9};
nodes[1] = {1, 2};
nodes[2] = {9, 4};
nodes[3] = {8, 12};
nodes[4] = {-19, 23};
priority_queue<Node, vector<Node>, cmp2> q;
for (int i = 0; i <= 4; i ++)
q.push(nodes[i]);
while (q.size())
{
auto t = q.top();
q.pop();
printf("x: %d ---- y: %d\n", t.x, t.y);
}
system("pause");
return 0;
}
- 输出结果
重载 $<$ 运算符
- 值小的优先级高(小根堆)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
struct Node
{
int x, y;
bool operator < (const Node &u) const
{
return x > u.x;
}
}nodes[N];
int main()
{
nodes[0] = {-3, -9};
nodes[1] = {1, 2};
nodes[2] = {9, 4};
nodes[3] = {8, 12};
nodes[4] = {-19, 23};
priority_queue<Node> q;
for (int i = 0; i <= 4; i ++)
q.push(nodes[i]);
while (q.size())
{
auto t = q.top();
q.pop();
printf("x: %d ---- y: %d\n", t.x, t.y);
}
system("pause");
return 0;
}
-
输出结果
-
值大的优先级高(大根堆)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
struct Node
{
int x, y;
bool operator < (const Node &u) const
{
return x < u.x;
}
}nodes[N];
int main()
{
nodes[0] = {-3, -9};
nodes[1] = {1, 2};
nodes[2] = {9, 4};
nodes[3] = {8, 12};
nodes[4] = {-19, 23};
priority_queue<Node> q;
for (int i = 0; i <= 4; i ++)
q.push(nodes[i]);
while (q.size())
{
auto t = q.top();
q.pop();
printf("x: %d ---- y: %d\n", t.x, t.y);
}
system("pause");
return 0;
}
- 输出结果
自定义排序
- 根据元素和进行降序排序
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
struct Gift
{
int p, s;
bool operator < (const Gift& u) const
{
return p + s > u.p + u.s;
}
}gift[N];
int main()
{
gift[0] = {0, 2};
gift[1] = {2, 3};
gift[2] = {0, 1};
sort(gift, gift + 3);
for (int i = 0; i < 3; i ++)
{
printf("%d %d\n", gift[i].p, gift[i].s);
}
return 0;
}
- 根据元素和进行升序排序
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
struct Gift
{
int p, s;
bool operator < (const Gift& u) const
{
return p + s < u.p + u.s;
}
}gift[N];
int main()
{
gift[0] = {0, 2};
gift[1] = {2, 3};
gift[2] = {0, 1};
sort(gift, gift + 3);
for (int i = 0; i < 3; i ++)
{
printf("%d %d\n", gift[i].p, gift[i].s);
}
return 0;
}