hdu 3507 Print Article

本文介绍了一种通过动态规划和斜率优化解决打印成本最小化问题的方法。问题要求计算输出一系列数字时的最低成本,考虑了每串数字输出的成本及常数额外开销。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 11610 Accepted Submission(s): 3544

Problem Description
Zero has an old printer that doesn’t work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost

M is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.

Input
There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.

Output
A single number, meaning the mininum cost to print the article.

Sample Input
5 5
5
9
5
7
5

Sample Output
230


【分析】
斜率优化dp的小水(难)题
大概题意就是要输出N个数字a[N],输出的时候可以连续连续的输出,每连续输出一串,它的费用是 “这串数字和的平方加上一个常数M”。

这题的做法可以参考 bzoj 1010 玩具装箱toy

另外,这道题有坑点,注意一串0连续排列的情况。


【代码】

//hdu 3507 Print Article
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=500005;
ll n,m;
ll dp[mxn],sum[mxn],c[mxn],q[mxn];
inline ll get(ll left,ll rig)
{
    ll up=dp[left-1]-dp[rig-1]+sum[left-1]*sum[left-1]-sum[rig-1]*sum[rig-1];
    ll down=2*(sum[left-1]-sum[rig-1]);
    if(!down) return dp[n+1];
    return up/down;
}
int main()
{
    ll i,j,calc,h,t;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        memset(dp,0x7f7f,sizeof dp);
        dp[0]=0;
        fo(i,1,n) scanf("%lld",&c[i]);
        fo(i,1,n) sum[i]=sum[i-1]+c[i];
        h=1,t=0;
        fo(i,1,n)   
        {
            while(h<t && get(q[h],q[h+1])<sum[i]) h++;
            ll now=q[h];if(!now) now=1;
            dp[i]=dp[i-1]+c[i]*c[i]+m;
            dp[i]=min(dp[i],dp[now-1]+(sum[i]-sum[now-1])*(sum[i]-sum[now-1])+m);
            while(h<t && get(q[t-1],q[t])>get(q[t],i)) t--;
            q[++t]=i;
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值