HDU 2829 - Lawrence - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829
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
题意:
给出一条笔直无分叉的铁路上有n个仓库,每个仓库有一个v[i]代表价值;
每两个仓库之间算作一段铁路,现在有m次攻击机会,一次攻击可以炸毁一段铁路;
m次攻击后,剩余的总价值为:Σ(v[i]*v[j]),i和j为所有任意两个互相可到达的仓库。
现要求选定m段铁路进行攻击炸毁,然后使得总价值最小。
题解:
设dp[i][j]是前i个仓库,炸掉j段铁路后,剩余总价值的最小值。(显然,j<i)
设w[a][b]表示铁路完好的情况下,从a仓库到b仓库的总价值,即:

那么,就有:
dp[i][j] = min( dp[k][j-1] + w[k+1][i] ),j≤k<i;
方程的意义是:炸毁仓库k和仓库k+1之间的那段铁路(即第k段铁路),算出总价值,枚举k找到最小的。
那么如何计算w[k+1][i]呢?
假设$sum\left[ i \right] = \sum\limits_{k = 1}^i {v\left[ k \right]}$,那么就有:
w[1][i] = w[1][k] + w[k+1][i] + (v[1]+v[2]+…+v[k]) × (v[k+1]+v[k+2]+…+v[i])
= w[1][k] + w[k+1][i] + sum[k] × (sum[i]-sum[k])
即w[k+1][i] = w[1][i] - w[1][k] - sum[k] × (sum[i]-sum[k])
我们把w[k+1][i]的计算式带入状态转移方程得到:
dp[i][j] = min{ dp[k][j-1] + w[1][i] - w[1][k] - sum[k] × (sum[i]-sum[k]) }
那么,对于这个DP,j一个循环、i一个循环、k一个循环,就是O(n3)的时间复杂度;
需要斜率优化,优化到O(n2)即可。
对于第a段铁路和第b段铁路(1≤a<b<i),我们若有:

可以说第b段铁路优于第a段铁路。
对上式进行变形可得:

我们假设:

那么
选择炸毁第b段铁路优于炸毁第a段铁路 <=> g(a,b) ≤ sum[i]
选择炸毁第b段铁路劣于炸毁第a段铁路 <=> g(a,b) > sum[i]
然后后面的操作就和HDU3507http://www.cnblogs.com/dilthey/p/8745843.html差不多了:
①在计算dp[i][j]时,若有j≤a<b<c<i,只要满足g(a,b) ≥ g(b,c),则b点必然被淘汰.
证明:若g(b,c) ≤ sum[i],则选择第c段铁路优于第b段铁路,b淘汰;
若g(b,c) > sum[i],则g(a,b) > sum[i],则选第b段铁路差于第a段,b淘汰。
②若在计算dp[i][j]时,k点被淘汰,则计算dp[i+1][j]时,k点必然也被淘汰.
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+; int n,m;
int sum[maxn]; //前缀和
int w[maxn]; //w[1][i]
int dp[maxn][maxn];
int q[maxn],head,tail; double g(int a,int b,int j)
{
double up = (dp[b][j-]-w[b]+sum[b]*sum[b]) - (dp[a][j-]-w[a]+sum[a]*sum[a]);
double down = sum[b]-sum[a];
return up/down;
}
int main()
{
while(scanf("%d%d",&n,&m) && n+m>)
{
sum[]=;
for(int i=,tmp;i<=n;i++)
{
scanf("%d",&tmp);
sum[i]=sum[i-]+tmp;
w[i]=w[i-]+sum[i-]*tmp;
} for(int i=;i<=n;i++) dp[i][]=w[i];
for(int j=;j<=m;j++)
{
head=tail=;
q[tail++]=j;
for(int i=j+,a,b;i<=n;i++)
{
while(head+<tail)
{
a=q[head], b=q[head+];
if(g(a,b,j)<=sum[i]) head++;
else break;
}
int k=q[head];
dp[i][j]=dp[k][j-]+w[i]-w[k]-sum[k]*(sum[i]-sum[k]); while(head+<tail)
{
a=q[tail-], b=q[tail-];
if(g(a,b,j)>=g(b,i,j)) tail--;
else break;
}
q[tail++]=i;
}
} printf("%d\n",dp[n][m]);
}
}
注意点:
本题看到v[i]在1到100之间,所以不会出现sum[b]-sum[a]=0这种除数为零的情况,所以可以直接求g(a,b),而不用再转成乘法比大小。
斜率DP也算是有一定的套路,能够优化一维的时间复杂度,所以刚开始做斜率DP的时候,应先从普通的DP进行考虑(包括条件初始化、状态转移的顺序之类),然后再考虑加入斜率优化。
HDU 2829 - Lawrence - [斜率DP]的更多相关文章
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
- HDU 2829 Lawrence (斜率DP)
斜率DP 设dp[i][j]表示前i点,炸掉j条边的最小值.j<i dp[i][j]=min{dp[k][j-1]+cost[k+1][i]} 又由得出cost[1][i]=cost[1][k] ...
- HDU 3480 - Division - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- ACM-ICPC 2016 沈阳赛区现场赛 I. The Elder && HDU 5956(斜率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5956 题意:一颗树上每条边有个权值,每个节点都有新闻要送到根节点就是1节点,运送过程中如果不换青蛙就是 ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HDU 2829 [Lawrence] DP斜率优化
解题思路 首先肯定是考虑如何快速求出一段铁路的价值. \[ \sum_{i=1}^k \sum_{j=1, j\neq i}^kA[i]A[j]=(\sum_{i=1}^kA[i])^2-\sum_{ ...
- HDU.2829.Lawrence(DP 斜率优化)
题目链接 \(Description\) 给定一个\(n\)个数的序列,最多将序列分为\(m+1\)段,每段的价值是这段中所有数两两相乘的和.求最小总价值. \(Solution\) 写到这突然懒得写 ...
- HDU 2829 Lawrence(四边形优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
随机推荐
- Xcode工程添加第三方文件的详细分析 Create folder references for any added folders(转)
在开发iOS项目的时候需要导入第三方的库文件,但是通过Xcode导入第三方源文件的时候会提示一些信息,不知所以然. 现在看到的文档都是针对Xcode3的,针对Xcode4的说明很少,现在分享出来. 官 ...
- Java实现高效的枚举元素集合
Set是Java集合类的重要组成部分,它用来存储不能重复的对象.枚举类型也要求其枚举元素各不相同.看起来枚举类型和集合是很相似的.然而枚举类型中的元素不能随意的增加.删除,作为集合而言,枚举类型非常不 ...
- phonegap入门–2 Android phonegap工程建立
一.环境要求: 需要安装Android ADT 二.支持Android相关设备列表: a)Android 2.1 (Deprecated May 2013) b)Android 2.2 c)Andro ...
- 6.824 Lab 5: Caching Extents
Introduction In this lab you will modify YFS to cache extents, reducing the load on the extent serve ...
- smarty模板文件书写javascript代码
在smarty文件里直接写javascript代码时候,造成500错误. javascript代码有很多的{}在同一行,而{}也是smarty引擎解析模板的关键标识符,smarty将对其进行分析,这时 ...
- 2014 华为校招机试题(c/c++开发类)
第一题: 1.2.3....n盏灯,同时有n个人, 第1个人将1的倍数的灯拉一下, 第2个人将2的倍数的灯拉一下, ...... 问最后有几盏灯是亮的, 初始状态下灯是灭的, 输入整数n(n<6 ...
- Windows驱动开发之线程与同步事件
转载请注明来源: enjoy5512的博客 : http://blog.csdn.net/enjoy5512 GitHub : https://github.com/whu-enjoy .1. 使用系 ...
- MegaCli 使用
安装: wget ftp://rpmfind.net/linux/Mandriva/devel/cooker/x86_64/media/non-free/release/megacli-8.02.21 ...
- WP8.1学习系列(第二十二章)——在页面之间导航
在本文中 先决条件 创建导航应用 Frame 和 Page 类 页面模板中的导航支持 在页面之间传递信息 缓存页面 摘要 后续步骤 相关主题 重要的 API Page Frame Navigation ...
- JS方法 - 字符串处理函数封装汇总 (更新中...)
一.计算一段字符串的字节长度 字符串的charCodeAt()方法, 可返回字符串固定位置的字符的Unicode编码,这个返回值是0-65535之间的整数,如果值<=255时为英文,反之为中文. ...