hdu4258Covered Walkway(斜率)

来源:互联网 发布:matlab2017a for mac 编辑:程序博客网 时间:2024/05/18 01:26

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

分析:
斜率优化dp
f[i]=min{f[j]+c+(a[i]-a[j+1])^2}
每个点只用被覆盖一次,在f[j]状态中j被覆盖过了,
所以下一次覆盖是从j+1~i

斜率优化DP
我们假设k < j < i
如果在j的时候决策要比在k的时候决策好,
那么也是就是f[j]+c+(a[i]-a[j+1])^2 < f[k]+c+(a[i]-a[k+1])^2
f[j]+(a[i]-a[j+1])^2 < f[k]+(a[i]-a[k+1])^2;
f[j]-2*a[i]*a[j+1]+a[j+1]^2 < f[k]-2*a[k+1]*a[i]+a[k+1]^2
两边移项一下,得到:
f[j]+a[j+1]^2-(f[k]+a[k+1]^2) < 2*a[i]*(a[j+1]-a[k+1])

(f[j]+a[j+1]^2-(f[k]+a[k+1]^2))/(2*(a[j+1]-a[k+1])) < a[i]

设y=f[]+a[]^2,x=2*a[]
那么不就是y[j]-y[k]/x[j]-x[k] < a[i]么?
左边是不是斜率的表示?

设g[j][k]=y[j]-y[k]/x[j]-x[k]

那么y[j]-y[k]/x[j]-x[k] < a[i]说明了什么呢?
我们前面假设j的决策比k的决策要好
那么g[j][k] < a[i]代表这j的决策比k的决策要更优

关键的来了:
现在从左到右,还是设k < j < i,
如果g[i][j] < g[j][k],那么j点便永远不可能成为最优解,可以直接将它踢出我们的最优解集

解释一下
我们假设g[i][j] < a[i],那么就是说i点要比j点优,排除j点

如果g[i][j]>=a[i],那么j点此时是比i点要更优,
但是同时g[j][k]>g[i][j]>a[i]
这说明还有k点会比j点更优,同样排除j点

接下来看看如何找最优解

设k < j < i

由于我们排除了g[i][j] < g[j][k]的情况,
所以整个有效点集呈现一种上凸性质,即k <=> j的斜率要大于j <=> i的斜率。

这样,从左到右,斜率之间就是单调递减的了
当我们的最优解取得在j点的时候,那么k点不可能再取得比j点更优的解了,于是k点也可以排除
换句话说,j点之前的点全部不可能再比j点更优了,可以全部从解集中排除

于是对于这题我们对于斜率优化做法可以总结如下:
1.用一个单调队列来维护解集
2.假设队列中从头到尾已经有元素a,b,c
那么当d要入队的时候,我们维护队列的上凸性质,
即如果g[d][c] < g[c][b],那么就将c点删除
直到找到g[d][x]>=g[x][y]为止,并将d点加入在该位置中
3.求解时候,从队头开始,如果已有元素a,b,c,
当i点要求解时,如果g[b][a] < a[i],那么说明b点比a点更优,a点可以排除,于是a出队
直到g[x][y]>=a[i]

tip:

凡是手写队列之类的,随时都要判断一下tou

这里写代码片#include<cstdio>#include<iostream>#include<cstring>#define ll long longusing namespace std;const int N=1000005;ll f[N],a[N];int n,q[N],tou,wei;ll c;double xl(int x,int y)  //斜率 {  //一开始纠结了很久计算斜率时x,y的顺序,后来才明白顺序无所谓,计算出来的结果都一样     return (double)((f[x]+a[x+1]*a[x+1])-(f[y]+a[y+1]*a[y+1]))/(double)(2*a[x+1]-2*a[y+1]);}void doit(){    f[1]=c; f[0]=0;    tou=0;wei=1;    q[tou]=0;    q[wei]=1;    for (int i=2;i<=n;i++)    {        while (tou<wei&&xl(q[tou+1],q[tou])<=a[i])   //先维护f再添加             tou++;  //q[tou+1]比q[tou]更优         f[i]=f[q[tou]]+(a[i]-a[q[tou]+1])*(a[i]-a[q[tou]+1])+c;        while (tou<wei&&xl(q[wei],i)<=xl(q[wei],q[wei-1])) //g(i,j)<g(j,k)             wei--;        q[++wei]=i;    }    printf("%I64d\n",f[n]);}int main(){    scanf("%d%I64d",&n,&c);  //ljhdu支持I64d     while (n&&c)    {        for (int i=1;i<=n;i++) scanf("%I64d",&a[i]);        doit();        scanf("%d%I64d",&n,&c);    }    return 0;} 
原创粉丝点击