HDU 2829 Lawrence(斜率DP)

Lawrence

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


 

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

 

17 2

 

Source

2009 Multi-University Training Contest 2 - Host by TJU

 

Recommend

gaojie

 

【思路】

状态设计:dp[i][j]表示前i个炸j次得到的最小值,其中0 <=j <= m,j < i <= n,1 <= k < i。

很容易想到DP方程:dp[i][j] = min{dp[k][j - 1] + cost[k + 1][i]},cost[x][y]即x到y整段的值。

这么硬搞导致时间复杂度为O(N^3),肯定会超时。

不妨把cost这么表示,cost[x][y] = w[y] - w[x - 1] - sum[x - 1] * (sum[y] - sum[x - 1]),使整个DP方程变为:

dp[i][j] = min{dp[k][j - 1] + w[i] - w[k] - sum[k] * sum[i] + sum[k]^2}。

所以当1 <= p < k <= i - 1,若满足:

\frac{(dp[k][j - 1] - w[k] + sum[k]^{2}) - (dp[p][j - 1] - w[p] + sum[p]^{2})}{sum[k] - sum[p]} \leq sum[i]

则dp[k][j - 1]比dp[p][j - 1]是更好的转移选择,否则后者更优。接下来就是斜率优化DP了,维护下凸包折线如此往复即可。

除了dp[i][0],我们每一个dp[i][j]都是由某个1 <= x < i对应的dp[x][j - 1]转移而来的,下凸包折线共有(m + 1)条,我们就用(m + 1)个双端队列去维护就好了。特别注意写的时候状态层次不要搞错了,不等式符号不要搞反了。

 

【代码】

//******************************************************************************
// File Name: HDU_2829.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月10日 星期五 10时43分32秒
//******************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int MAXN = 1005;

int n, m;
ll sum[MAXN], w[MAXN];
int head[MAXN], tail[MAXN];
ll dq[MAXN][MAXN], dp[MAXN][MAXN];

ll get_up(int j, int k, int p)
{
	return (dp[k][j] - w[k] + sum[k] * sum[k]) - (dp[p][j] - w[p] + sum[p] * sum[p]);
}

ll get_down(int k, int p)
{
	return (sum[k] - sum[p]);
}

bool judge(int j, int i)
{
	ll x1 = get_down(dq[j][tail[j] - 1], dq[j][tail[j] - 2]);
	ll y1 = get_up(j, dq[j][tail[j] - 1], dq[j][tail[j] - 2]);
	ll x2 = get_down(i, dq[j][tail[j] - 1]);
	ll y2 = get_up(j, i, dq[j][tail[j] - 1]);
	return (x1 * y2 - y1 * x2) < 0;
}

bool check(int j, int i)
{
	return (get_up(j, dq[j][head[j]], dq[j][head[j] + 1]) >= sum[i] * get_down(dq[j][head[j]], dq[j][head[j] + 1]));
}

int main()
{
	while (scanf("%d %d", &n, &m) == 2 && (n | m)) {
		sum[0] = w[1] = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%lld", &sum[i]);
			sum[i] += sum[i - 1];
			w[i] = w[i - 1] + sum[i - 1] * (sum[i] - sum[i - 1]);
		}
		for (int j = 0; j <= m; j++) {
			head[j] = tail[j] = 0;
			for (int i = j + 1; i <= n; i++) {
				if (j == 0)
					dp[i][j] = w[i];
				else {
					while (tail[j - 1] - head[j - 1] >= 2 && check(j - 1, i)) head[j - 1]++;
					int k = dq[j - 1][head[j - 1]];
					dp[i][j] = dp[k][j - 1] + w[i] - w[k] - sum[k] * sum[i] + sum[k] * sum[k];
				}
				while (tail[j] - head[j] >= 2 && judge(j, i)) tail[j]--;
				dq[j][tail[j]++] = i;
			}
		}
		printf("%lld\n", dp[n][m]);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值