天天看点

HDU 2829 Lawrence(四边形优化dp/斜率优化dp)Lawrence

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4322    Accepted Submission(s): 1984

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 targets were the railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".

You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British Intelligence has assigned a Strategic Importance to each depot---an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:

HDU 2829 Lawrence(四边形优化dp/斜率优化dp)Lawrence

Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.

Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves---they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked this rail line right in the middle:

HDU 2829 Lawrence(四边形优化dp/斜率优化dp)Lawrence

The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:

HDU 2829 Lawrence(四边形优化dp/斜率优化dp)Lawrence

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.

Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.

Input

There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each from 1 to 100, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.

Output

For each data set, output a single integer, indicating the smallest Strategic Value for the railroad that Lawrence can achieve with his attacks. Output each integer in its own line.

Sample Input

4 1
4 5 1 2
4 2
4 5 1 2
0 0

       

Sample Output

17
2

       

Source

2009 Multi-University Training Contest 2 - Host by TJU

        比较典型的问题,把n个节点分成m块,使得最后价值最小。

        根据题意,很容易得出dp方程:dp[i][j]=dp[i-1][k]+sigma(sigma(a[l]*a[p]))(k<l<p<=j),两个sigma分别对l和p求和,其中dp[i][j]表示前j个点,分成i段的最小价值。转换一下式子,引入两个新数组可得:dp[i][j]=d[i-1][k]+c[j]-c[k]-s[k]*(s[j]-s[k]),其中c[i]表示a中从1~i任意两个数乘积的和,s[i]表示a的前缀和。我们发现,分别枚举i、j和k,时间复杂度为O(MN^2)显然不能接受。

        于是我们的第一种解决方案就是对式子变形,看是否能用斜率优化。还是老套路,假设决策k优于决策l,且有k>l,则dp[i-1][k]+c[j]-c[k]-s[k]*(s[j]-s[k])<dp[i-1][l]+c[j]-c[l]+s[l]*(s[j]-s[l]),化简移项可得dp[i-1][k]-c[k]+s[k]^2-(dp[i-1][l]-c[l]+s[l]^2)<s[j]*(s[k]-s[l])。又s[i]单调递增,所以有当k优于l时,斜率K<s[j]。故我们每次找一个最优的决策k,使得他对任意l<k,都有斜率小于s[j],所以要维护一个相邻两点斜率单调递增的向右下凸出的凸包。值得注意的是,由于题目要求,当分成i块时,点的个数至少为i+1,故初始队列中就要包含一个决策i,而不是0。具体见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
#define N 1100

using namespace std;

long long s[N],a[N],c[N],q[N],dp[N][N];
int h,t,n,m,p,i,j;

double getdp(int k)
{
	return dp[i-1][k]+c[j]-c[k]-s[k]*(s[j]-s[k]);
}

long long Y(int k)
{
	return dp[i-1][k]+s[k]*s[k]-c[k];
}

long long X(int x)
{
	return s[x];
}

bool judge(int l,int k,int j)
{
	return (Y(k)-Y(l))<=(X(k)-X(l))*s[j];
}

bool maintain(int k,int i,int j)
{
	return (Y(k)-Y(i))*(X(j)-X(k))>=(Y(j)-Y(k))*(X(k)-X(i));
}

int main()
{
	while(~scanf("%d%d",&n,&m)&&n+m)
	{
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
            s[i]=a[i]+s[i-1];
            c[i]=c[i-1]+a[i]*s[i-1];
        }
        memset(dp,INF,sizeof(dp));
        dp[0][0]=0;
        for(int i=1;i<=n;i++)
            dp[0][i]=c[i];
        for(i=1;i<=m;i++)
        {
            h=t=0; q[0]=i;						//初始时,把决策i加入队列
            for(j=i+1;j<=n;j++)						//循环从i+1开始
            {
                while (h<t && judge(q[h],q[h+1],j)) h++;	
                dp[i][j]=getdp(q[h]);
                while (h<t && maintain(q[t],q[t-1],j)) t--;
                q[++t]=j;
            }
        }
        printf("%I64d\n",dp[m][n]);
	}
	return 0;
}
           

        其实,斜率优化最近讲的够多了,如果单单只是斜率优化,这道题目可能不值得我写一篇博文。所以很显然还有另外一种方法。

        第二种解决方案就是四边形优化了,其实我当时总结四边形优化的时候就想找一道题既能够用四边形优化,也能够用斜率优化,但没找到,没想到这次发现了意外之喜。稍微回顾一下四边形优化。首先,他的使用条件是dp数组是由一个满足四边形不等式的w数组相加得来的,即dp数组也满足四边形不等式。这样的话,对于当前状态i、j,dp[i][j]取最有的决策,就在s[i-1][j]到s[i,j-1]之间。如此一来,决策的枚举数量大大减少,达到了优化的效果。

        联系到这一题,很容易看出dp[i][j]=d[i-1][k]+w[k+1][j]。其中这个w[i][j]表示,从i~j所有数字为一段产生的价值。这个很容易可以通过预处理算出来。然后dp数组显然也是由w数组构成的。现在的问题就是,如何判定w[i][j]是否满足四边形不等式。其实我也没什么好方法,网上有一种:如果(w[i+1][j]-w[i][j])关于j单调递减,那么w[i][j]就满足四边形不等式,它的证明和正确性都不得而知,但是既然有人这么说,那么再稍微找几个数字验算一下,没有问题就直接用吧。所以就直接用了,对于每一个状态记录一个决策数组,把每次选取的决策记录下来,作为后面状态决策选取的范围。具体见代码:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 1010
using namespace std;

long long w[N][N],dp[N][N],a[N],sum[N],c[N];
int s[N][N],n,m;

int main()
{
    while(~scanf("%d%d",&n,&m)&&n+m)
    {
        memset(s,0,sizeof(s));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=a[i]+sum[i-1];
            c[i]=c[i-1]+a[i]*sum[i-1];
        }
        for(int i=1;i<n;i++)
            for(int j=i+1;j<=n;j++)
                w[i][j]=c[j]-c[i-1]-sum[i-1]*(sum[j]-sum[i-1]);				//预处理w数组
        memset(dp,INF,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            dp[0][i]=w[1][i];
            s[0][i]=0;
        }
        //dp[0][0]=0;
        for(int i=1;i<=m;i++)
        {
            s[i][n+1]=n;
            for(int j=n;j>i;j--)
            {
                int l=s[i-1][j],r=min(j-1,s[i][j+1]);					//确定决策枚举范围
                for(int k=l;k<=r;k++)
                    if (dp[i][j]>dp[i-1][k]+w[k+1][j])
                    {
                        dp[i][j]=dp[i-1][k]+w[k+1][j];
                        s[i][j]=k;							//记录最优决策
                    }
            }
        }
        printf("%I64d\n",dp[m][n]);
    }
    return 0;
}