Lawrence

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

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
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2830 2832 2828 2827 2833 
四边形优化:
当函数w(i,j)满足 w(a,c)+w(b,d) <= w(b,c)+w(a,d) 且a<=b< c <=d 时,我们称w(i,j)满足四边形不等式。。

 
当函数w(i, j)满足w(i', j) <= w(i, j'); i <= i' < j <= j' 时,称w关于关于区间包含关系单

调。
 
s(i, j)=k是指m(i, j)这个状态的最优决策
 
以上定理的证明自己去查些资料
 
今天看得lrj的书中介绍的 四边形优化  做个笔记,加强理解 

最有代价用d[i,j]表示 
d[i,j]=min{d[i,k-1]+d[k+1,j]}+w[i,j] 
其中w[i,j]=sum[i,j] 
四边形不等式   
     w[a,c]+w[b,d]<=w[b,c]+w[a,d](a<b<c<d) 就称其满足凸四边形不等式 
决策单调性 
     w[i,j]<=w[i',j']   ([i,j]属于[i',j']) 既 i'<=i<j<=j'

于是有以下三个定理

定理一: 如果w同时满足四边形不等式 和 决策单调性 ,则d也满足四边形不等式
定理二:当定理一的条件满足时,让d[i,j]取最小值的k为K[i,j],则K[i,j-1]<=K[i,j]<=K[i+1,j] 
定理三:w为凸当且仅当w[i,j]+w[i+1,j+1]<=w[i+1,j]+w[i,j+1]

由定理三知 判断w是否为凸即判断 w[i,j+1]-w[i,j]的值随着i的增加是否递减 
于是求K值的时候K[i,j]只和K[i+1,j] 和 K[i,j-1]有关,所以 可以以i-j递增为顺序递推各个状态值最终求得结果  将O(n^3)转为O(n^2) 

 
 题目描述 :
在一条具有n个仓库的道路上,放置m个炸弹,被炸断之后的这段铁路[i,j],它的值为a[i]*(a[i+1],,,,a[j])+a[i+1]*a(a[i+2],,,+a[j])+,,,,+a[j-1]*a[j];,求被炸弹分割之后的铁路和的最小值

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. 
d[i][k]表示在前i个仓库里放k个炸弹,cost[i][j]表示[i,j]区间内的Strategic Value
d[i][k]=min(d[j][k-1]+cost[j+1][i]);
cost[i][j]=cost[i][j-1]+sum*a[j],sum==(a[i]+a[i+1]+a[i+2]+,,,,+a[j-1])
时间复杂度为1000*1000*1000=10^9,肯定会超时。
可以采用斜率优化或者四边形优化。

(设w[i][j]表示i到j的value,f[i][j]表示为前j个仓库放i个炸弹,那么可以得到f[i][j]=min{f[i-1][k-1]+w[k][j]}(i<k<=j),如果直接dp的话是O(M*N^2)的复杂度。

然而这个状态转移方程和POJ_1160的状态转移方程是一样的,因而只要我们能够证明w为凸的话,就可以证明f[i][j]为凸,那么就可以用四边形不等式进行优化了。

要证w为凸,只要证w[i][j]+w[i+1][j+1]<=w[i][j+1]+w[i+1][j]即可,也就是证明w[i+1][j]-w[i][j]是关于j单调递减的。又因为w[i][j]=w[i+1][j]+(A[j]-A[i])*a[i],其中a[i]表示i点权,A[i]表示1,2,…,i的点权和,代入到w[i+1][j]-w[i][j]中就可以得到w[i+1][j]-w[i][j]=(A[i]-A[j])*a[i],显然是随j的增加而递减的。这样我们就证明出了w为凸,进而可以证明f[i][j]的求解可以用四边形不等式优化。)

目前只会四边形优化,还有此题wa了很久的原因是无穷大的设立,inf=1<<20,不够大,inf=1e30才够大,
提醒我们需要注意细节。
设立初始值,d[i][0]=cost[1][i],用s[i][j]表示d[i][j]的决策。s[i][0]=0,s[n+1][i]=n;
1:s[i][k-1]<=s[i][k]<=s[i+1][k];
2:d[i][k]=min(d[j][k-1]+cost[j+1][i]);
观察表达式,k从小到大枚举(2式),i从大到小枚举(1式中s[i][k]<=s[i+1][k])。k在外层循环,i在内层循环(1式中s[i][k]<=s[i+1][k])
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 1100
#define LL long long
const int inf =1e18;
using namespace std;
int n,m;
LL a[];
LL cost[M][M];
LL d[M][M];
int s[M][M];
void init()
{
memset(d,,sizeof(d));
memset(cost,,sizeof(cost));
memset(s,,sizeof(s));
} void solve()
{
/* for(int i=1;i<=n;i++)
{
LL sum=0;
int j;
for(int t=1;t<=n;t++)
{
j=i+t;
if(j>n)
break;
sum+=a[j-1];
cost[i][j]=cost[i][j-1]+sum*a[j];
}
}*/
for(int i=;i<=n;i++){
LL sum=;
cost[i][i]=;
for(int j=i+;j<=n;j++){
sum+=a[j-];
cost[i][j]=cost[i][j-]+sum*a[j];
}
} /* for(int i=1;i<=n;i++)
{for(int j=1;j<=n;j++)
printf("%d ",cost[i][j]);
printf("\n");
}
printf("\n");
*/
for(int i=;i<=n;i++)
{
s[i][]=;
s[n+][i]=n;
d[i][]=cost[][i];
}
for(int k=;k<=m;k++)
for(int i=n;i>=;i--)
{
d[i][k]=1e18;
for(int j=s[i][k-];j<=s[i+][k];j++)
{
if((d[j][k-]+cost[j+][i])<d[i][k])
{
d[i][k]= d[j][k-]+cost[j+][i];
s[i][k]=j;
}
}
}
/* for(int i=0;i<=n+1;i++)
{for(int j=0;j<=m;j++)
printf("%d ",s[i][j]);
printf("\n");
}
printf("\n");
for(int i=0;i<=n;i++)
{for(int j=0;j<=m;j++)
printf("%d ",d[i][j]);
printf("\n");
}
printf("\n");*/
printf("%lld\n",d[n][m]);
} int main()
{
//freopen("test.txt","r",stdin);
while(scanf("%d%d",&n,&m))
{
if(n==&&m==)
break;
init();
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
solve();
}
return ;
}

hdu 2829(四边形优化 && 枚举最后一个放炸弹的地方)的更多相关文章

  1. HDU 2829 斜率优化DP Lawrence

    题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...

  2. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  3. HDU 2829 Lawrence(四边形优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

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

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

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

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

  6. HDU 3506 (环形石子合并)区间dp+四边形优化

    Monkey Party Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Tot ...

  7. 51Nod 1022 石子归并 V2(区间DP+四边形优化)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1022 题目大意: N堆石子摆成一个环.现要将石子有次序地合并成 ...

  8. 石子合并(直线版+环形版)&(朴素写法+四边形优化+GarsiaWachs算法)

    石子合并-直线版 (点击此处查看题目) 朴素写法 最简单常见的写法就是通过枚举分割点,求出每个区间合并的最小花费,从而得到整个区间的最小花费,时间复杂度为O(n^3),核心代码如下: ; i < ...

  9. B - Lawrence HDU - 2829 斜率dp dp转移方程不好写

    B - Lawrence HDU - 2829 这个题目我觉得很难,难在这个dp方程不会写. 看了网上的题解,看了很久才理解这个dp转移方程 dp[i][j] 表示前面1~j 位并且以 j 结尾分成了 ...

随机推荐

  1. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

  2. sgu179 SGU起航!

    //发现dfs除了搜索功能外的其他功能,他本身是一种序列,这个题恰是"先序"的下一个(合法范围内)序列! #include<iostream> #include< ...

  3. 寒武纪camp Day2

    补题进度:8/10 A(计数+BIT) 题意: 给一个长度为n的数组a[],任意选0<=i<=j<n,将a[i]~a[j]从小到大排序,形成新的数组.问有多少个不同的新数组. N,a ...

  4. 怎么配置ODBC microsoft Access 安装(win10)

    笔者看着书配置Access连接方法但就是找不到书中的"Access.mdb". 后来才知道.mdb需要自己创建....对于刚入门的我很无语啊!!!!! 下面带你走出小坑 1.从控制 ...

  5. win8,win10里面内置的IE浏览器网银无法输入密码

    win8,win10里面内置的IE浏览器网银无法输入密码,安装控件也没效果,部分网银直接导致IE崩溃,只需要简单设置即可解决. 方法/步骤   1 打开IE浏览器,点击右上角的小齿轮图标,在下拉菜单中 ...

  6. HUNT:一款可提升漏洞扫描能力的BurpSuite漏洞扫描插件

    今天给大家介绍的是一款BurpSuite插件,这款插件名叫HUNT.它不仅可以识别指定漏洞类型的常见攻击参数,而且还可以在BurpSuite中组织测试方法. HUNT Scanner(hunt_sca ...

  7. linux字符驱动之poll机制按键驱动

    在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动.虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的:在实际的应用场所里,有没有那么一种情 ...

  8. influxDB系列(二)

    来源于我在一个influxDB的qq交流群中的提问, 然后有个人 提了一个问题---->触发了我的思考!! :) 哈哈 自己的每一次说出一个回答,都是一次新的思考,也都进行了一些查阅资料,思考, ...

  9. jni——如何转换有符号与无符号数

    java数据结构默认均为有符号数,而通过jni转换到c/c++层,却不一定是有符号数. 如若在java中存储的即为无符号数,则在jni中可将jbyte直接进行类型转换. 若进行操作,则可在计算时,先将 ...

  10. setenv LD_LIBRARY_PATH

    For most Linux binaries, NCL was built using gcc and gfortran. This may cause a dependency on a file ...