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
 
题意:n(1<=n<=1000)个数,将其分成m + 1 (0 <= m < n)组,要求每组数必须是连续的而且要求得到的价值最小。
一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0
思路:可以把题目理解为整数划分类型的题目,关键是打表发现可以用四边形不等式优化
dp[i][j] 前i个数 分成j组  dp[i][j]=min(dp[k][j-1]+(d[i]-(sum[i]-sum[k])*sum[k]-d[k]); d[]表示前缀的任意两点的权值和   sum[]为前缀和
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int a[];
int sum[];
int d[];
int dp[][]; //前i个点分成j组
int s[][];
int main(){
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
if(!n&&!m) break;
memset(dp,inf,sizeof(dp));
for(int i=;i<=n;i++)
cin>>a[i],sum[i]=sum[i-]+a[i];
for(int i=;i<=n;i++){
d[i]=a[i]*sum[i-]+d[i-];
}
for(int i=;i<=n;i++){
dp[i][]=d[i];
s[i][]=;
}
for(int j=;j<=m+;j++){
s[n+][j]=n;
for(int i=n;i>=j;i--){
for(int k=s[i][j-];k<=s[i+][j];k++){
if(dp[i][j]>dp[k][j-]+d[i]-(sum[i]-sum[k])*sum[k]-d[k]){
dp[i][j]=dp[k][j-]+d[i]-(sum[i]-sum[k])*sum[k]-d[k];
s[i][j]=k;
}
}
}
}
cout<<dp[n][m+]<<endl;
}
return ;
}

hdu 2829 Lawrence(四边形不等式优化dp)的更多相关文章

  1. hdoj 2829 Lawrence 四边形不等式优化dp

    dp[i][j]表示前i个,炸j条路,并且最后一个炸在i的后面时,一到i这一段的最小价值. dp[i][j]=min(dp[i][k]+w[k+1][i]) w[i][j]表示i到j这一段的价值. # ...

  2. [HDU2829] Lawrence [四边形不等式优化dp]

    题面: 传送门 思路: 依然是一道很明显的区间dp 我们设$dp\left[i\right]\left[j\right]$表示前$j$个节点分成了$i$块的最小花费,$w\left[i\right]\ ...

  3. HDU 2829 Lawrence(斜率优化DP O(n^2))

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

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

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

  5. 【无聊放个模板系列】HDU 3506 (四边形不等式优化DP-经典石子合并问题[环形])

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  6. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】

    题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...

  7. codevs3002石子归并3(四边形不等式优化dp)

    3002 石子归并 3 参考 http://it.dgzx.net/drkt/oszt/zltk/yxlw/dongtai3.htm  时间限制: 1 s  空间限制: 256000 KB  题目等级 ...

  8. CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性

    LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...

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

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

随机推荐

  1. Parcelable encountered IOException writing serializable object

    异常: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object 这是在in ...

  2. (最简单)红米手机5A的USB调试模式在哪里开启的方法

    当我们使用安卓手机链接Pc的时候,或者使用的有些APP比如我们公司营销小组当使用的APP引号精灵,之前使用的老版本就需要开启usb调试模式下使用,现当新版本不需要了,如果手机没有开启usb调试模式,P ...

  3. MySQL慢查询日志释疑总结

      之前写了一篇"MySQL慢查询日志总结",总结了一些MySQL慢查询日志常用的相关知识,这里总结一下在工作当中遇到关于MySQL慢查询日志的相关细节问题,有些是释疑或自己有疑惑 ...

  4. ssh服务突然连接不了案例总结

    ssh服务突然连接不了案例总结   一台Oracle数据库服务器(Linux版本为Oracle Linux Server release 5.7)今天中午突然出现短暂的ssh连接不上的情况,ssh连接 ...

  5. 【转贴】一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历

    原文:https://juejin.im/post/5902e087da2f60005df05c3d ------------------------------------------------- ...

  6. Python基础——0前言

    python虽然这几年才兴起,但是已经是一门“老”语言了. python的诞生历史也很有趣.Python的创始人为Guido van Rossum(龟叔).1989年圣诞节期间,在阿姆斯特丹,Guid ...

  7. git 忽略 .idea文件

    多人开发时,会出现明明在gitignore中忽略了.idea文件夹,但是提交时仍旧会出现.idea内文件变动的情况 原因.idea已经被git跟踪,之后再加入.gitignore后是没有作用的 解决办 ...

  8. vue 组件中的钩子函数 不能直接写this

    export default { data(){ return { num: 18 } }, beforeRouteEnter(to, from, next){ next(vm=>{ vm.nu ...

  9. HttpServletResponse简单理解

    Web服务器收到一个http请求,会针对每个请求创建一个HttpServletRequest和HttpServletResponse对象,从客户端取数据用HttpServletRequest,向客户端 ...

  10. uWSGI、WSGI和uwsgi

    WSGI wsgi server (比如uWSGI) 要和 wsgi application(比如django )交互,uwsgi需要将过来的请求转给django 处理,那么uWSGI 和 djang ...