区间dp,切割dp[i][j]的花费和切法无关(无后效性)
dp[i][j]表示区间i,j的花费,于是只要枚举切割方法就行了,区间就划分成更小的区间了。O(n^3)
看了看四边形不等式,证明太长了。
#include<bits/stdc++.h> //变量不要取成ignore left之类 using namespace std; const int maxn = 51; int cut[maxn]; int dp[maxn][maxn]; const int INF = 0x3fffffff; int main() { //freopen("in.txt","r",stdin); int L,n; while(scanf("%d",&L),L){ scanf("%d",&n); cut[0] = 0; cut[n+1] = L; for(int i = 1; i <= n; i++) scanf("%d",cut+i); for(int i = 0; i <= n; i++) dp[i][i+1] = 0; for(int d = 1; d <= n; d++){ for(int i = 0,mi = n-d; i <= mi; i++){ int j = i+d+1; dp[i][j] = INF; for(int k = i+1; k < j; k++){ dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]); } dp[i][j] += cut[j]-cut[i]; } } printf("The minimum cutting is %d.\n",dp[0][n+1]); } return 0; }
时间: 2024-10-17 00:38:32