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]);
        }
    }
     
  • 相关阅读:
    Java学习 · 初识 多线程
    Java学习 · 初识 IO流
    Java学习 · 初识 容器和数据结构
    Java学习 · 初识 异常机制
    Java学习 · 初识 面向对象深入二
    纯golang爬虫实战-(六)-关于cookiejar的理解 (2020-02-14 13:50)
    纯golang爬虫实战-(五)-登录并带cookie访问
    golang使用gopacket包进行数据包捕获实践(未成功)
    纯go+sqlite3+html/template网站(类似MVC实现数据库CURD、上传下载文件)-最少依赖才是硬道理
    纯golang+sqlite3 CRUD
  • 原文地址:https://www.cnblogs.com/zxhl/p/5671954.html
Copyright © 2011-2022 走看看