#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100010
#define K 210
using namespace std;
typedef unsigned long long ll;
ll sum[N],a[N],f[N][2],q[N];
ll n,k;
int tmp;
ll fy(int j1,int j2,int d)
{
return f[j1][d]-f[j2][d]+sum[j2]*sum[j2]-sum[j1]*sum[j1];
}
ll fx(int j1,int j2)
{
return sum[j2]-sum[j1];
}
int main()
{
scanf("%llu%llu",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%llu",&a[i]);
sum[i]=sum[i-1]+a[i];
}
tmp=0;
for(int j=1;j<=k;j++)
{
tmp^=1;
int head=0,tail=0;
q[head]=0;
for(int i=1;i<=n;i++)
{
while(head<tail&&fy(q[head],q[head+1],tmp^1)<=fx(q[head],q[head+1])*sum[i])head++;
while(head<tail&&fy(q[tail-1],q[tail],tmp^1)*fx(q[tail],i)>=fy(q[tail],i,tmp^1)*fx(q[tail-1],q[tail]))tail--;
int t=q[head];
f[i][tmp]=f[t][tmp^1]+sum[t]*(sum[i]-sum[t]);
q[++tail]=i;
}
}
printf("%llu\n",f[n][tmp]);
}
BZOJ 3675 APIO2014 序列分割 斜率优化DP
原文地址:http://blog.csdn.net/wzq_qwq/article/details/46408309