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]);
        }
    }
     
  • 相关阅读:
    Direct3D 11 Tutorial 4: 3D Spaces_Direct3D 11 教程4:3D空间
    Direct3D 11 Tutorial 3: Shaders and Effect System_Direct3D 11 教程3:着色器和效果系统
    Direct3D 11 Tutorial 2: Rendering a Triangle_Direct3D 11 教程2:渲染一个三角形
    Direct3D 11 Tutorial 1: Basics_Direct3D 11 教程1:基础
    使用git命令push到自己的仓库,显示Unknown且没有贡献记录的解决方案
    数据结构与算法笔试面试题整理
    网络笔试面试题整理
    C++笔试面试题整理
    使用.gitignore删除Github上的.idea文件
    做游戏服务器端开发时的一些收获与总结
  • 原文地址:https://www.cnblogs.com/zxhl/p/5671954.html
Copyright © 2011-2022 走看看