Covered Walkway(HDU4258,dp斜率优化)

Covered Walkway

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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

Source

The University of Chicago Invitational Programming Contest 2012

Recommend

liuyiding

与上一题一样是个果果的dp斜率优化。。还是要强调不等号的问题...

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N = 1000010;
 7 #define For(i,n) for(int i=1;i<=n;i++)
 8 #define Rep(i,l,r) for(int i=l;i<=r;i++)
 9 long long dp[N],n,C,num[N],q[N],l,r;
10
11 long long sqr(long long a){return (a*a);}
12
13 long long up(int j,int i){
14     return (dp[j]+sqr(num[j+1])-(dp[i]+sqr(num[i+1])));
15 }
16
17 long long down(int j,int i){
18     return (num[j+1]-num[i+1]);
19 }
20
21 void DP(){
22     int l = 0 ,r = 1;
23     For(i,n){
24         while(l+1<r && up(q[l],q[l+1]) >= 2*num[i]*down(q[l],q[l+1])) l++;
25         dp[i]=dp[q[l]] + sqr(num[i]-num[q[l]+1]) + C;
26         printf("%I64d\n",dp[i]);
27         while(l+1<r && up(q[r-2],q[r-1])*down(q[r-1],i) >= up(q[r-1],i)*down(q[r-2],q[r-1])) r--;
28         q[r++]=i;
29     }
30     printf("%I64d\n",dp[n]);
31 }
32
33 void read(long long &v){
34     char ch = getchar();long long num=0;
35     while(ch>‘9‘||ch<‘0‘) ch=getchar();
36     while(ch>=‘0‘&&ch<=‘9‘){
37         num = num*10 + ch-‘0‘;
38         ch=getchar();
39     }
40     v = num;
41 }
42
43 int main(){
44     while(read(n),read(C),n+C){
45         For(i,n) read(num[i]);
46         DP();
47     }
48     return 0;
49 }
时间: 2024-07-30 20:31:27

Covered Walkway(HDU4258,dp斜率优化)的相关文章

学渣乱搞系列之dp斜率优化

学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很大部分参考了大神Accept的文章,不过此神貌似早已绝迹江湖,这篇文章写得好,也写得很差,前半部分叙述得很好,可是关键,关键部分说得很乱,有些许错误,很多大神都进行了评论指出,但是大神Accept貌似没有修改的意思,故重新总结下,以便自己以后查阅和复习啊. 下面看一个例题Print Article.

BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)

[ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场暴雨,于是L先生决定紧急在某些工厂建立一些仓库以免产品被淋坏.由于地形的不同,在不同工厂建立仓库的费用可能是不同的.第i个工厂目前已有成品Pi件,在第i个工厂位置建立仓库的费用是Ci.对于没有建立仓库的工厂,其

【BZOJ-4518】征途 DP + 斜率优化

4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 230  Solved: 156[Submit][Status][Discuss] Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路必须在同一天中走完. Pine希望每一天走的路长度尽可能相近,所以他

【BZOJ-1010】玩具装箱toy DP + 斜率优化

1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][Status][Discuss] Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P

DP 斜率优化题目/决策单调性题目

斜率优化 冬令营讲到了dp斜率优化后瞎写了一些斜率优化,因为毕竟上次写是老早以前了,当时对这个并不是很理解,现在有一点数学基础,稍微好一点了. 瞎找的斜率优化题,可能有点太水了,我这种这么菜的人竟然都切得那么快.可能难度不够,因为有个y坐标不单调要套个平衡树的我是真不会,这个嘛以后再说了. 1.hdu3507 Print Article 给定非负数列,划成若干块,每块价值是左边那玩意儿.求总权值min. 这个自己写写就可以搞出来了吧,现在看真心不难,除非数值有负的..那个要平衡树,现在写不动.

BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][Status][Discuss] Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P

hdu 3507(DP+斜率优化)

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 6653    Accepted Submission(s): 2054 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

BZOJ3437:小P的牧场(DP+斜率优化)

小P的牧场[题目描述]背景:小P 是个特么喜欢玩MC 的孩纸...小P 在MC 里有n 个牧场,自西向东呈一字形排列(自西向东用1…n 编号),于是他就烦恼了:为了控制这n 个牧场,他需要在某些牧场上面建立控制站,每个牧场上只能建立一个控制站,每个控制站控制的牧场是它所在的牧场一直到它西边第一个控制站的所有牧场(它西边第一个控制站所在的牧场不被控制)(如果它西边不存在控制站,那么它控制西边所有的牧场),每个牧场被控制都需要一定的花费(毕竟在控制站到牧场间修建道路是需要资源的嘛~),而且该花费等于

BZOJ 1010: [HNOI2008]玩具装箱toy(DP+斜率优化)

[HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P教授要求在一个一维容器中的玩具编号是连续的.同时如果一个一维容器中有多个玩具,那么两件玩具之间要加入一个单位长度的填充物,形式地说如果将第i件玩具到第j个玩具放到一个容器中,那么容器的长度将为