HDU2829 Lawrence —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-2829
Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4678 Accepted Submission(s): 2150
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 5 1 2
4 2
4 5 1 2
0 0
2
题意:
给出一个序列,定义一个连续段的值为:连续段内每每两个数的积之和。现要求将序列断开m处,即把序列断成m+1断子序列。使得每段的值之和最小。
题解:
1.设dp[i][j]为:第j个数属于第i段时,值之和的最小值。可得:dp[i][j] = min(dp[i-1][k] + cost[k+1][i]) 其中 i-1<=k<=j-1。
2.对于cost[i][j]:
可知:cost[1][j] = cost[1][k] + cost[k+1][j] + sum[k]*(sum[j] - sum[k]),
移项:cost[k+1][j] = cost[1][j] - cost[1][k] - sum[k]*(sum[j] - sum[k]),因而cost数组可改成一维,sum为前缀和。
因此:dp[i][j] = min(dp[i-1][k] + cost[j] - cost[k] - sum[k]*(sum[j] - sum[k])) 其中 i-1<=k<=j-1。
此步转化的目的是要分离 i 和 j,使得在使用斜率优化时能够去掉 i ,变成只与 j 、k有关的式子。
3.斜率优化DP,与此题HDU3480 Division的形式一样。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; int sum[MAXN], cost[MAXN], dp[MAXN][MAXN];
int q[MAXN], head, tail; int getUP(int i, int k1, int k2)
{
return (dp[i-][k1] - cost[k1] + sum[k1]*sum[k1])-
(dp[i-][k2] - cost[k2] + sum[k2]*sum[k2]);
} int getDOWN(int k1, int k2)
{
return sum[k1]-sum[k2];
} int getDP(int i, int j, int k)
{
return dp[i-][k] + cost[j] - cost[k] - sum[k]*(sum[j]-sum[k]);
} int main()
{
int n, m;
while(scanf("%d%d", &n,&m)&&(m||n))
{
sum[] = cost[] = ;
for(int i = ; i<=n; i++)
{
int val;
scanf("%d", &val);
sum[i] = sum[i-] + val;
cost[i] = cost[i-] + val*sum[i-];
} for(int i = ; i<=n; i++) //初始化第一阶段
dp[][i] = cost[i];
for(int i = ; i<=m+; i++) //从分成i-1段的状态转移到分成i段的状态
{
head = tail = ;
q[tail++] = i-; //因为分成i-1段最少需要i-1个数,故备选状态从i-1开始
for(int j = i; j<=n; j++) //因为分成i段最少需要i个数,故从i开始
{
while(head+<tail && getUP(i, q[head+],q[head])<=
getDOWN(q[head+],q[head])*sum[j]) head++;
dp[i][j] = getDP(i, j, q[head]); while(head+<tail && getUP(i, j, q[tail-])*getDOWN(q[tail-],q[tail-])<=
getUP(i, q[tail-],q[tail-])*getDOWN(j,q[tail-])) tail--;
q[tail++] = j;
}
}
printf("%d\n", dp[m+][n]);
}
}
HDU2829 Lawrence —— 斜率优化DP的更多相关文章
- HDU2829 Lawrence(斜率优化dp)
学了模板题之后上网搜下斜率优化dp的题目,然后就看到这道题,知道是斜率dp之后有思路就可以自己做不出来,要是不事先知道的话那就说不定了. 题意:给你n个数,一开始n个数相邻的数之间是被东西连着的,对于 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...
- bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...
- [BZOJ3156]防御准备(斜率优化DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
- BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP
1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...
随机推荐
- MyBatis_SelectKey使用oracle 序列插入主键
mapper 如下: 使用<selectkey>实现 也可以使用oracle的row 级触发器trigger实现: <?xml version="1.0" enc ...
- Silverlight 离线安装包
直接下载地址 https://www.microsoft.com/getsilverlight/locale/en-us/html/Microsoft%20Silverlight%20Release% ...
- 邁向IT專家成功之路的三十則鐵律 鐵律八:IT人學習之道-基礎功
修練過中國武術的人都知道,任何一種拳法的學習最重要的就是基礎功,而基礎功又可分為內在與外在的修練,內在的修練強調在平心.靜氣.不爭的調息.至於外在這首重在站樁.鬆沉.不疾不徐的應對能力.有了深厚基礎的 ...
- Android 蓝牙 笔记
安卓:短信复制机制 软件识别蓝牙 软件可以读取蓝牙的信息 给蓝牙弄上一个类似于短信的东西 并且存一个短信 然后发到客户端 客户端 可以分类 分成表格 形式 这样做的目的是为了 让你的设备可以写蓝牙给 ...
- 5.【nuxt起步】-swiper组件
接下来是一个比较常用,也比较重要的组件 swiper,可以自行搜索 vue swiper,有很多开源组件,我这里就复用之前一个熟悉的, 1.新建component/banner.vue 刷新报错: 要 ...
- Delphi图像处理 -- 颜色矩阵变换
转载自阿发伯:http://blog.csdn.net/maozefa/article/details/8316430 阅读提示: <Delphi图像处理>系列以效率为侧重点,一般 ...
- mysql 授权新的root用户
grant all privileges to *.* on system@'localhost' identified by 'woshishui' with grant option;
- java性能监控工具jconsole-windows
jconsole Starts a graphical console that lets you monitor and manage Java applications. Synopsis jco ...
- substr使用注意
substr使用时要判断起点和长度是否超过了串本身的长度,否则会抛异常
- 读《疯狂Java讲义》笔记总结三
1.初始化块 实际上初始化块是一个假象,使用javac命令编译Java类后,该Java类中的初始化块会消失--初始化块中代码会被 "还原" 到每一个构造器中,且位于构造器全部代码的 ...