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. sqlserver2000连接失败,不存在或拒绝访问

    一 看ping 服务器IP能否ping通. 这个实际上是看和远程sql server 2000服务器的物理连接是否存在.如果不行,请检查网络,查看配置,当然得确保远程sql server 2000服务 ...

  2. contesthunter 6201 走廊泼水节【克鲁斯卡尔+并查集】

    很有意思的题,所以还是截lyddalao的课件 #include<iostream> #include<cstdio> #include<algorithm> us ...

  3. c++ memset函数

    函数名称:memset 函数所需头文件:#include<cstring> 函数作用:内存赋值函数,用来给某一块内存空间进行赋值的. 函数结构:memset(变量,一个数字,一个数字)  ...

  4. ACM_输出格式

    输出格式 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某水比参加了XX杯,但是他太水,所以三等都木有,所以他决定出一道水题水一 ...

  5. ora-20000 unable to analyze

    ora-20000 unable to analyze 无法分析表 check: select * from wmsprdata.cmp3$88278表不存在. result:应该是系统自动任务2:0 ...

  6. 19 C#循环语句的跳过和中断 continue和break

    在C#的循环语句中,有的时候我们希望跳过其中某个循环,有时我们希望当某个条件满足时,直接终止整个循环.C#为我们提供了 continue;和break;语句. continue和break的用法一样, ...

  7. Git学习笔记(2)-创建仓库

    一.Git简介 1.Git是什么 Git是分布式版本控制系统 2.Git有什么特点 (1)Git是分布式的SCM,SVN是集中式的 (2)Git每个历史版本存储完整的文件,SVN存储文件差异 (3)G ...

  8. EasyUI tree 异步树与采用扁平化实现的同步树

    所谓好记性不如烂笔头,为了以防忘记,才写下这篇博客,废话不多.. 异步树: tips:   可以采用easyui里的原始数据格式,也可以采用扁平化的数据格式. 使用场景: 当菜单模块数量庞大或者无限极 ...

  9. Eclipse开发工具介绍

    Eclipse是一个基于Java的.开放源码的.可扩展的应用开发平台,它为编程人员提供了一流的Java集成开发环境(Integrated Development Environment,IDE).在E ...

  10. Typora——自定义设置

    Typora提供自定义设置,在偏好设置里面,有一个主题文件夹,如果对界面的样式进行设定,可以添加一个css文件,命名规范是 github.user.css,下面代码会对h1~h4进行自动序列化 bod ...