【题解】洛谷P2085 最小函数值(堆)

这个题和序列合并非常相似。只不过传的参数多了一点。。想了解思想的可以去我之前博客序列合并那道题看。

由于函数系数为正整数,所以对每一个函数来说在正整数范围内都是增函数,知道这一点就可以解决这个问题了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct H
{
	int a;
	int b;
	int c;
	int x;	
}w[10010];
int n,m;
priority_queue<H> q;
bool operator < (const H &o1,const H &o2)
{
	return o1.a*o1.x*o1.x+o1.b*o1.x+o1.c>o2.a*o2.x*o2.x+o2.b*o2.x+o2.c;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&w[i].a,&w[i].b,&w[i].c);
	}
	for(int i=1;i<=n;i++)
	{
		q.push((H){w[i].a,w[i].b,w[i].c,1});
	}
	for(int i=1;i<=m;i++)
	{
		H tmp=q.top();
		q.pop();
		printf("%d ",tmp.a*tmp.x*tmp.x+tmp.b*tmp.x+tmp.c);
		tmp.x++;
		q.push(tmp);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Rem_Inory/article/details/81489716