标签:fine main i++ ant queue logs 斜率优化dp hit 维护
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 11141 Accepted Submission(s): 3393
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <map> 10 #define ll __int64 11 using namespace std; 12 int dp[500005]; 13 int q[500005]; 14 int sum[500005]; 15 int head,tail,n,m; 16 int getDP(int i,int j) 17 { 18 return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]); 19 } 20 int getUP(int j,int k) 21 { 22 return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]); 23 } 24 int getdown(int j,int k) 25 { 26 return 2*(sum[j]-sum[k]); 27 } 28 int main() 29 { 30 while(scanf("%d %d",&n,&m)==2) 31 { 32 for(int i=1; i<=n; i++) 33 scanf("%d",&sum[i]); 34 sum[0]=dp[0]=0; 35 for(int i=1; i<=n; i++) 36 sum[i]+=sum[i-1]; 37 head=tail=0; 38 q[tail++]=0; 39 for(int i=1; i<=n; i++) 40 { 41 while(head+1<tail && getUP(q[head+1],q[head])<=sum[i]*getdown(q[head+1],q[head])) 42 head++; 43 dp[i]=getDP(i,q[head]); 44 while(head+1<tail && getUP(i,q[tail-1])*getdown(q[tail-1],q[tail-2])<=getUP(q[tail-1],q[tail-2])*getdown(i,q[tail-1])) 45 tail--; 46 q[tail++]=i; 47 } 48 printf("%d\n",dp[n]); 49 } 50 return 0; 51 }
标签:fine main i++ ant queue logs 斜率优化dp hit 维护
原文地址:http://www.cnblogs.com/hsd-/p/6413559.html