Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4109 Accepted Submission(s): 1873
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
#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;
}