bzoj1911: [Apio2010]特别行动队 斜率优化dp
https://www.lydsy.com/JudgeOnline/images/1911_1.jpg
题解:斜率优化dp,斜率优化的状态都很明显,就是关于x的函数f(x)关于y的函数g(y),然后f(x)单调即可斜率优化,这题就是dp【i】表示选前i的最优方案;
转移方程:dp[i]=dp[j]+a(sum[j]-sum[i])^2+(sum[i]-sum[j])*b+c;
/**************************************************************
Problem: 1911
User: walfy
Language: C++
Result: Accepted
Time:1624 ms
Memory:24728 kb
****************************************************************/
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod (1000000007)
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const double g=10.0,eps=1e-12;
const int N=1000000+10,maxn=1200000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
ll q[N],sum[N],n,a,b,c,dp[N];
inline ll x(int i)
{
return sum[i];
}
inline ll y(int i)
{
return dp[i]+a*sum[i]*sum[i]-b*sum[i];
}
inline double slope(int i,int j)
{
return (y(j)-y(i))/(x(j)-x(i));
}
int main()
{
scanf("%lld%lld%lld%lld",&n,&a,&b,&c);
for(int i=1;i<=n;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-1];
}
int head=1,last=1;q[head]=0;
for(int i=1;i<=n;i++)
{
while(head<last&&slope(q[head],q[head+1])>2*a*sum[i])head++;
int j=q[head];
dp[i]=dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])*a+(sum[i]-sum[j])*b+c;
while(head<last&&slope(q[last-1],q[last])<slope(q[last],i))last--;
q[++last]=i;
// printf("%lld\n",dp[i]);
}
printf("%lld\n",dp[n]);
return 0;
}
/********************
********************/