Covered Gateway
Time Limit: 30000/10000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Problem descriptionyour university wants to build a new way, 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 specified way are covered or not.
The building contractor has an interesting pricing scheme. To cover the procedure from a point
XTo a point
Y, They will charge
C+ (
X-
Y) 2, where
CIs a constant. Note that it is possible
X = y. If so, then the contractor wowould simply charge
C.
Given the points along the same way and the constant
C, What is the minimum cost to cover the entire way?
Inputthere 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
NIs the number of points which must be covered, and
CIs the contractor's constant. Each of the following
NLines will contain a single integer, representing a point along the same way 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 0 s.
Outputfor 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 input10 5000123456710112456078999010190 0
Sample output30726
Source the University of Chicago Invitational Programming Contest 2012
Recommendliuyiding is the same as the DP slope optimization of a fruit as in the previous question .. We still need to emphasize the problem of inequality...
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 slope way (hdustm8, DP slope optimization)