https://vjudge.net/problem/HDU-6546
题意:wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n).
现在他想在∑ni=1xi = m 且 x 为正整数的条件下求∑ni=1Fi(xi)的最小值。
请求出这个最小值。
优先队列维护每个函数f(x+1)-f(x)的值,每次取最小的去增加x的值。
代码:#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
struct Node
{
int a, b, c;
int x;
LL sum, sub;
bool operator < (const Node& that) const
{
return this->sub > that.sub;
}
void Update()
{
sum = a*x*x+b*x+c;
sub = a*(x+1)*(x+1)+b*(x+1)+c-sum;
}
Node(int aa, int bb, int cc, int xx):a(aa), b(bb), c(cc),x(xx){}
};
int n, m;
int main()
{
scanf("%d %d", &n, &m);
int a, b, c;
priority_queue<Node> que;
for (int i = 1;i <= n;i++)
{
scanf("%d %d %d", &a, &b, &c);
Node node(a, b, c, 1);
node.Update();
que.emplace(node);
}
LL res = 0;
m -= n;
while (m--)
{
Node now = que.top();
que.pop();
now.x++;
now.Update();
que.emplace(now);
}
while (!que.empty())
{
res += que.top().sum;
que.pop();
}
printf("%lld\n", res);
return 0;
}