Print Article[HDU 3507,2010 ACM-ICPC Multi-University Training Contest]

69 篇文章 0 订阅

题目地址请点击


Print Article


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 ( 0n500000,0M1000 ).
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


Solution

fi 表示前 i 个字母打出来的最少费用。

fi=mini1j=0{fj+(k=j+1iCk)2+M}

推出斜率公式,

A(i)[2A(j)2A(k)]>fjfk+A2(j)A2(k)

其中,
A(x)=i=1xCi


Code

#include <iostream>
#include <cstdio>

#define LL long long

#define Min(x,y) ((x)<(y)?(x):(y))

using namespace std;

LL n,m,Max_Right,lastcost;
LL c[500010],A[500010],f[500010],As[500010];
double ks[500010];

int main(){

    while(scanf("%lld%lld",&n,&m)!=EOF){

        A[0]=0;
        for(LL i=1;i<=n;i++){
            scanf("%lld",&c[i]);
            A[i]=A[i-1]+c[i];
        }

        lastcost=Max_Right=As[0]=f[0]=0;
        ks[Max_Right+1]=9223372036854775807LL;ks[Max_Right]=-9223372036854775807LL;

        for(LL i=1;i<=n;i++){
            for(LL j=lastcost;j<=Max_Right;j++)
                if(ks[j]<=A[i]&&ks[j+1]>=A[i]){
                    lastcost=j;
                    f[i]=f[j]+(A[i]-As[j])*(A[i]-As[j])+m;
                    for(LL k=Max_Right;k>=0;k--)
                        if((double)(f[i]+A[i]*A[i]-f[k]-As[k]*As[k])/(2*A[i]-2*As[k])>=ks[k]){
                            Max_Right=k+1;As[Max_Right]=A[i];f[Max_Right]=f[i];
                            ks[Max_Right]=(double)(f[i]+A[i]*A[i]-f[k]-As[k]*As[k])/(2*A[i]-2*As[k]);
                            ks[Max_Right+1]=9223372036854775807LL;
                            lastcost=Min(lastcost,Max_Right);
                            break;
                        }
                    break;
                }
        }

        printf("%lld\n",f[n]);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值