zoukankan      html  css  js  c++  java
  • HDU 4258 Covered Walkway 斜率优化DP

    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+20, M = 1e2+10, mod = 1e9+7, inf = 1e9+1000;
    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+1]*a[k+1] + a[j+1] * a[j+1]);
    }
    int main()
    {
        while(~scanf("%I64d%I64d",&n,&c)) {
            if(n==0&&c==0) break;
            for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
            deque< int > q;
            dp[0] = 0;
            q.push_back(0);
            for(int i=1;i<=n;i++) {
                int now = q.front();q.pop_front();
                while(!q.empty()&&cal(q.front() , now) <= 2ll * (a[q.front()+1] - a[now+1]) * a[i]) now = q.front(),q.pop_front();
                q.push_front(now);
                dp[i] = dp[now] + c + (a[i]-a[now+1])*1ll*(a[i]-a[now+1]);
                now = q.back();q.pop_back();
                while(!q.empty()&&cal(now , q.back()) * 2ll * (a[i+1] - a[now+1])  > 2ll * (a[now+1] - a[q.back()+1]) * cal(i,now) )  now = q.back(),q.pop_back();
                q.push_back(now);
                q.push_back(i);
            }
            printf("%I64d
    ",dp[n]);
        }
    }
     
  • 相关阅读:
    Python-属性描叙符协议ORM实现原理依据- __set__ __get__ __delete__
    Python-类属性查询协议-__getattr__ __getattribute__
    Python-__init__ 和 __new__区别和原理
    Python-在不在判断 in 和 in判断协议- in __contains__
    Python-求序列长度和序列长度协议-len() __len__
    Python-序列反转和序列反转协议-reversed __reversed__
    Python-序列切片原理和切片协议-[start:end:step] __getitem__
    Python-序列常用方法 + * += extend append方法区别
    Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict arrary
    Python-函数式编程-map reduce filter lambda 三元表达式 闭包
  • 原文地址:https://www.cnblogs.com/zxhl/p/5671954.html
Copyright © 2011-2022 走看看