1370:最小函数值(minval)

【题目描述】

m个(如有重复的要输出多个)。

【输入】

第一行输入两个正整数m。

以下Ai<=10,Bi<=100,Ci<=10000。

【输出】

将这m个数应该输出到一行,用空格隔开。

【输入样例】

3 10
4 5 3
3 4 5
1 7 1

【输出样例】

9 12 12 19 25 29 31 44 45 54

【提示】

【数据规模】

n,m≤10000。

#include <bits/stdc++.h>
using namespace std;

int f(int a, int b, int c, int x)
{
    return a * x * x + b * x + c;
}

void show(priority_queue<int> &q)
{
    if (q.size() > 0) {
        int t = q.top();
        q.pop();
        show(q);
        cout << t << " ";
    }
}

int main()
{
    // freopen("1.txt", "r", stdin);
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n), c(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i] >> c[i];
    }
    priority_queue<int> q;
    for (int i = 0; i < n; i++) {
        for (int x = 1; x <= m; x++) {
            int t = f(a[i], b[i], c[i], x);
            if (q.size() < m) {
                q.push(t);
            } else if (t < q.top()) {
                q.pop();
                q.push(t);
            }
        }
    }
    show(q);
    return 0;
}