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. P2622 关灯问题II(状压bfs)

    P2622 关灯问题II 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯——按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j] ...

  2. mysqladmin(MySQL管理工具)

    mysqladmin是一个执行管理操作的客户端程序.它可以用来检查服务器的配置和当前状态.创建和删除数据库等. 1.mysqladmin命令的语法: shell > mysqladmin [op ...

  3. TensorFlow---image recognition--classify_image运行、文件说明与错误(路径)解决

    tutorial系列mnist已经玩过了,这篇玩一下 classify_image,其实就是image label.模型已经训练好的了,直接下载下来在.pb文件中. 本机环境: Win10 + Pyt ...

  4. [转]MVC4项目中验证用户登录一个特性就搞定

    本文转自:http://www.mrhuo.com/Article/Details/470/A-Attribute-For-MVC4-Project-Used-To-Validate-User-Log ...

  5. 中国移动Lumia机强制升级Windows10手机开发者预览版的方法

    [最新消息4-9]微软已经确定将于PST太平洋标准时间周五上午十点也就是北京时间本周六(4-11)凌晨一点推送更新Windows10手机预览版 但是推送更新的机型不包括Lumia Icon 和930 ...

  6. CF822C Hacker, pack your bags!

    思路: 对于一个区间[l, r],只需枚举所有满足r' < l并且二者duration之和为x的区间[l', r'],寻找其中二者cost之和最小的即可.于是可以开一个数组a[],a[i]表示所 ...

  7. 60查找nanopim1plus的HDMI为720p输出的问题

    60查找nanopim1plus的HDMI为720p输出的问题 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本:V1. ...

  8. [转帖]关于flask-login中各种API使用实例

    原贴:http://www.cnblogs.com/alima/p/5796298.html 简介:简单的集成flask,WTForms,包括跨站请求伪造(CSRF),文件上传和验证码. 一.安装(I ...

  9. Activity随笔

    Activity的生命周期 1.正常情况下的生命周期 onCreate:  Activity正在被创建,生命周期中的第一个方法,常在此方法中做一些初始化工作,比如调用setContentView方法, ...

  10. C++(extern关键字的理解和作用深入)

    extern关键字的理解和作用深入 extern是一个关键字,它告诉编译器存在着一个变量或者一个函数,如果在当前编译语句的前面中没有找到相应的变量或者函数, 也会在当前文件的后面或者其它文件中定义 引 ...