Covered Walkway

Time Limit: 10000ms
Memory Limit: 131072KB

This problem will be judged on HDU. Original ID: 4258
64-bit integer IO format: %I64d      Java class name: Main

 
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≤109), 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 109, 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

 
解题:晚上再写一下思路,详细地写一下思路
 
 蛋疼的斜率优化之旅开始了啊!
dp[i] = dp[j] + (d[i]-d[j+1])^2
这是没有优化的,优化主要手段是单调队列!
 
假设 j < i && k < i  假设j比k更优。我们如何判断j是真的比k优呢。
 
dp[j] + (d[i]-d[j+1])2 + c <= dp[k] + (d[i]-d[k+1])2 + c
 
=>      dp[j] + d2[j+1] - 2*d[i]*d[j+1]   <=   dp[k] + d2[k+1] - 2*d[i]*d[k+1]
 
=>  dp[j] + d2[j+1] - (dp[k] + d2[k+1])  <=  2*d[i]*(d[j+1]-d[k+1])
 
=>  (dp[j] + d2[j+1] - (dp[k] + d2[k+1]))/(d[j+1]-d[k+1])  <=  2*d[i]
 
d[i]是已知的,j比k好,就要满足上面的条件。至少满足上面的条件。
 
如何求dp[i]?在单调队列首,判断第一个和第二个,如果第二个比第一个好,第一个出队,一直这么继续。直到队首是最优的选择。
 
如何维护队尾呢?在本题中,斜率是越少越好!只需要比较(队尾第一个与队尾第二个的斜率)和(i 与队尾元素的斜率),如果i与队尾元素的斜率更少,那么i比队尾更优。队尾出,这样继续,知道队里的比i优,这时插入i.
 
斜率越小越好?因为一直要保持上面的那个<= d[i]的关系!越小,越能保持后面的关系。
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
LL n,c,d[maxn],dp[maxn];
int q[maxn],head,tail;
LL G(int k,int j){
return dp[j] + d[j+]*d[j+] - (dp[k]+d[k+]*d[k+]);
}
LL S(int k,int j){
return (d[j+]-d[k+]);
}
int main(){
int i,j;
while(scanf("%I64d %I64d",&n,&c),(n||c)){
for(i = ; i <= n; i++)
scanf("%I64d",d+i);
dp[] = q[] = ;
head = tail = ;
for(i = ; i <= n; i++){
while(head < tail && G(q[head],q[head+]) <= *d[i]*S(q[head],q[head+])) head++;
dp[i] = dp[q[head]] + (d[i]-d[q[head]+])*(d[i]-d[q[head]+])+c;
while(head < tail && G(q[tail-],q[tail])*S(q[tail],i) >= G(q[tail],i)*S(q[tail-],q[tail])) tail--;
q[++tail] = i;
}
printf("%I64d\n",dp[n]);
}
return ;
}
 

BNUOJ 26224 Covered Walkway的更多相关文章

  1. HDU 4258 Covered Walkway 斜率优化DP

    Covered Walkway Problem Description   Your university wants to build a new walkway, and they want at ...

  2. hdu 4258 Covered Walkway

    题目大意: 一个N个点的序列,要将他们全部覆盖,求总最少费用:费用计算: c+(x-y)2 分析: 斜率优化DP 我们假设k<j<i.如果在j的时候决策要比在k的时候决策好,那么也是就是d ...

  3. HDU 4258(Covered Walkway-斜率优化)

    Covered Walkway Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  4. ACM - 动态规划专题 题目整理

    CodeForces 429B  Working out 预处理出从四个顶点到某个位置的最大权值,再枚举相遇点,相遇的时候只有两种情况,取最优解即可. #include<iostream> ...

  5. HDU 4258 斜率优化dp

    Covered Walkway Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. Walkway.js – 用线条制作简约的 SVG 动画

    Walkway.js 是一个使用线条和路径元素组成 SVG 动画图像的简单方法.只需根据提供的配置对象创建一个新的 Walkway 实例就可以了.这种效果特别适合那些崇尚简约设计风格的网页.目前, W ...

  7. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  8. 备忘:maven 错误信息: Plugin execution not covered by lifecycle configuration

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. 【技巧】“Plugin execution not covered by lifecycle configuration...“异常的处理

    问题现象: 在Eclipse(JEE mars)中新建maven project,选择archetype为:maven-archetype-plugin,结果生成的project存在错误:“Plugi ...

随机推荐

  1. bzoj 1592: [Usaco2008 Feb]Making the Grade 路面修整【dp】

    因为是单调不降或单调不升,所以所有的bi如果都是ai中出现过的一定不会变差 以递增为例,设f[i][j]为第j段选第i大的高度,预处理出s[i][j]表示选第i大的时,前j个 a与第i大的值的差的绝对 ...

  2. P3573 [POI2014]RAJ-Rally

    传送门 很妙的思路 首先这是一个DAG,于是我们先在原图和反图上各做一遍,分别求出\(diss_i\)和\(dist_i\)表示从\(i\)点出发的最短路和以\(i\)为终点的最短路 我们考虑把点分为 ...

  3. Django day 38 结算中心,支付中心,计算价格方法

    一:结算中心 二:支付中心 三:计算价格方法

  4. Django day35 redis连接池,redis-list操作,django中使用redis,支付宝支付

    一:redis连接池, 二:redis-list操作, 三:django中使用redis, 四:支付宝支付

  5. Tuple类型的使用

    1.什么是Tuple Tuple类型,可以存放任何类型 2.Tuple有哪些分类 .Net 4.0 定义了8个泛型Tuple类,和一个Tuple静态类 3.Tuple的使用

  6. 42使用NanoPiM1Plus在Android4.4.2下的录音测试

    42使用NanoPiM1Plus在Android4.4.2下的录音测试 大文实验室/大文哥壹捌陆捌零陆捌捌陆捌贰21504965 AT qq.com完成时间:2017/12/5 17:51版本:V1. ...

  7. phpcms标签用法(转)

    1.显示指定catid的栏目名称和链接 {$CATEGORYS[25]['catname']}  {$CATEGORYS[25]['url']} 获取父栏目id/获取父栏目名称  $CATEGORY[ ...

  8. java 物理分页和逻辑分页

    A.逻辑分页利用游标分页,好处是所有数据库都统一,坏处就是效率低.1.逻辑分页的第一种方式,利用ResultSet的滚动分页.这种分页方式依靠的是对结果集的算法来分页,因此通常被称为“逻辑分页”.步骤 ...

  9. karma+requirejs+angular 测试

    http://karma-runner.github.io/0.8/plus/RequireJS.html karma 不是测试框架,只是一个运行测试框架的服务器 karma测试的原理是,将所有的文件 ...

  10. Context Switches msdn

    Context Switches  https://msdn.microsoft.com/en-us/library/ms682105(VS.85).aspx The scheduler mainta ...