hdu 2829 Lawrence(四边形不等式优化dp)
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.
#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)的更多相关文章
- 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这一段的价值. # ...
- [HDU2829] Lawrence [四边形不等式优化dp]
题面: 传送门 思路: 依然是一道很明显的区间dp 我们设$dp\left[i\right]\left[j\right]$表示前$j$个节点分成了$i$块的最小花费,$w\left[i\right]\ ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- 【无聊放个模板系列】HDU 3506 (四边形不等式优化DP-经典石子合并问题[环形])
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...
- BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】
题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...
- codevs3002石子归并3(四边形不等式优化dp)
3002 石子归并 3 参考 http://it.dgzx.net/drkt/oszt/zltk/yxlw/dongtai3.htm 时间限制: 1 s 空间限制: 256000 KB 题目等级 ...
- CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性
LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
随机推荐
- Ubuntu 18.04 安装博通(Broadcom)无线网卡驱动
目录 Ubuntu 18.04 安装博通(Broadcom)无线网卡驱动 Package gcc is not configured yet. 解决办法 history history | grep ...
- logback日志配置
第一步:加入jar包.要加入slf4j和logback的jar包,slf4j需要的jar包为slf4j-api,logback需要2个jar包(logback-classic.logback-core ...
- Canadian-dollar_RMB
import pandas as pd import matplotlib.pyplot as plt import statsmodels as sm from statsmodels.graphi ...
- 局部敏感哈希(LSH)之simhash和minhash
minhash 1. 把文档A分词形成分词向量L 2. 使用K个hash函数,然后每个hash将L里面的分词分别进行hash,然后得到K个被hash过的集合 3. 分别得到K个集合中的最小hash,然 ...
- Sublime3如何用快捷键实现字母的大小写转换
说明 有的时候需要将字母大小写一键转换一下,很显然,通过编辑器来实现会更加轻量级,而不是打开IDE去实现 我用的Sublime3版本如下: 步骤 1.打开Sublime的Key Bindings 2. ...
- SQLServer之创建辅助XML索引
创建辅助XML索引 使用 CREATE INDEX (Transact-SQL)Transact-SQL DDL 语句可创建辅助 XML 索引并且可指定所需的辅助 XML 索引的类型. 创建辅助 XM ...
- MySql 学习之路-基础
Mysql 自学之路 本文包含基础部分与高级部分 一.基础 数据库操作 Show databases:显示所有的数据库 Show tables: 显示所有的数据库表 Use databasename: ...
- 调用远程主机上的 RMI 服务时抛出 java.rmi.ConnectException: Connection refused to host: 127.0.0.1 异常原因及解决方案
最近使用 jmx 遇到一个问题,client/server 同在一台机器上,jmx client能够成功连接 server,如果把 server 移植到另一台机器上192.168.134.128,抛出 ...
- Django 【orm】或
方式一: q=Q() q.connection="or" q.children.append(("pk",1)) q.children.append((&quo ...
- KERBEROS PROTOCOL TUTORIAL
KERBEROS PROTOCOL TUTORIAL This tutorial was written by Fulvio Ricciardi and is reprinted here wit ...