蒟蒻不会四边形不等式
形似
fi,j=min{fi−1,k+wk,j}
的方程 且满足四边形不等式和区间包含单调性 就可以用四边形不等式优化
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=305;
const int M=35;
int n,m;
int f[M][N],s[M][N];
int a[N],w[N][N];
int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
w[i][j]=w[i][j-1]+a[j]-a[(i+j)>>1];
memset(f,0x3f,sizeof(f));
for (int i=1;i<=n;i++)
f[1][i]=w[1][i],s[1][i]=0;
for (int i=2;i<=m;i++){
s[i][n+1]=n;
for (int j=n;j>i;j--)
for (int k=s[i-1][j];k<=s[i][j+1];k++)
if(f[i-1][k]+w[k+1][j]<f[i][j])
s[i][j]=k,f[i][j]=f[i-1][k]+w[k+1][j];
}
printf("%d\n",f[m][n]);
return 0;
}