XVI.[HDU3507]Print Article
没什么好说的,这题比任务安排还水,随便推推完事。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,s[500100],f[500100],q[500100],l,r;
signed main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
for(int i=1;i<=n;i++){
scanf("%lld",&s[i]);
if(!s[i]){i--,n--;continue;}
s[i]+=s[i-1];
}
// for(int i=1;i<=n;i++)printf("%d ",s[i]);puts("");
l=r=0;
for(int i=1;i<=n;i++){
while(r-l&&f[q[l]]-f[q[l+1]]+s[q[l]]*s[q[l]]-s[q[l+1]]*s[q[l+1]]>=2*s[i]*(s[q[l]]-s[q[l+1]]))l++;
f[i]=f[q[l]]+m+(s[i]-s[q[l]])*(s[i]-s[q[l]]);
while(r-l&&(f[q[r-1]]-f[q[r]]+s[q[r-1]]*s[q[r-1]]-s[q[r]]*s[q[r]])*(s[q[r]]-s[i])>=(f[q[r]]-f[i]+s[q[r]]*s[q[r]]-s[i]*s[i])*(s[q[r-1]]-s[q[r]]))r--;
q[++r]=i;
}
printf("%lld\n",f[n]);
}
return 0;
}