【bzoj 1911】 [Apio2010]特别行动队(斜率优化dp)
1911: [Apio2010]特别行动队
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 3532 Solved: 1618
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4
-1 10 -20
2 2 3 4
Sample Output
9
HINT
Source
【题解】【斜率优化dp】
【按照题目,可得转移方程:f[i]=f[j]+a*
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll f[1000010],sum[1000010],n,a,b,c;
ll q[1000010],h,t;
ll get(ll i,ll j)
{
return f[i]-f[j]+a*(sum[i]*sum[i]-sum[j]*sum[j])-b*(sum[i]-sum[j]);
}
int main()
{
ll i,j;
scanf("%lld",&n);
scanf("%lld%lld%lld",&a,&b,&c);
for(i=1;i<=n;++i) scanf("%lld",&sum[i]);
sum[0]=f[0]=0;
for(i=1;i<=n;++i) sum[i]+=sum[i-1];
h=t=0;
for(i=1;i<=n;++i)
{
while(h<t&& get(q[h],q[h+1])<=2*a*sum[i]*(sum[q[h]]-sum[q[h+1]])) h++;
ll x=sum[i]-sum[q[h]];
f[i]=f[q[h]]+a*x*x+b*x+c;
while(h<t && get(q[t-1],q[t])*(sum[q[t]]-sum[i])<=get(q[t],i)*(sum[q[t-1]]-sum[q[t]])) t--;
q[++t]=i;
}
printf("%lld\n",f[n]);
return 0;
}
既然无能更改,又何必枉自寻烦忧