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]);
}
}
 

最新文章

  1. IScroll5兼容IE修改
  2. JavaWeb用Jdbc操作MySql数据库(二)
  3. su和su - 的区别
  4. Android 内存溢出解决方案(OOM) 整理总结
  5. [HDOJ4325]Flowers(树状数组 离散化)
  6. Castle IOC容器内幕故事(下)
  7. HBase0.98.1 通过协调器进行表的行数统计
  8. 2016 Vultr VPS最新优惠码,赠送新用户70美元,亲测有效
  9. Python进行JSON格式化输出,以及汉字显示问题
  10. HDFS-put: unexpected URISyntaxException
  11. 嵌入式linux教程
  12. 简易图书管理系统(主要是jsp的练习)
  13. gzip1
  14. GitLab篇之Linux下环境搭建
  15. C#零基础入门08:代码规范
  16. gzexe加密 脚本
  17. 谈谈GPU与FPGA的一些看法
  18. Java温故而知新(4)类String字符串
  19. LeetCode:11. ContainerWithWater(Medium)
  20. poj2689素数问题

热门文章

  1. Oracle 10g 和11g r2 下载地址(使用迅雷)
  2. TortoiseGit-创建分支、合并分支
  3. 转 Citrix XenCenter安装VM之挂载ISO详解
  4. java笔记--关于int和byte[]的转换
  5. Linux瑞士军刀:密码管理Keeweb
  6. 获取oracle 表字段,表名,以及主键之类等等的信息。
  7. python string与list互转
  8. 18.用两个栈实现队列[2StacksToImplementQueue]
  9. 利用FFmpeg生成视频缩略图 2.1.6
  10. HDU2050离散数学折线分割平面