优先级队列的头文件是:queue 。
默认写法下,是一个大根堆:
cpp
priority_queue<int> heap; //默认写法下,是⼀个⼤根堆
< 数据类型, 存数据的结构,数据之间的⽐较⽅式 >
cpp
priority_queue<int, vector<int>, less<int>> heap2;
// 也是⼤根堆
cpp
priority_queue<int, vector<int>, greater<int>> heap3; //
⼩根堆
当优先级队列⾥⾯存的是结构体类型时,需要在结构体中重载 < 比较符,,从⽽创建出⼤根堆或者小根堆。
++在优先队列中,当比较函数返回 true时,表示第一个参数应该排在第二个参数后面(优先级更低)。【非常重要】++
- 以b为基准,定义大根堆 小小大根
cpp
struct node
{
int a, b, c;
bool operator < (const node& x) const
{
return b < x.b;
}
};
- 以b为基准,定义小根堆
cpp
struct node
{
int a, b, c;
bool operator < (const node& x) const
{
return b > x.b;
}
};
题意就是 Ai,Bi,Ci 的值是不变的,但x的值可以变。求函数的前m个最小值。

方案一:假设m为10,我们第一个想到的就是暴力求解,可能存在 一个函数前10个都是最小 的情况,所以我们可以把 所有函数的前10个值 都算一遍,然后取最小的10个。(超时了)

方案2:根据题目数据范围,这是递增函数,如果一个函数在x = 2时只算到29,那还有必要去计算 x = 3时计算出来的54吗?没有必要了。x=3比x=2时大,一定不是最小。所以,可以算出所有函数 x = 1时的值,如果那个函数最小,再去算这个函数x = 2时的值,然后把x = 2时的值与其他函数 x=1时 统一比较,计算出的最小的函数的再计算下一个x的值,以此类推。
怎么知道哪个数最小呢?所以要有函数值。
如果这个函数最小,怎么知道接下来计算的是哪个函数呢? 所以要有函数编号。
怎么知道接下来计算哪个x值呢? 所以要有x值 (代入值)。

因此,我们使用一个结构体:<函数值,函数编号,x值>
cpp
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e4 + 10;
int a[N], b[N], c[N];
struct node
{
int a; //函数值
int b; //第几个函数
int c; //第几个x
bool operator <(const node& x) const //小根堆
{
return a > x.a;
}
};
priority_queue<node> heap;
int main()
{
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i] >> b[i] >> c[i];
}
for (int i = 1; i <= n; i++)
{
heap.push({a[i]+b[i]+c[i], i, 1});
}
while(m--)
{
auto t = heap.top(); heap.pop();
int x = t.a, y = t.b, z = t.c;
cout << x << " ";
int sum = a[y]*(z+1)*(z+1) + b[y]*(z+1) + c[y];
heap.push({sum, y, z+1});
}
return 0;
}