POJ3612:Telephone Wire
一道很棒的DP题目。
裸的DP方程很好搞:
$f[i][j]=min \{ f[i-1][k]+ C \times |k-j| +(k-a[i])^2 \}$
这个复杂度显然无法承受,考虑优化。
这个东西肯定不满足单调性和斜率上的优化条件,所以考虑分解这个式子。
$f[i][j]=min \{ f[i-1][k]+ C \times |j-k| +(k-a[i])^2 \}$
$f[i][j]=\begin{equation} \begin{cases} (j-a[i])^2+j \times C+min \{ f[i-1][k]-k\times C \} (k \leq j) \\ (j-a[i])^2-j \times C+min \{ f[i-1][k]+k\times C\} (j < k) \end{cases} \end{equation} $
然后把右边那两部分预处理一下就行了
//POJ 3612
//by Cydiater
//2016.10.8
#include <iostream>
#include <cstdlib>
#include <queue>
#include <map>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
inline int read(){
char ch=getchar();int x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int N,C,height[MAXN],high[MAXN],f[MAXN],low[MAXN],ans=oo;
namespace solution{
void init(){
N=read();C=read();
up(i,1,N)height[i]=read();
}
void slove(){
memset(high,10,sizeof(high));
memset(low,10,sizeof(low));
up(i,1,100)f[i]=height[1]<=i?(i-height[1])*(i-height[1]):oo;
up(i,2,N){
int tmp=oo;
down(j,100,1)low[j]=tmp=min(tmp,f[j]+j*C);tmp=oo;
up(j,1,100)high[j]=tmp=min(tmp,f[j]-j*C);
up(j,1,100)f[j]=j<height[i]?oo:((j-height[i])*(j-height[i])+min(low[j]-j*C,high[j]+j*C));
}
up(i,1,100)ans=min(ans,f[i]);
}
void output(){
cout<<ans<<endl;
}
}
int main(){
//freopen("input.in","r",stdin);
using namespace solution;
init();
slove();
output();
return 0;
}
POJ3612:Telephone Wire的更多相关文章
- [POJ3612] Telephone Wire(暴力dp+剪枝)
[POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...
- [USACO07NOV]电话线Telephone Wire
[USACO07NOV]电话线Telephone Wire 时间限制: 1 Sec 内存限制: 128 MB 题目描述 电信公司要更换某个城市的网线.新网线架设在原有的 N(2 <= N &l ...
- 【USACO07NOV】电话线Telephone Wire
题目描述 电信公司要更换某个城市的网线.新网线架设在原有的 N(2 <= N <= 100,000)根电线杆上, 第 i 根电线杆的高度为 height_i 米(1 <= heigh ...
- BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP
BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是 ...
- P2885 [USACO07NOV]电话线Telephone Wire
P2885 [USACO07NOV]电话线Telephone Wire 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话 ...
- 【动态规划】bzoj1705: [Usaco2007 Nov]Telephone Wire 架设电话线
可能是一类dp的通用优化 Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设 ...
- bzoj1705[Usaco2007 Nov]Telephone Wire 架设电话线(dp优化)
1705: [Usaco2007 Nov]Telephone Wire 架设电话线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 441 Solved: ...
- DP+滚动数组 || [Usaco2007 Nov]Telephone Wire 架设电话线 || BZOJ 1705 || Luogu P2885
本来是懒得写题解的…想想还是要勤发题解和学习笔记…然后就滚过来写题解了. 题面:[USACO07NOV]电话线Telephone Wire 题解: F[ i ][ j ] 表示前 i 根电线杆,第 i ...
- [USACO 07NOV]电话线Telephone Wire
题目描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to rep ...
随机推荐
- 226 Invert Binary Tree
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = ...
- 东大OJ-1391-Big big Power
题目描述 Calculate the power num a^(b^c) mod 1e9+7 输入 Multiple test cases,each case has three integers a ...
- PLSQL Developer不支持Oracle 64位客户端解决方法
问题描述: 在虚拟机同网段,搭建Oracle 11.2.04数据库64位的,本机操作系统Win10 x64和PLSQL 9.03,目前想利用PLSQL远程登录ORACLE数据库操作.当初用 insta ...
- 文本溢出省略解决笔记css
text-overflow:ellipsis; overflow:hidden; white-space:nowrap; *white-space:nowrap;
- [转]fastjson
原文地址:http://www.cnblogs.com/zhenmingliu/archive/2011/12/29/2305775.html FastJSON是一个很好的java开源json工具类库 ...
- Extjs-Ext.Ajax.request设置超时
ExtJs的Ajax提交主要是:Ext.Ajax.request或form1.getForm().submit,超时时间默认是30秒. 很多时候,后台处理比较多,往往需要超出30秒的限制.此时,可以通 ...
- 扩展easyUI tab控件,添加加载遮罩效果
项目里要用HighChart显示图表,如果返回的数量量太多,生成图表是一个很耗时的过程.tab控件又没有显示遮罩的设置(至少本菜是没有找到), Google了一下,根据另一个兄台写的方法,拿来改造了一 ...
- navigate连接MySQL报错:navigate your password has expired to log in your must change it using a client that supports
如图: 终端进入mysql: 第一次show databases的的时候,密码过期了,然后重置密码为12345,再次就可以显示了 参考连接:http://www.jb51.net/article/79 ...
- Extract Fasta Sequences Sub Sets by position
cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...
- JAVA基本类型的转换
1.String转成Int 例1: String str = "123"; try { int a = Integer.parseInt(str); } catch (Number ...