HDU 4258 Covered Walkway

http://acm.hdu.edu.cn/showproblem.php?pid=4258

题意:一个N个点的序列,要将他们全部覆盖,求总最少费用;费用计算: c+(x-y)2

Idea:

 f[i] = min(f[j] + (a[i]-a[j+1]) ^ 2+c) (N^2)

公式变形+数形结合:f[i] = min{f[j] + a[j+1]^2 - 2*a[i]*a[j+1] + a[i]^2 +C}

令x = a[j+1],y = f[j] + a[j+1]^2;

f[i] = y - 2*a[i]*x + a[i]^2 + C;

问题转化为: 已知直线的斜率为a[i],求过前i-1个点直线与y轴的最小截距。

最后优化:经过一系列的推理,可以得到--用下凸曲线来维护检查集,可以在均摊O(1)的时间内,找到斜率连续变化时的最小值。

(画图慢慢观察下,可参考另外一题:http://blog.csdn.net/xdu_truth/article/details/7943237)


Covered Walkway

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1175    Accepted Submission(s): 451


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≤10 9), 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 10 9, 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
 

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#include<algorithm>
#define ll long long

ll a[1000100];
ll dp[1001000];
int q[1001000];
int n; ll c;
ll sqr(ll t) { return t*t ; }
ll f(int j,int k)
{
    return dp[j]-dp[k]+sqr(a[j+1])-sqr(a[k+1]);
}
ll g(int j,int k) { return a[j+1]-a[k+1]; }
int main()
{
    while(cin>>n>>c,n)
    {
        for(int i=1;i<=n;i++)  scanf("%I64d",a+i);
        int head=0,tail=-1;
        q[++tail]=0;
        dp[1]=0;  a[0]=0;
        for(int i=1;i<=n;i++)
        {
            while(head+1<=tail&&f(q[head],q[head+1])>2*a[i]*g(q[head],q[head+1])) head++;  ///q[head] is worse than q[head+1]
            int u=q[head];
            dp[i]=dp[u]+c+sqr(a[i]-a[u+1]);     
            while(head+1<=tail&&f(q[tail-1],q[tail])*g(q[tail],i)>f(q[tail],i)*g(q[tail-1],q[tail]))  tail--;
            q[++tail]=i;
        }
        printf("%I64d\n",dp[n]);
    }
}




你可能感兴趣的:(单调性优化DP,斜率优化DP)