Function
Problem Description
wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n).
现在他想在∑ni=1xi = m 且 x 为正整数的条件下求∑ni=1Fi(xi)的最小值。
请求出这个最小值。
Input
第一行两个正整数 n, m。
下面 n 行,每行三个整数 a, b, c 分别代表二次函数的二次项, 一次项,常数项系数。
1 ≤ n ≤ m ≤ 100, 000
1 ≤ a ≤ 1, 000
−1, 000 ≤ b, c ≤ 1, 000
Output
一行一个整数表示答案。
Sample Input
2 3
1 1 1
2 2 2
Sample Output
13
HINT
由f(x+1)-f(x)得到a(2x+1)+b,这就是x增加后f的增长数量。
又因为a>0,所以全部从x=1开始,所以有m-n个数还未被使用,循环m-n次,把数据用上,求∑ni=1Fi(xi)的最小值,
Code:
#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
struct score
{
int a,b,c;
int x;
int xxx;
bool operator<(const score &q)const
{
return xxx>q.xxx;
}
};
priority_queue<score,vector<score>,less<score> >x;
int main()
{
int n,m;
score temp;
ll sum=0;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>temp.a>>temp.b>>temp.c;
temp.x=1;
temp.xxx=temp.a*(2*temp.x+1)+temp.b;
x.push(temp);
sum+=temp.a+temp.b+temp.c;
}
for(int i=0; i<m-n; i++)
{
score t;
t=x.top();
x.pop();
sum+=t.xxx;
t.x++;
t.xxx=t.a*(2*t.x+1)+t.b;
x.push(t);
}
cout<<sum<<endl;
}