DP/四边形不等式


  做过POJ 1739 邮局那道题后就很容易写出动规方程:

    dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价)

  $w(l,r)=\sum_{i=l}^{r}\sum_{j=i+1}^{r}a[i]*a[j]$

  那么就有 $w(l,r+1)=w(l,r)+a[j]*\sum\limits_{i=l}^{r}a[i]$

  所以:w[i][j]明显满足 关于区间包含的单调性

  然后我们大胆猜想,小(bu)心(yong)证明,w[i][j]满足四边形不等式,所以这题就跟邮局那题一样了……

  咳咳好吧作为一个有节操的人,我还是尝试着证明了一下(结果发现用来证明的时间比我写代码的时间要长……)

  先把w(i,j)的定义搬下来:\[ w(l,r)=\sum\limits_{i=l}^{r}\sum\limits_{j=i+1}^{r}a[i]*a[j] \]

  形象一点来说就是:

    对于$ i\leq i' < j \leq j' $

    中间的都是要算两次,剩下的部分:

      (左)表示w(i,i'-1),[左]表示 $\sum_{k=i}^{i'-1}a[k] $

      (中)表示w(i',j),[中]表示 $\sum_{k=i'}^j a[k] $

      (右)表示w(j+1,j'),[右]表示 $\sum_{k=j+1}^{j'} a[k] $

    \[ w(i,j)+w(i',j')=(左)+[左]*[中]+(右)+[右]*[中]+(中) \\ w(i,j')+w(i',j)=(左+右)+[左+右]*[中]+(中)  \]

    

    其中\[ [左+右]*[中]=[左]*[中]+[右]*[中]  \]

    但\[ (左+右)=(左)+(右)+[左]*[右] \]

    所以\[ (左+右)>(左)+(右) \]

    所以\[w(i,j)+w(i',j') \leq w(i,j')+w(i',j) \]  

 //HDOJ 2829
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
#define CC(a,b) memset(a,b,sizeof(a))
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') sign=-; ch=getchar();}
while(isdigit(ch)) {v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=,INF=~0u>>;
const double eps=1e-;
#define debug
/*******************template********************/
int dp[N][N],s[N][N],w[N][N],b[N],a[N],n,m; int main(){
while(scanf("%d%d",&n,&m)!=EOF && n){
m++;
F(i,,n) a[i]=getint();
F(i,,n){
b[i]=a[i];
w[i][i]=;
F(j,i+,n){
w[i][j]=w[i][j-]+a[j]*b[i];
b[i]+=a[j];
}
}
F(i,,n) F(j,,m) dp[j][i]=INF;
F(i,,n){
dp[][i]=w[][i];
s[][i]=;
}
F(i,,m){
s[i][n+]=n;
D(j,n,i)
F(k,s[i-][j],s[i][j+])
if(dp[i-][k]+w[k+][j]<dp[i][j]){
s[i][j]=k;
dp[i][j]=dp[i-][k]+w[k+][j];
}
}
printf("%d\n",dp[m][n]);
}
return ;
}

Lawrence

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

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

【HDOJ】【2829】Lawrence的更多相关文章

  1. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

  2. 【集训笔记】博弈论相关知识【HDOJ 1850【HDOJ2147

    以下资料来自:http://blog.csdn.net/Dinosoft/article/details/6795700 http://qianmacao.blog.163.com/blog/stat ...

  3. 【HDOJ 5379】 Mahjong tree

    [HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...

  4. HDOJ 1238 Substrings 【最长公共子串】

    HDOJ 1238 Substrings [最长公共子串] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  7. 【HDOJ 2089】不要62

    [HDOJ 2089]不要62 第一个数位dp的题 做的老困难了...只是好歹是做出来了 迈出了第一步.. 对大牛来说这样的题都是小case ps:新上一个记忆化方法 一些绕弯的题里用dfs好想些 代 ...

  8. 【HDOJ 5371】 Hotaru&#39;s problem

    [HDOJ 5371] Hotaru's problem Manacher算法+穷举/set Manacher算法一好文:http://blog.csdn.net/yzl_rex/article/de ...

  9. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  10. 【HDOJ 5399】Too Simple

    pid=5399">[HDOJ 5399]Too Simple 函数映射问题 给出m函数 里面有0~m个函数未知(-1) 问要求最后1~n分别相应仍映射1~n 有几种函数写法(已给定的 ...

随机推荐

  1. WindowsApi 解压缩文件

    .解压方法 转载自http://www.2cto.com/kf/201204/128704.html "C#解压.zip文件,网上一搜一大堆方法,有使用System.IO.Compressi ...

  2. Operation is not valid due to the current state of the object.

    今天遇到一个asp.net的草郁闷的问题,看下截图 狂晕啊,在google上狂搜了一下,好在已经有大侠也遇到过这个问题了,先看下别人的解决办法吧 Operation is not valid due ...

  3. 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法

    用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count Ac ...

  4. IOS开发之后台处理

    1 前言IOS4 之后提供了后台处理,在后台运行应用程序,在一些情形下甚至可以在用户按下Home按钮之后在后台运行. 2 详述IOS可以在用户按下Home按钮后将应用程序添加到暂停状态.这种暂停执行的 ...

  5. WPF数据双向绑定

    设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...

  6. [css filter]filter在界面实现滤镜效果

    最近逛当当,发现当当尾品会的首页推荐最底端的商品链接是灰色的图片,然后鼠标hover之后就会变成正常的彩色 肯定不是通过img来改变的,然后直接看了一下源码,其实是用的filter属性 _(:з」∠) ...

  7. XAML(2) - 依赖属性

    3.依赖属性 在用WPF编程时,常常会遇到"依赖属性"这个术语.WPF元素是带有方法,属性和事件的类.WPF元素的几乎每个属性都是依赖属性, 这是什么意思?依赖属性可以依赖其他输入 ...

  8. java的基本数据类型特征

    java的数据类型分为基本数据类型和引用数据类型. 基本数据类型分为数值型.字符型(char).布尔型(boolean) 数值型变量 1.整数型 类型 占用存储空间 表示范围 byte 1字节Byte ...

  9. .Net码农学Android---小点整理

    小点整理 虽然两大语言的编程思想相同,语法也相似,但具体到使用时,还是有些别扭,可能还是不太熟悉,现就自己遇到的一些微小问题整理如下: String/Int转换 C#:String--->Int ...

  10. SQL30081N 检测到通信错误。正在使用的通信协议:"TCP/IP"

    环境描述: 今天在虚拟机上安装了Linux系统,并且装了DB2,但是在连接的时候遇到了个问题,百思不得其解.下面是具体问题跟解决办法 问题描述: 解决办法: 1.先ping服务器是否可以ping通. ...