Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2308 Accepted Submission(s): 1012
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:
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:
The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:
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.
4 1 4 5 1 2 4 2 4 5 1 2 0 0
17 2
题解:用dp[i][j]表示用了i个炸弹,第i个炸弹把j和后面的数分开,该状态下的最小花费。
转移如下:
dp[i][j]=min(dp[i-1][k]+w[k+1][j]),0<=j<k.
w[i][j]表示第i个数到第j数中的数构成一个联通块的最小花费。
用sum[i]表示前i个数字的和。
那么w[i][j]=w[i][j-1]+a[j]*(sum[j-1]-sum[i-1])。
所以我们可以O(n^2)预处理出所有w[i][j]。
但是总复杂度依然是O(N^3),还需要进行优化;
第一种优化方法:四边形不等式优化。
可以证明出w[i][j]满足四边形不等式:w[i][j]+w[i+1][j+1]<=w[i+1][j]+w[i][j+1]。
设s[i][j]为dp[i][j]的最优决策,令s[i][j]=k,即dp[i][j]=dp[i-1][k]+w[k+1][j]。
因为w[i][j]满足四边形不等式,所以(详细证明可以参见讲述四边形不等式的论文)
s[i-1][j]<=s[i][j]<=s[i][j+1]。
因此我们可以将复杂度优化到O(n^2).
代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define nn 1100
#define inff 0x3fffffff
typedef __int64 LL;
using namespace std;
int n,m;
LL dp[nn][nn];
LL w[nn][nn];
int s[nn][nn];
LL a[nn];
LL sum[nn];
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m)&&(n+m))
{
sum[0]=0;
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(i=1;i<=n;i++)
{
w[i][i]=0;
for(j=i+1;j<=n;j++)
{
w[i][j]=w[i][j-1]+a[j]*(sum[j-1]-sum[i-1]);
}
}
for(i=1;i<=n;i++)
{
dp[0][i]=inff;
s[0][i]=0;
}
dp[0][0]=0;
for(i=1;i<=m;i++)
{
dp[i][0]=0;
s[i][n]=n-2;
for(j=n-1;j>=1;j--)
{
dp[i][j]=inff;
for(k=s[i-1][j];k<=min(s[i][j+1],j-1);k++)
{
if(dp[i-1][k]+w[k+1][j]<dp[i][j])
{
dp[i][j]=dp[i-1][k]+w[k+1][j];
s[i][j]=k;
}
}
}
}
LL ans=LL(inff)*inff;
for(i=0;i<n;i++)
{
ans=min(ans,dp[m][i]+w[i+1][n]);
}
printf("%I64d\n",ans);
}
return 0;
}
第二种方法:斜率优化。
再来看转移方程式:
dp[i][j]=min(dp[i-1][k]+w[k+1][j]),0<=k<j
我们可以把w[k+1][j]展开:
dp[i][j]=min(dp[i-1][k]+a[k+1]*(sump[j]-sum[k+1])+a[k+2]*(sum[j]-sum[k+1])+....a[j]*(sum[j]-sum[j])),
dp[i][j]=min(dp[i-1][k]+sum[j]*(sum[j]-sum[k])-a[k+1]*sum[k+1]-a[k+2]*sum[k+2]-.....a[j]*sum[j])
令f[x]=a[x]*sum[x];
令sumf[x]=f[1]+f[2]+..f[x]。
则转移方程为:
dp[i][j]=min(dp[i-1][k]+sum[j]*(sum[j]-sum[k])-(sumf[j]-sumf[k]))
dp[i][j]=min(dp[i-1][k]+sumf[k]-sum[j]*sum[k])+sum[j]*sum[j]-sumf[j]。
设y(k)=dp[i-1][k]+sumf[k],x(k)=sum[k],
设p=y(k)-sum[j]*x(k),则
y(k)=sum[j]*x(k)+p.
把上面的方程放在以x为横轴,y为纵轴的二维坐标平面上,那么sum[j]相当于直线的斜率,p为直线的纵截距。
当j固定的时候问题可以转化为,一条斜率已知的直线,现在平面上有一些点,让这条直线至少过平面的一个点,并且直线的纵截距p最小。显然直线所过的点一定在凸包上。
因为sum[j]随着j的增加而增加,x(k)随着k的增加而增加,所以我们可以用单调队列维护凸包上的点。
单调队列要满足两个性质:
1,队列中点的横坐标从队头至队尾递增
2,队列中相邻两点的斜率从对头至队尾递增
转移的时候:
如果队列只有一个点,那么用该点来更新。
否则:如果队首的点与下一个点的斜率小于sum[j],队首出队。直至队首的点与下一个点的斜率大于等于sum[j],或者队列里面只有一个点。那么用此时的队首来更新。
每个点只出队一次,入队一次。所以总复杂度为O(n^2)。
代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define nn 1100
#define inff 0x3fffffff
typedef __int64 LL;
using namespace std;
int n,m;
LL dp[nn][nn];
LL a[nn];
LL sum[nn];
LL sumf[nn];
int top,pop;
struct node
{
LL x,y;
node(){}
node(LL xx,LL yy)
{
x=xx,y=yy;
}
}que[nn];
void add(LL x,LL y)
{
while(pop-top>=2)
{
if((que[pop-1].y-que[pop-2].y)*(x-que[pop-1].x)<(y-que[pop-1].y)*(que[pop-1].x-que[pop-2].x))
{
break;
}
pop--;
}
que[pop++]=node(x,y);
}
void shan(LL k)
{
while(pop-1>top)
{
if(que[top+1].y-que[top].y<(que[top+1].x-que[top].x)*k)
{
top++;
}
else
break;
}
}
LL w[nn][nn];
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&(n+m))
{
sum[0]=0;
sumf[0]=0;
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum[i]=sum[i-1]+a[i];
sumf[i]=sumf[i-1]+a[i]*sum[i];
}
for(i=1;i<=n;i++)
{
w[i][i]=0;
for(j=i+1;j<=n;j++)
{
w[i][j]=w[i][j-1]+a[j]*(sum[j-1]-sum[i-1]);
}
}
for(i=1;i<=n;i++)
{
dp[0][i]=LL(inff)*inff;
}
dp[0][0]=0;
for(i=1;i<=m;i++)
{
dp[i][0]=0;
top=pop=0;
add(sum[0],dp[i-1][0]+sumf[0]);
for(j=1;j<n;j++)
{
shan(sum[j]);
dp[i][j]=que[top].y-sum[j]*que[top].x+sum[j]*sum[j]-sumf[j];
add(sum[j],dp[i-1][j]+sumf[j]);
}
}
LL ans=LL(inff)*inff;
for(i=0;i<n;i++)
{
ans=min(ans,dp[m][i]+w[i+1][n]);
}
printf("%I64d\n",ans);
}
return 0;
}