hdu6546 Function
2024-09-01 17:25:39
Function
\(\text{Alice}\) 有 \(n\) 个二次函数 \(F_i(x)=a_ix^2+b_ix+c_i(i \in [1,n])\)。
现在他想在 \(\sum_{i=1}^{n}{x_i=m}\) 且 \(x\) 为正整数的条件下求 \(\sum_{i=1}^n{F_i(x_i)}\) 的最小值。
请求出这个最小值。Input
第一行两个正整数\(n,m。\)
下面\(n\)行,每行三个整数\(a,b,c,\)分别代表二次函数的二次项,一次项,常数项系数。Output
一行一个整数表示答案。
Sample Input
2 3
1 1 1
2 2 2Sample Output
13
Data range
对于全部测试数据满足:
- \(a_i\in[1,10^3]\)
- \(b_i,c_i\in[-10^3,10^3]\)
- \(n\leq m\)
测试点编号 | \(m\) |
---|---|
1 ~ 2 |
\(\leqslant 10\) |
3 ~ 4 |
\(\leqslant 100\) |
5 ~ 6 |
\(\leqslant 500\) |
7 ~ 10 |
\(\leqslant 5 \times 10^3\) |
11 ~ 20 |
\(\leqslant 10^5\) |
思路:
先给每个函数的\(x\)赋为\(1\),再把\(f_i(x_i+1)-f_i(x_i)\)与\(i\)存入优先队列,按\(\Delta f_i\)进行\(x_i+1\)的操作\(,\)再把\(f_i(x_i+1)-f_i(x_i)\)与\(i\)塞回优先队列\(,\)重复\(m-n\)次
证明:
\(\because f^{'}_i(x)=2a_i+b_i\And a_i \geq 1\)
\(\therefore \Delta f_i\)随\(x_i+1\)操作单调增加
又\(\because\)要求\(\sum_{i=1}^nf_i(x_i)\)最小
\(\therefore\)最小的\(\Delta f_i\)一定要执行\(x_i+1\)操作
\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)
#include<cstdio>
#include<cmath>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define read read1<ll>()
Type inline T read1()
{
T t=0;
char k;
bool fl=0;
do k=getchar(),(k=='-')&&(fl=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return fl?-t:t;
}
# define A pair<ll,int>
# define ll long long
priority_queue<A,vector<A>,greater<A> >q;
int s,nx[100001],m;
ll a[100001],b[100001],c[100001],ans;
ll f(ll x,int n){return a[n]*x*x+b[n]*x+c[n];}
int main()
{
freopen("function.in","r",stdin);
freopen("function.out","w",stdout);
s=read;m=read-s;
for(int i=0;i++^s;nx[i]=1)
{
a[i]=read,b[i]=read,c[i]=read;
q.push(A(f(2,i)-f(1,i),i));
ans+=f(1,i);
}
while(m--)
{
A tem=q.top();
q.pop();
ans+=tem.first;
++nx[tem.second];
tem.first=f(nx[tem.second]+1,tem.second)-f(nx[tem.second],tem.second);
q.push(tem);
}
printf("%lld\n",ans);
return 0;
}
最新文章
- python第一天 - dict
- 重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress
- Servlet+Jsp实现图片或文件的上传功能
- LeetCode题解——3Sum Closest
- Linux系统维护修复模式
- Window Phone 8 应用程序连接扩展图片中心,图片扩展,图片查看器
- 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐
- 【strtok()】——分割字符串
- TCP报文段的首部格式
- 关于laravel框架的跨域请求/jsonp请求的理解
- PHP设计模式:工厂方法
- hdfs文件按修改时间下载
- 第一篇:操纵MySQL数据库(1) - 基于MySQLdb库
- djangoueditor 集成xadmin
- tkinter内嵌Matplotlib系列(二)之函数曲线绘制
- JMeter结果树响应数据中文乱码解决办法
- TypeScript中处理大数字(会丢失后面部分数字)
- 使用Spring框架来管理模板类
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象 - lady-奕奕的个人空间 - 开源中国社区
- php实现随机数字、字母的验证码