Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1603 Accepted Submission(s): 706
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:
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.
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
Source
2009 Multi-University Training Contest 2 - Host by TJU
Recommend
gaojie
斜率DP
设dp[i][j]表示前i点,炸掉j条边且i点右边的边被炸的最小值。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方程
假设在计算i时,k<h,h比k点优
dp[k][j-1]+sum[k]*(sum[i]-sum[k])<=dp[h][j-1]+sum[h]*(sum[i]-sum[h])
化简得 ((sum[h]*sum[h]-dp[h][j-1])-(sum[k]*sum[k]-dp[k][j-1]) ) /(sum[h]-sum[k]) <=sum[i]
yh=sum[h]*sum[h]-dp[h][j-1] xh=sum[h]
(yh-yk)/(xh-xk)<=sum[i]
右边不等式是递增的
g[k,h]=(yh-yk)/(xh-xk)
上述不等式成立说明h比k优
如果k<h<i g[k,h]>g[i,h]那么k可以淘汰
如果g[h,i]<sum[i] h可以淘汰
可以得出 y=dp[k][j-1]-cost[1][k]+sum[k]^2
x=sum[k].
斜率sum[i]
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define positive(a) ((a)>0?(a):-(a))
using namespace std;
int sum[1010];//cost[i]存前i项的和
int cost[1010];//cost[i]存1到i之间的战略价值
int dp[1010][1010];
int n,m,head,tail;
int ans;
int q[1010];//队列
void solve()
{
int i,j,k,p1,p2,p3,x1,x2,x3,y1,y2,y3;
for(i=1;i<=n;i++)
{
dp[i][0]=cost[i];//初始为没炸时的状态
dp[i][i-1]=0;//全炸时的状态
}
for(j=1;j<=m;j++)//循环先算i算完i后才可以算j
{
head=tail=0;
q[tail++]=j;
for(i=j+1;i<=n;i++)//k不能等于i若k等于i.dp[k][j-1]->dp[i][j]不能转移所以1<=k<i
{
while(tail-head>=2)//队中元素大于等于两个。
{
p1=q[head];
p2=q[head+1];
x1=sum[p1];
x2=sum[p2];
y1=dp[p1][j-1]-cost[p1]+sum[p1]*sum[p1];
y2=dp[p2][j-1]-cost[p2]+sum[p2]*sum[p2];
if(y2-y1<=(x2-x1)*sum[i])//(y2-y1)/(x2-x1)<=sum[i]时p2比p1优
head++;//所以去除p1
else
break;
}
k=q[head];//取出最优解
dp[i][j]=dp[k][j-1]-cost[k]+cost[i]+sum[k]*sum[k]-sum[k]*sum[i];
while(tail-head>=2)//去掉上凸点
{
p1=q[tail-2];
p2=q[tail-1];
p3=i;
x1=sum[p1];
x2=sum[p2];
x3=sum[p3];
y1=dp[p1][j-1]-cost[p1]+sum[p1]*sum[p1];
y2=dp[p2][j-1]-cost[p2]+sum[p2]*sum[p2];
y3=dp[p3][j-1]-cost[p3]+sum[p3]*sum[p3];
if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))//(y3-y2)/(x3-x2)<=(y2-y1)/(x2-x1)时p2为上凸点
tail--;
else
break;
}
q[tail++]=i;
}
}
ans=dp[n][m];
}
int main()
{
int i,a;
while(scanf("%d%d",&n,&m),n||m)
{
sum[0]=0;
cost[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a);
sum[i]=sum[i-1]+a;
cost[i]=cost[i-1]+sum[i-1]*a;
}
solve();
printf("%d\n",ans);
}
return 0;
}