Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4703 Accepted Submission(s): 2167
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.
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 14 5 1 24 24 5 1 20 0
Sample Output
172
题意:
给你n个数,让你切m刀,使之分成m+1个子集,并且使得子集的总价值和最小,每一个子集的价值为自己集合内元素两两相乘的积的和
解析:
这里有两种方法
1.斜率优化DP
两个状态的斜率优化DP
dp[i][j]表示前i个数分成j个集合的最小价值
cost[i]表示a[1-i]的集合内两两元素的乘积的和(因为这样可以算出化成斜率式)
所以(j,i]的两两元素乘积的和=cost[i]-cost[j]-sum[j]*(sum[i]-sum[j])
dp[i][c]=min{dp[j][c-1]+cost[i]-cost[j]-sum[j]*(sum[i]-sum[j])} j<i
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int lli;
const int MAXM = 5e3 +10;
const int MAXN = 1e3 +10;
#define INF 0x3f3f3f3f
//lli sum[MAXN];
lli xb[MAXN];
lli dp[MAXN][MAXN];
lli q[MAXN],head,tail;
lli sum[MAXN],cost[MAXN];
lli n,m;
lli DP(int i,int j,int c)
{
return dp[j][c-1]+cost[i]-cost[j]-sum[j]*(sum[i]-sum[j]);
}
lli UP(int i,int j,int c)
{
return dp[i][c-1]-cost[i]+sum[i]*sum[i]-(dp[j][c-1]-cost[j]+sum[j]*sum[j]);
}
lli DOWN(int i,int j)
{
return sum[i]-sum[j];
}
int main()
{
int t;
int ncount=0;
while(scanf("%lld%lld",&n,&m),n+m)
{
m++;
sum[0]=0;
//scanf("%lld%lld",&n,&m);
//ncount++;
for(int i=1;i<=n;i++)
{
scanf("%lld",&xb[i]);
sum[i]=sum[i-1]+xb[i];
}
cost[0]=0;
for(int i=1;i<=n;i++)
{
cost[i]=cost[i-1]+xb[i]*sum[i-1];
}
//sort(xb+1,xb+1+n);
dp[0][0]=0;
for(int i=1;i<=n;i++)
dp[i][1]=DP(i,0,0);
for(int j=2;j<=m;j++)
{
tail=head=0;
q[tail++]=0;
for(int i=1;i<=n;i++) //head+1<tail保证队列里面至少有一个点,从2*m-1是为了将为2*m找答案的点m插入到队列中
{
while(head+1<tail&&UP(q[head+1],q[head],j)<=sum[i]*DOWN(q[head+1],q[head]))
head++;
dp[i][j]=DP(i,q[head],j);
int p=i; //为了保证之后答案的寻找最大只能到i-m+1
while(head+1<tail&&UP(p,q[tail-1],j)*DOWN(q[tail-1],q[tail-2])<=UP(q[tail-1],q[tail-2],j)*DOWN(p,q[tail-1]))
tail--;
q[tail++]=p;
}
}
printf("%lld\n",dp[n][m]);
}
return 0;
}
2.四边形优化DP
关于他的详细证明点击打开链接
还有相关的PPT点击打开链接
cost[i][j]表示[i,j]内元素两两相乘的乘积的和
第一个条件显然满足,第二个条件
所以就可以用四边形优化来求(复杂度为n^2)
f(i,j)=min{f(k,j-1)+cost(k+1,i)} k<i
#include <cstdio>
typedef long long int lli;
#define INF 923372036854775798
const int MAXN = 1e3+100; //四边形优化O(n*n)
lli xb[MAXN];
lli dp[MAXN][MAXN];
lli cost[MAXN][MAXN];
lli sum[MAXN];
int s[MAXN][MAXN];
int main()
{
lli n,m;
while(scanf("%lld%lld",&n,&m),n+m)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&xb[i]);
sum[i]=sum[i-1]+xb[i];
cost[i][0]=0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i>j||i==j) cost[i][j]=0;
else cost[i][j]=cost[i][j-1]+xb[j]*(sum[j-1]-sum[i-1]); //[i,j]
}
}
for(int i=0;i<=n;i++) //初始化DP,都是为了使第一层DP赋初值
{
s[i][0]=1;
s[n+1][i]=n; //是为了让第一次查找j=1,i=1-n||i=n+1,j=1-m情况下,无法进行优化,只能从头(1)找到尾(n)
dp[i][0]=cost[1][i]; //是为了使查找dp[i][1]的时候赋上正确的值,这样因为同时还可以找出s[i][1]
}
for(int j=1;j<=m;j++) //j从大到小是因为要提前算出s[i][j-1]
{
for(int i=n;i>=1;i--) //i从小到大是因为要提前算出s[i+1][j],不影响的原因也是dp[i][j]是跟上一循环状态dp[k][j-1]有关的,与本循环内状态无关
{
dp[i][j]=INF;
for(int k=s[i][j-1];k<=s[i+1][j];k++)
{
if(dp[k][j-1]+cost[k+1][i]<dp[i][j])
{
dp[i][j]=dp[k][j-1]+cost[k+1][i];
s[i][j]=k;
}
}
}
}
printf("%lld\n",dp[n][m]);
}
return 0;
}