zoukankan      html  css  js  c++  java
  • BNUOJ 26224 Covered Walkway

    Covered Walkway

    Time Limit: 10000ms
    Memory Limit: 131072KB
    This problem will be judged on HDU. Original ID: 4258
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

    Source

     
    解题:晚上再写一下思路,详细地写一下思路
     
     蛋疼的斜率优化之旅开始了啊!
    dp[i] = dp[j] + (d[i]-d[j+1])^2
    这是没有优化的,优化主要手段是单调队列!
     
    假设 j < i && k < i  假设j比k更优。我们如何判断j是真的比k优呢。
     
    dp[j] + (d[i]-d[j+1])2 + c <= dp[k] + (d[i]-d[k+1])2 + c
     
    =>      dp[j] + d2[j+1] - 2*d[i]*d[j+1]   <=   dp[k] + d2[k+1] - 2*d[i]*d[k+1]
     
    =>  dp[j] + d2[j+1] - (dp[k] + d2[k+1])  <=  2*d[i]*(d[j+1]-d[k+1])
     
    =>  (dp[j] + d2[j+1] - (dp[k] + d2[k+1]))/(d[j+1]-d[k+1])  <=  2*d[i]
     
    d[i]是已知的,j比k好,就要满足上面的条件。至少满足上面的条件。
     
    如何求dp[i]?在单调队列首,判断第一个和第二个,如果第二个比第一个好,第一个出队,一直这么继续。直到队首是最优的选择。
     
    如何维护队尾呢?在本题中,斜率是越少越好!只需要比较(队尾第一个与队尾第二个的斜率)和(i 与队尾元素的斜率),如果i与队尾元素的斜率更少,那么i比队尾更优。队尾出,这样继续,知道队里的比i优,这时插入i.
     
    斜率越小越好?因为一直要保持上面的那个<= d[i]的关系!越小,越能保持后面的关系。
     
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 using namespace std;
    16 const int maxn = 1000100;
    17 LL n,c,d[maxn],dp[maxn];
    18 int q[maxn],head,tail;
    19 LL G(int k,int j){
    20     return dp[j] + d[j+1]*d[j+1] - (dp[k]+d[k+1]*d[k+1]);
    21 }
    22 LL S(int k,int j){
    23     return (d[j+1]-d[k+1]);
    24 }
    25 int main(){
    26     int i,j;
    27     while(scanf("%I64d %I64d",&n,&c),(n||c)){
    28         for(i = 1; i <= n; i++)
    29             scanf("%I64d",d+i);
    30         dp[0] = q[0] = 0;
    31         head = tail = 0;
    32         for(i = 1; i <= n; i++){
    33             while(head < tail && G(q[head],q[head+1]) <= 2*d[i]*S(q[head],q[head+1])) head++;
    34             dp[i] = dp[q[head]] + (d[i]-d[q[head]+1])*(d[i]-d[q[head]+1])+c;
    35             while(head < tail && G(q[tail-1],q[tail])*S(q[tail],i) >= G(q[tail],i)*S(q[tail-1],q[tail])) tail--;
    36             q[++tail] = i;
    37         }
    38         printf("%I64d
    ",dp[n]);
    39     }
    40     return 0;
    41 }
    View Code
     
  • 相关阅读:
    初学node.js,安装nodemon,学习debug模式,安装cpu-stat
    当离开浏览器窗口,提示语title更改
    构建react项目失败解决办法
    vue 安装cli3.0版本,创建项目
    上传js,js修改html
    上传图片
    css3 伸缩百分比的调整
    css3 伸缩布局 display:flex等
    解决HTML5提出的新的元素不被IE6-8识别的解决办法
    web前端,多语言切换,data-localize,
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3914592.html
Copyright © 2011-2022 走看看