#HDU2829#Lawrence(DP+ 斜率优化)

来源:互联网 发布:淘宝店铺店标素材 编辑:程序博客网 时间:2024/06/05 11:56

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4109    Accepted Submission(s): 1873


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 14 5 1 24 24 5 1 20 0
 

Sample Output
172
 
题目大意:给定一个长度为n的序列,至多将序列分成m+1段,每段序列都有权值,权值为序列内两个数两两相乘之和。求序列权值和最小为多少?

Dp[j][i]表示前i个数,分成j段能得到的最小序列权值和。
令w[a][b]表示从a到b的最小序列权值和
令W[i]表示从1到i的序列权值和
令sum[i]为前缀和,A[i]为数值
则有:w[a + 1][b] = W[b] - W[a] - sum[a] * (sum[b] - sum[a])

基础DP式:
Dp[j][i] = min(Dp[j - 1][k] + w[k + 1][i])
则有:
Dp[j][i] = min(Dp[j - 1][k] + W[i] - W[k] - sum[k] * (sum[i] - sum[k]))

状态是O(N ^ 2)的,但是转移是O(N)的,硬转总时间为O(N ^ 3),超时。
刚开始学斜率优化的DP,现给出两种理解思路:

#1:

假设p<k<i,对于Dp[j][i],如果k比p更优,则:

Dp[j - 1][k] + W[i] - W[k] - sum[k] * (sum[i] - sum[k])< Dp[j - 1][p] + W[i] - W[p] - sum[p] * (sum[i] - sum[p])

化简,移项:

(Dp[j - 1][k] - W[k]  + sum[k] * sum[k]) - (Dp[j - 1][p]  - W[p] + sum[p] * sum[p])/((sum[k] *   - sum[p])< sum[i]

令等式左边上方括号分别为yk, yp, 下方分别对应为xk,xp,这就成为一个斜率的表达式了。

分3种情况讨论k存在的意义=>所有决策点满足一个下凸包的性质。

#2:(做题比较推荐第二种,但建议先理解第一种)

直接看方程,

y = Dp[j - 1][k] + w[i] - w[k] + sum[k] ^ 2

x = sum[k] , b = sum[i] ,  g = Dp[j][i]

则有直线方程:          y - bx = g

平面上若干点(x,y)都由若干决策点(即k的值)决定,将此直线从下向上平移,它接触到的第一个点为最佳决策点。(请画图或自行想象)

因为斜率b是上升的,所以,下一阶段的直线方程斜率更高,于是最佳决策点一定形成了下凸包序列。


用单调队列实现,队头判断相邻两点间斜率是否满足#1中的结论,队尾保证是下凸包。

给出向量叉乘公式:


#include<iostream>#include<cstdio>#include<cstdlib>using namespace std;typedef long long LL;const int Max = 1005;int N, M;int fro, back;int A[Max], Q[Max];LL Dp[Max][Max];LL sum[Max], W[Max];void getint(int & num){     char c;int flg = 1;    num = 0;     while((c = getchar()) < '0' || c > '9')   if(c == '-')    flg = -1;     while(c >= '0' && c <= '9'){num = num * 10 + c - 48;  c = getchar();}     num *= flg; } LL get_Dp(int i, int k, int j){return Dp[j - 1][k] + W[i] - W[k] + sum[k] * (sum[k] - sum[i]);}LL nume(int j, int a, int b){return (Dp[j][a] - W[a] + sum[a] * sum[a]) - (Dp[j][b] - W[b] + sum[b] * sum[b]);}LL deno(int a, int b){return sum[a] - sum[b];}int main(){while(~scanf("%d%d", &N, &M)&& N && M){++ M;sum[0] = W[0] = 0;for(int i = 1; i <= N; ++ i){getint(A[i]);sum[i] = sum[i - 1] + A[i];W[i] = W[i - 1] + sum[i - 1] * A[i];}for(int i = 1; i <= N; ++ i)Dp[1][i] = W[i];for(int j = 2; j <= M; ++ j){fro = 1, back = 0;Q[++ back] = 0;for(int i = 1; i <= N; ++ i){while(fro < back && nume(j - 1, Q[fro + 1], Q[fro]) < sum[i] * deno(Q[fro + 1], Q[fro]))++ fro;Dp[j][i] = get_Dp(i, Q[fro], j);while(fro < back && nume(j - 1, i, Q[back]) * deno(Q[back], Q[back - 1]) <= nume(j - 1, Q[back], Q[back - 1]) * deno(i, Q[back]))-- back;Q[++back] = i;}}printf("%I64d\n", Dp[M][N]);}return 0;}