博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4258 斜率优化dp
阅读量:4558 次
发布时间:2019-06-08

本文共 2898 字,大约阅读时间需要 9 分钟。

Covered Walkway

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1496    Accepted Submission(s): 602

Problem Description
Your university wants to build a new walkway, and they want at least part of it to be covered. There are certain points which must be covered. It doesn’t matter if other points along the walkway are covered or not. 
The building contractor has an interesting pricing scheme. To cover the walkway from a point at 
x to a point at 
y, they will charge 
c+(
x-
y)
2, where 
c is a constant. Note that it is possible for 
x=y. If so, then the contractor would simply charge 
c
Given the points along the walkway and the constant 
c, what is the minimum cost to cover the walkway?
 

 

Input
There will be several test cases in the input. Each test case will begin with a line with two integers, 
n (1≤
n≤1,000,000) and 
c (1≤
c≤10
9), where 
n is the number of points which must be covered, and 
c is the contractor’s constant. Each of the following 
n lines will contain a single integer, representing a point along the walkway that must be covered. The points will be in order, from smallest to largest. All of the points will be in the range from 1 to 10
9, inclusive. The input will end with a line with two 0s.
 

 

Output
For each test case, output a single integer, representing the minimum cost to cover all of the specified points. Output each integer on its own line, with no spaces, and do not print any blank lines between answers. All possible inputs yield answers which will fit in a signed 64-bit integer.
 

 

Sample Input
10 5000
1 23 45 67 101 124 560 789 990 1019
0 0
 

 

Sample Output
30726
 

 

Source
 

题意:

有n个点需要被覆盖,覆盖第j到第i之间的点的花费是c+(x[i]-x[j])^2,问把所有的点都覆盖的最小花费。

输入n,c

输入n个点x[1...n]

当输入0 0时结束

代码:

//有状态转移方程dp[i]=min(dp[j]+C+(a[i]-a[j+1])*(a[i]-a[j+1])),数据是1e6的两重循环必然不行//设k
(yj-yk)/2*(xj-xk)
g[i,j]那么j点永远不可能是i的最优解,因为://我们假设g[i,j]
=a[i],那么j点此时是比i点要更优,//但是同时g[j,k]>g[i,j]>sum[i]。这说明还有k点会比j点更优,同样排除j点。排除多余的点,这便是一种优化!//其实就是维护一个斜率递增的(下凸上凹)的图形。因此要把i点加入队列之前先判断是否能够维护斜率递增如果//不能就把队列最后一个元素删掉直到是斜率递增的然后加入i点。#include
#include
#include
using namespace std;typedef long long ll;const int maxn=1000009;int n,m,que[maxn];ll dp[maxn],a[maxn];ll getdp(int i,int j){ return dp[j]+m+(a[i]-a[j+1])*(a[i]-a[j+1]);}ll getup(int j,int k){ return dp[j]-dp[k]+a[j+1]*a[j+1]-a[k+1]*a[k+1];}ll getlow(int j,int k){ return 2*(a[j+1]-a[k+1]);}int main(){ while(scanf("%d%d",&n,&m)&&(n+m)){ for(int i=1;i<=n;i++) scanf("%lld",&a[i]); int head=0,tail=0; dp[0]=0; que[tail++]=0; for(int i=1;i<=n;i++){ while(head+1
<=a[i]*getlow(que[head+1],que[head])) head++; dp[i]=getdp(i,que[head]); while(head+1
=getup(i,que[tail-1])*getlow(que[tail-1],que[tail-2])) tail--; que[tail++]=i; } printf("%lld\n",dp[n]); } return 0;}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6885414.html

你可能感兴趣的文章
分隔指定内容,提取章节数
查看>>
this point
查看>>
验证登录信息是否合法
查看>>
线程池
查看>>
WIFI密码破解全攻略
查看>>
iOS开发之画图板(贝塞尔曲线)
查看>>
4嵌入式作业io
查看>>
IntelliJ Idea编译报错:javacTask: 源发行版 1.7 需要目标发行版 1.7
查看>>
Cognos中新建SQLserver数据源的步骤
查看>>
HttpClient连接超时及读取超时
查看>>
SQL优化方法
查看>>
SEO必须掌握的高级搜索指令
查看>>
生产者消费者模型
查看>>
ORACLE 字符串超长问题解决方案
查看>>
线程 题待做
查看>>
PL/SQL可以连oracle,但是jdbc连不上 【转】
查看>>
使用 highlight.js 在网页中高亮显示java 代码 【原】
查看>>
[转]高颜值、好用、易扩展的微信小程序 UI 库,Powered by 有赞
查看>>
[转]SQL Server如何启用xp_cmdshell组件
查看>>
[转]微擎应用笔记3--manifest.xml文件使用说明
查看>>