P2085 最小函数值 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 输入系数时同时把x=1的情况入队
- place[i]代表第i个函数目前应该处理的自然数
- 输出m个,每次循环输出堆顶后遍历一遍所有的函数,找到最小的值,将它入队,同时修改其place值
#include <bits/stdc++.h>
using namespace std;
#define N 1e5
#define INF 2e9
#define MAX 10005
int n, m;
int f[MAX][3];
//小根堆
priority_queue<int, vector<int>, greater<int>> q;
int place[MAX];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
scanf("%d%d%d", &f[i][0], &f[i][1], &f[i][2]);
q.push(f[i][0] + f[i][1] + f[i][2]);
}
for (int i = 1; i <= n; i++)
place[i] = 2;
for (int i = 1; i <= m; i++)
{
printf("%d ", q.top());
q.pop();
int minn = INF, idx = 0;
for (int j = 1; j <= n; j++)
{
int temp = f[j][0] * place[j] * place[j] + f[j][1] * place[j] + f[j][2];
if (temp < minn)
minn = temp, idx = j;
}
q.push(minn);
place[idx]++;
}
}
标签:函数,int,MAX,最小,place,define From: https://www.cnblogs.com/Wang-Xianyi/p/16651166.html