Covered Walkway


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≤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
 
题意:
  给你n个点和一个C,每个点有点权,让你用连续段覆盖着n个点, 每段的花费是 (x-y)^2 +c 此段覆盖x点到y点的权值差的平方 加上 常数C
  问你覆盖这n个点的最小花费
题解:
  dp[i] = min{dp[j] + C + (a[j+1]-a[i])^2};
  n^2显然超时
  这里需要用公式推,就不证明了
  前面几道斜率DP写的很清楚
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e6+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; ll n,c,a[N];
ll dp[N];
ll cal(int j,int k) {
return (dp[j] - dp[k] - a[k+]*a[k+] + a[j+] * a[j+]);
}
int main()
{
while(~scanf("%I64d%I64d",&n,&c)) {
if(n==&&c==) break;
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
deque< int > q;
dp[] = ;
q.push_back();
for(int i=;i<=n;i++) {
int now = q.front();q.pop_front();
while(!q.empty()&&cal(q.front() , now) <= 2ll * (a[q.front()+] - a[now+]) * a[i]) now = q.front(),q.pop_front();
q.push_front(now);
dp[i] = dp[now] + c + (a[i]-a[now+])*1ll*(a[i]-a[now+]);
now = q.back();q.pop_back();
while(!q.empty()&&cal(now , q.back()) * 2ll * (a[i+] - a[now+]) > 2ll * (a[now+] - a[q.back()+]) * cal(i,now) ) now = q.back(),q.pop_back();
q.push_back(now);
q.push_back(i);
}
printf("%I64d\n",dp[n]);
}
}
 

HDU 4258 Covered Walkway 斜率优化DP的更多相关文章

  1. hdu 4258 Covered Walkway

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

  2. hdu 3507 Print Article(斜率优化DP)

    题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...

  3. HDU 2829 Lawrence(斜率优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

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

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

  5. hdu 3045 Picnic Cows(斜率优化DP)

    题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...

  6. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  7. hdu 3507 Print Article —— 斜率优化DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m ...

  8. HDU 3507 单调队列 斜率优化

    斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...

  9. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

随机推荐

  1. MySQL各版本的区别(转)

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...

  2. ARP欺骗病毒,网页“篡改”,注入iframe代码!

    ---------------权威资料看这里--------------- 清华大学信息网络工程研究中心-中国教育和科研计算机网应急响应组<ARP 欺骗网页劫持攻击分析>PDF文件,直接I ...

  3. 通过开源程序同时解决DNS劫持和DNS污染的问题

    我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...

  4. Swift翻译之-关于Swift

    IMPORTANT 重要的 This is a preliminary document for an API or technology in development. Apple is suppl ...

  5. wordpress编辑主题时报错Warning: scandir() has been disabled for security reasons in

    在ubuntu下面安装了一个wordpress程序,在后台什么都没干,编辑主题时,发现页面中报下面的错误. notice: /home/wwwroot/test.localhost/wordpress ...

  6. 昂贵的聘礼(dijkstra)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38549   Accepted: 11158 Descripti ...

  7. Centos7上使用官方YUM源安装Mysql

    1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. 安装mysql-co ...

  8. Unity运行时刻资源管理

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/03/2998157.html Unity运行时刻资源管理 ------------------ ...

  9. Maven使用常见问题整理

    Maven使用常见问题整理  1.更新eclipse的classpath加入新依赖  1.在dependencyManagement里面加入包括版本在内的依赖信息,如:   <dependenc ...

  10. 如何用ndk-stack察看android崩溃堆栈

    前提:要打开eclipse的LogCat窗口 1.保存log,先要选中eclipse的LogCat的所有行,点击保存,假设保存到了/User/mac/Desktop/log.txt 2.找到你的so( ...