【题目描述】
有nn个函数,分别为F1,F2,...,FnF1,F2,...,Fn。定义Fi(x)=Aix2+Bix+Ci(x∈N∗)Fi(x)=Aix2+Bix+Ci(x∈N∗)。给定这些Ai、BiAi、Bi和CiCi,请求出所有函数的所有函数值中最小的mm个(如有重复的要输出多个)。
【输入】
第一行输入两个正整数nn和mm。
以下nn行每行三个正整数,其中第ii行的三个数分别位AiAi、BiBi和CiCi。输入数据保证Ai<=10,Bi<=100,Ci<=10000Ai<=10,Bi<=100,Ci<=10000。
【输出】
将这nn个函数所有可以生成的函数值排序后的前mm个元素。这mm个数应该输出到一行,用空格隔开。
【输入样例】
3 10 4 5 3 3 4 5 1 7 1
【输出样例】
9 12 12 19 25 29 31 44 45 54
【提示】
【数据规模】
n,m≤10000n,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; }