思路:dp[i][x]=dp[j][x-1]+val[i]-val[j]-sum[j]*sum[i]+sum[j]*sum[j];
其中val[i]表示1~~i是一段的权值。
然后就是普通斜率dp做法。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstring>
#define Maxn 1010
#define LL __int64
using namespace std;
LL dp[Maxn][Maxn],num[Maxn],sum[Maxn],val[Maxn];
int que[Maxn*];
LL getleft(int x,int j,int k)
{
return dp[j][x]-val[j]+sum[j]*sum[j]-(dp[k][x]-val[k]+sum[k]*sum[k]);
}
LL getright(int j,int k)
{
return sum[j]-sum[k];
}
int main()
{
int n,m,i,j,head,rear;
while(scanf("%d%d",&n,&m)!=EOF,n||m){
memset(dp,,sizeof(dp));
for(i=;i<=n;i++){
scanf("%I64d",num+i);
sum[i]=sum[i-]+num[i];
}
for(i=;i<=n;i++){
val[i]=val[i-]+num[i]*sum[i-];
dp[i][]=val[i];
}
for(j=;j<m;j++){
head=,rear=;
que[++rear]=j;
for(i=j+;i<=n;i++){
while(head<rear&&getleft(j,que[head+],que[head])<sum[i]*getright(que[head+],que[head]))
head++;
dp[i][j+]=dp[que[head]][j]+val[i]-val[que[head]]-sum[que[head]]*sum[i]+sum[que[head]]*sum[que[head]];
while(head<rear&&getleft(j,i,que[rear])*getright(que[rear],que[rear-])<=getleft(j,que[rear],que[rear-])*getright(i,que[rear]))
rear--;
que[++rear]=i;
}
}
printf("%I64d\n",dp[n][m]);
}
return ;
}