小根堆里面套 PII
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
int main()
{
priority_queue<PII, vector<PII>, greater<PII>>q;
int t;
cin >> t;
while (t -- )
{
int x, y;
cin >> x >> y;
q.push({x, y});
}
while (!q.empty())
{
cout << q.top().first << ' ' << q.top().second << '\n';
q.pop();
}
return 0;
}
根堆里面是结构体要手写排序函数,不然报错
#include <iostream>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int>PII;
struct node
{
int x, t;
};
bool operator>(node a, node b){
return a.x < b.x;
}
int main()
{
priority_queue<node, vector<node>, greater<node>>q;
q.push({12, 4}), q.push({5, 8}), q.push({45, 3}), q.push({8, 10}), q.push({4, 13});
while (q.size())
{
node k = q.top();
q.pop();
cout << k.x << ' ' << k.t << '\n';
}
return 0;
}
运算符重载规律
#include <iostream>
using namespace std;
struct node
{
int x, y;
};
int operator+(node a, node b)
{
return a.y + b.x;
}
int fun(node a, node b)
{
return a + b;
}
int main()
{
node a, b;
a = {2, 5}, b = {7, 3};
cout << fun(a, b);
return 0;
}