Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2196 Accepted Submission(s): 962
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:
![](https://i-blog.csdnimg.cn/blog_migrate/6fe47896de43c749009567c184fcdb47.jpeg)
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:
![](https://i-blog.csdnimg.cn/blog_migrate/4103bccd9791002737cda4be678eb695.jpeg)
The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:
![](https://i-blog.csdnimg.cn/blog_migrate/c6d64ab324cb43ee9ab9ce3af4fc580c.jpeg)
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
这道题是二维斜率优化dp,其实和一维是一样的,这题可以用滚动数组优化空间。
具体参考:点击打开链接
做这些题最主要的发现决策变量的的单调性,一般就是解不等式。左侧分离与i无关的变量,右侧为仅与i有关的的变量。
设k1<k2,必须有g[k1,k2]>h(i),而h(i)随i单调不减,这样起初k1会是最优解,而k2可以看成在沉睡,当h(i)增大时,g[k1,k2]<=h(i),此时k2苏醒,成为最优解,而k1在这之后永远不会成为最优解了,因为这以后g[k1,k2]<=h(i)恒成立。
具体实现细节可以参考其他大牛的资料,也可参看代码:
代码:
#include<cstdio>
#include<iostream>
#define Maxn 1010
using namespace std;
int mat[Maxn],sum[Maxn],w[Maxn],dp[2][Maxn],q[Maxn];
int dx(int a,int b,int c){
return dp[c][a]-w[a]+sum[a]*sum[a]-(dp[c][b]-w[b]+sum[b]*sum[b]);
}
int dy(int a,int b){
return sum[a]-sum[b];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n||m){
for(int i=1;i<=n;i++){
scanf("%d",mat+i);
sum[i]=sum[i-1]+mat[i];
w[i]=w[i-1]+sum[i-1]*mat[i];
}
int a=0,b=1;
for(int i=1;i<=n;i++) dp[a][i]=w[i];
for(int j=2;j<=m+1;j++){
int s=0,e=0;
q[0]=j-1;
a^=1,b^=1;
for(int i=j;i<=n;i++){
while(s<e&&dx(q[s],q[s+1],b)>=sum[i]*dy(q[s],q[s+1])) s++;
while(s<e&&dx(q[e-1],q[e],b)*dy(q[e],i)>=dx(q[e],i,b)*dy(q[e-1],q[e])) e--;
q[++e]=i;
dp[a][i]=dp[b][q[s]]+w[i]-w[q[s]]-sum[q[s]]*(sum[i]-sum[q[s]]);
}
}
printf("%d\n",dp[a][n]);
}
return 0;
}