i, j 为第i个切割点和第j个切割点,
dp[i][j] =min(dp[i][j], dp[i][k] + dp[k][j]) + a[j] - a[i];
这样只要开 52 * 52数组就可以了
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#define N 55
//看看LLC的优化方法
using namespace std;
int dp[N][N];
const int inf =( -1u >> 1) ;
int a[52];
void init(int n)
{
for (int i = 0; i <= n + 1 ; i++)
for (int j = i + 2; j <= n + 1; j++)
dp[i][j] = inf;
}
int main()
{
int l, n;
while (scanf("%d", &l) != EOF && l)
{
scanf("%d", &n);
a[0] = 0;
for (int i = 1; i <= n; i++)
scanf("%d", a+i);
a[n + 1] = l;
for (int i = 0; i <= n; i++)
dp[i][i + 1] = 0;
init(n);
for (int k = 1; k <= n; k++)
for (int i = 0; i < n + 1 - k; i++)
{
for (int j = i + 1; j < (i + k + 1); j++)
dp[i][i + k + 1] = min(dp[i][i + k + 1], dp[i][j] + dp[j][i + k + 1]);
dp[i][i + k + 1] += (a[i + k + 1] - a[i]);
}
printf("The minimum cutting is %d.\n", dp[0][ n + 1]);
}
return 0;
}