拿走一个区间的代价只与最大最小值有关,并且如果最后一次拿走包含区间右端点的子序列一定不会使答案更劣,于是设f[i][j][x][y]为使i~j区间剩余最小值为x最大值为y且若有数剩余一定包含j的最小代价,特别地f[i][j][0][0]表示取完i~j区间的最小代价。转移时考虑j最后和哪一段一起拿走,有f[i][j][min(x,a[j])][max(y,a[j])]=min{f[i][d-1][x][y]+f[d][j-1][0][0]},这样就能处理拿走一段后区间的合并了。
区间dp好难啊。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } #define N 55 int n,w,v,a[N],b[N],f[N][N][N][N]; int main() { #ifndef ONLINE_JUDGE freopen("bzoj4897.in","r",stdin); freopen("bzoj4897.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif n=read(),w=read(),v=read(); for (int i=1;i<=n;i++) b[i]=a[i]=read(); sort(b+1,b+n+1);int t=unique(b+1,b+n+1)-b-1; for (int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+t+1,a[i])-b; memset(f,42,sizeof(f)); for (int i=1;i<=n+1;i++) { f[i][i-1][0][0]=0; for (int x=1;x<=t;x++) for (int y=x;y<=t;y++) f[i][i-1][x][y]=0; } for (int k=1;k<=n;k++) for (int i=1;i<=n-k+1;i++) { int j=i+k-1; for (int x=1;x<=t;x++) for (int y=x;y<=t;y++) for (int d=i;d<=j;d++) f[i][j][min(x,a[j])][max(y,a[j])]=min(f[i][j][min(x,a[j])][max(y,a[j])],f[i][d-1][x][y]+f[d][j-1][0][0]); for (int x=1;x<=t;x++) for (int y=x;y<=t;y++) f[i][j][0][0]=min(f[i][j][0][0],f[i][j][x][y]+w+v*(b[y]-b[x])*(b[y]-b[x])); } cout<<f[1][n][0][0]; return 0; }