标签:des style blog http color os io java strong
Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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斜率优化)
标签:des style blog http color os io java strong
原文地址:http://www.cnblogs.com/kjerome/p/3952191.html