题目链接: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

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

题意:

给出一个序列,定义一个连续段的值为:连续段内每每两个数的积之和。现要求将序列断开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的更多相关文章

  1. HDU2829 Lawrence(斜率优化dp)

    学了模板题之后上网搜下斜率优化dp的题目,然后就看到这道题,知道是斜率dp之后有思路就可以自己做不出来,要是不事先知道的话那就说不定了. 题意:给你n个数,一开始n个数相邻的数之间是被东西连着的,对于 ...

  2. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  3. HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

    题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...

  4. 【转】斜率优化DP和四边形不等式优化DP整理

    (自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...

  5. bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

    题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...

  6. bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

    题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...

  7. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  8. 【BZOJ-1096】仓库建设 斜率优化DP

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3719  Solved: 1633[Submit][Stat ...

  9. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

随机推荐

  1. k8s的使用入门

    1.kubectl命令就是apiserver的客户端工具,可以实现对nodes资源的增删改查. # 描述一个节点的信息 kubectl describe node k8s-node1 # 查看k8s集 ...

  2. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  3. Mac环境下安装运行splash

    http://blog.csdn.net/chenhy8208/article/details/69391097 最近需要使用scrapy爬虫做一些开发,用到了splash.我本机是mac环境,跳着看 ...

  4. scala工具sbt的安装和使用;idea如何创建scala项目

    scala的sbt类似于java的maven mac:brew install sbt linux:yum Install sbt 或者下载二机制包 使用sbt需要想mvn一样搭建公司私服,不然,下载 ...

  5. 5.【nuxt起步】-swiper组件

    接下来是一个比较常用,也比较重要的组件 swiper,可以自行搜索 vue swiper,有很多开源组件,我这里就复用之前一个熟悉的, 1.新建component/banner.vue 刷新报错: 要 ...

  6. editplus重新载入文档

    editplus重新载入文档 :document->reload

  7. karaf中利用Bundle引入外部log4j配置文件

    环境准备: 1.在karaf_home下新建 config及logs目录 2.将mylog4j.properties拷贝到config文件夹下 查看log4j-1.2.17.jar/MANIFEST. ...

  8. sql_视图和函数

    创建视图: create view xxx as select * from userinfo; 删除视图: drop view xxx 修改视图: alter view xxx as selete ...

  9. SolidEdge 打开工程图提示图纸已过期怎么办

    如下图所示,打开工程图时提示图纸已过期   点击工具-图纸视图跟踪器,按提示打开过期的装配体文件   更新这个装配体文件   然后切换到刚才提示过期的工程图文件,点击更新视图,下次再打开的时候就不会提 ...

  10. Hadoop 源码阅读技巧

    http://www.cnblogs.com/xuxm2007/category/388607.html     个人谈谈阅读hadoop源代码的经验.首先,不得不说,hadoop发展到现在这个阶段, ...