标签:amp input tail lines can like work out blog
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12185 Accepted Submission(s): 3733
//和上一个题一样,dp[i]=min(dp[j]+m+(sum[i]-sum[j])^2) #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=500009; int n,m,que[maxn]; ll sum[maxn],dp[maxn]; ll getdp(int i,int j){ return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]); } ll getup(int j,int k){ return dp[j]-dp[k]+sum[j]*sum[j]-sum[k]*sum[k]; } ll getlow(int j,int k){ return 2*(sum[j]-sum[k]); } int main() { while(scanf("%d%d",&n,&m)==2){ sum[0]=0; for(int i=1;i<=n;i++){ scanf("%lld",&sum[i]); sum[i]+=sum[i-1]; } int head=0,tail=0; que[tail++]=0; for(int i=1;i<=n;i++){ while(head+1<tail&&getup(que[head+1],que[head])<sum[i]*getlow(que[head+1],que[head])) head++; dp[i]=getdp(i,que[head]); while(head+1<tail&&getup(que[tail-1],que[tail-2])*getlow(i,que[tail-1])>=getup(i,que[tail-1])*getlow(que[tail-1],que[tail-2])) tail--; que[tail++]=i; } printf("%lld\n",dp[n]); } return 0; }
标签:amp input tail lines can like work out blog
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6885520.html