HDU 2829 Lawrence (斜率DP)

斜率DP

设dp[i][j]表示前i点,炸掉j条边的最小值。j<i

dp[i][j]=min{dp[k][j-1]+cost[k+1][i]}

又由得出cost[1][i]=cost[1][k]+cost[k+1][i]+sum[k]*(sum[i]-sum[k])

cost[k+1][i]=cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k])

代入DP方程

可以得出 y=dp[k][j-1]-cost[1][k]+sum[k]^2

x=sum[k].

斜率sum[i]

const maxn=2008;
var n,m,i,j,h,t:longint;
    a,sum,cost,q:array[0..maxn*10] of int64;
    f:array[0..maxn,0..maxn] of int64;
function kx(x,y:int64):int64;
begin
    exit(sum[x]-sum[y]);
end;
function ky(x,y:int64):int64;
begin
    exit((f[x,j-1]-cost[x]+sum[x]*sum[x])-(f[y,j-1]-cost[y]+sum[y]*sum[y]))
end;
begin
    readln(n,m);
    for i:=1 to n do read(a[i]);
    for i:=1 to n do sum[i]:=sum[i-1]+a[i];
    for i:=1 to n do cost[i]:=cost[i-1]+a[i]*sum[i-1];
    for i:=1 to n do f[i,0]:=cost[i];
    for i:=1 to n do f[i,i-1]:=0;
    for j:=1 to m do
    begin
        h:=0; t:=1; q[1]:=j;
        for i:=j+1 to n do
        begin
            while (h<t) and (kx(q[h+1],q[h])*sum[i]>ky(q[h+1],q[h])) do inc(h);
            f[i,j]:=-sum[i]*sum[q[h]]+f[q[h],j-1]-cost[q[h]]+sum[q[h]]*sum[q[h]]+cost[i];
            while (h<t) and (ky(i,q[t])*kx(q[t],q[t-1])<=ky(q[t],q[t-1])*kx(i,q[t])) do dec(t);
            inc(t);
            q[t]:=i;
       end;
    end;
    writeln(f[n,m]);
end.
        
时间: 2024-07-30 01:11:18

HDU 2829 Lawrence (斜率DP)的相关文章

HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程很容易想出来,dp[i][j] 表示前 j 个数分成 i 组.但是复杂度是三次方的,肯定会超时,就要对其进行优化. 有两种方式,一种是斜率对其进行优化,是一个很简单的斜率优化 dp[i][j] = min{dp[i-1][k] - w[k] + sum[k]*sum[k] - sum[k]*sum[

HDU 2829 Lawrence(动态规划-四边形不等式)

Lawrence Problem Description T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary target

HDU 3480 - Division - [斜率DP]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others) Little D is really interested in the theorem of sets recently. There's a problem that confused him a long time.   

HDU 2829 Lawrence

Lawrence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 282964-bit integer IO format: %I64d      Java class name: Main T. E. Lawrence was a controversial figure during World War I. He was a British officer w

HDU2829 Lawrence(斜率优化dp)

学了模板题之后上网搜下斜率优化dp的题目,然后就看到这道题,知道是斜率dp之后有思路就可以自己做不出来,要是不事先知道的话那就说不定了. 题意:给你n个数,一开始n个数相邻的数之间是被东西连着的,对于连着的一片的数,它们的价值就是两两乘积的和.所以4 5 1 2一开始就是4*5+4*1+4*2+5*1+5*2+1*2... 注意到两两乘积的和其实是可以这么算的((a1+a2+a3+..an)^2-(a1^2+a2^2+....))/2.现在我可以在数与数之间切m刀,问切完之后的最小价值是多少.

hdu 3507 斜率dp

不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个的最小费用,那么有如下的DP方程: dp[i]= min{ dp[j]+(sum[i]-sum[j])^2 +M }  0<j<i 其中 sum[i]表示数字的前i项和. 相信都能理解上面的方程. 直接求解上面的方程的话复杂度是O(n^2) 对于500000的规模显然是超时的.下面讲解下如何用斜率

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4990    Accepted Submission(s): 1509 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的平均值最小. 解题思路:斜率DP经典题, 详细分析见: NOI2004年周源的论文<浅谈数形结合思想在信息学竞赛中的应用> 还有要注意要用输入输出外挂,不是getchar()版的,是fread()版的,第一次遇到这么变态的题目- -|||. 代码: 1 #include<iostream&g

HDU 3507 Print Article(斜率DP优化)

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 t