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. SVN报错 Not Found In Revision 不支持空目录

    如果你要初始化上传的SVN目录为空,有可能会报这个错误 解决方法:在SVN下新建一个目录即可

  2. poj Find a multiple【鸽巢原理】

    参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...

  3. 392 Is Subsequence 判断子序列

    给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...

  4. [ SPOJ RESTACK ] Restacking haybales

    \(\\\) Description 给出一个环,每个位置有一个初值 \(A_i\),有一个目标值 \(B_i\),保证 \(\sum A_i=\sum B_i\) 每个位置只能把值分给隔壁的,每次分 ...

  5. 常见Z纯CSS小样式合集(三角形)

    三角形 .sanjiao{ width:0px; height: 0px; overflow: hidden; border-width: 100px; border-color: transpare ...

  6. Android通过微信实现第三方登录并使用OKHttp获得Token及源码下载

    这里对于App在微信开放平台上申请AppID和secret在这里就略过了,我们微信的授权登录流程,腾讯官网给的流程如下: 1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用 ...

  7. table的数据行tr上下移动

    昨天帮别人解决一个前端页面表格里的数据行上下移动的前端效果,直奔google找了几个demo,发现demo是实现了效果,但是代码很多,最后还是决定自己用jquery写个吧, 首先将前端效果分析出编程逻 ...

  8. iOS从手机相册选择一张照片并显示 Objective-C

    要先给app设置访问相册的权限: 在项目的Info.plist文件里添加Privacy - Photo Library Usage Description权限 ViewController.h: #i ...

  9. java生成excel

    package test.poi; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; ...

  10. 分析器错误消息: 该配置节不能包含 CDATA 或文本元素。

    原因当然是web.config配置文件中,有字符串文本了,估计不小心加上的一些字符,所以会报错,去掉就行,例如13行的s