hdu 4258 Covered Walkway

Covered Walkway

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


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
 

简单一维斜率优化dp,参考:点击打开链接
思想类似,但是这题被一个地方坑了,WA了好多次,之前一维单调队列维护和dp更新顺序可换,即先入队再更新dp,但这题恰好不可以,因为dp更新要用到队列第二个元素,这个可能被入队元素覆盖,因此,最好入队写在dp更新之后。嗨,以前写代码太随意,因为以前确实没遇到过这种情况,误认为两者一样,今天终于吃了一次亏。谨记在心!

代码:
#include<cstdio>
#include<iostream>
#define ll long long
#define Maxn 1000010
using namespace std;

ll dp[Maxn],mat[Maxn];
int q[Maxn];
ll dx(int a,int b){
    return dp[b]+mat[b+1]*mat[b+1]-(dp[a]+mat[a+1]*mat[a+1]);
}
ll dy(int a,int b){
    return mat[b+1]-mat[a+1];
}
int main()
{
    int n,c;
    while(scanf("%d%d",&n,&c),n||c){
        for(int i=1;i<=n;i++)
            scanf("%I64d",mat+i);
        int s=0,e=0;
        dp[0]=q[0]=0;
        for(int i=1;i<=n;i++){
            while(s<e&&dx(q[s],q[s+1])<=2*mat[i]*dy(q[s],q[s+1])) s++;
            //while(s<e&&dx(q[e-1],q[e])*dy(q[e],i)>=dx(q[e],i)*dy(q[e-1],q[e])) e--;
            //q[++e]=i;  要写在dp更新之后
            dp[i]=dp[q[s]]+(mat[q[s]+1]-mat[i])*(mat[q[s]+1]-mat[i])+c;
            while(s<e&&dx(q[e-1],q[e])*dy(q[e],i)>=dx(q[e],i)*dy(q[e-1],q[e])) e--;
            q[++e]=i;
        }
        printf("%I64d\n",dp[n]);
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值