题海拾贝:P2085 最小函数值

         Hello大家好!很高兴我们又见面啦!给生活添点passion,开始今天的编程之路!

我的博客:<但凡.

我的专栏:《编程之路》、《数据结构与算法之美》、《题海拾贝》

欢迎点赞,关注!

1、题目

题海拾贝:P2085 最小函数值_第1张图片

2、题解 

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
typedef long long LL;
using namespace std;
const int N = 1e4 + 10;
LL A[N], B[N], C[N];
struct node
{
	LL num;//值
	LL i;//第i个方程式
	LL j;//代入的值
	//重载运算符写结构体里面不能typedef 
	bool operator <(const node& n) const
	{
		return num > n.num;
	}
};
LL cal(int i, int x)//第i个方程式和代入得值x
{
	return A[i] * x * x + B[i] * x + C[i];
}
int main()
{
	//存储函数
	LL n, m;
	cin >> n >> m;
	for (LL i = 1;i <= n;i++)
	{
		cin >> A[i] >> B[i] >> C[i];
	}
	priority_queue heap;
	for (LL i = 1;i <= n;i++)
	{
		heap.push({ cal(i,1),i,1 });
	}
	while (m--)
	{
		node t = heap.top();heap.pop();
		LL num = t.num;int i = t.i;int j = t.j;
		cout << num << " ";
		heap.push({ cal(i,j + 1),i,j + 1 });
	}
}

        好了,今天的内容就分享到这,我们下期再见! 

 

 

你可能感兴趣的:(题海拾贝,算法,c++,数据结构)