首页 > 代码库 > Covered Walkway(HDU4258,dp斜率优化)

Covered Walkway(HDU4258,dp斜率优化)

Covered Walkway

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)


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 5000123456710112456078999010190 0
 

 

Sample Output
30726
 

 

Source
The University of Chicago Invitational Programming Contest 2012
 

 

Recommend
liuyiding
 
与上一题一样是个果果的dp斜率优化。。还是要强调不等号的问题...
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int N = 1000010; 7 #define For(i,n) for(int i=1;i<=n;i++) 8 #define Rep(i,l,r) for(int i=l;i<=r;i++) 9 long long dp[N],n,C,num[N],q[N],l,r;10 11 long long sqr(long long a){return (a*a);}12 13 long long up(int j,int i){14     return (dp[j]+sqr(num[j+1])-(dp[i]+sqr(num[i+1])));15 }16 17 long long down(int j,int i){18     return (num[j+1]-num[i+1]);19 }20 21 void DP(){22     int l = 0 ,r = 1;23     For(i,n){24         while(l+1<r && up(q[l],q[l+1]) >= 2*num[i]*down(q[l],q[l+1])) l++;25         dp[i]=dp[q[l]] + sqr(num[i]-num[q[l]+1]) + C;26         printf("%I64d\n",dp[i]); 27         while(l+1<r && up(q[r-2],q[r-1])*down(q[r-1],i) >= up(q[r-1],i)*down(q[r-2],q[r-1])) r--;28         q[r++]=i;29     }30     printf("%I64d\n",dp[n]);31 }32 33 void read(long long &v){34     char ch = getchar();long long num=0;35     while(ch>9||ch<0) ch=getchar();36     while(ch>=0&&ch<=9){37         num = num*10 + ch-0;38         ch=getchar();39     }40     v = num;41 }42 43 int main(){44     while(read(n),read(C),n+C){45         For(i,n) read(num[i]);46         DP();47     }48     return 0;49 }

 

Covered Walkway(HDU4258,dp斜率优化)