题意:
n个数,每次去掉一个数的代价,是该数前后与其之和
询问最后剩余两数的最小代价之和
解题思路:
一开始就想到是区间dp了,但是脑残没有枚举中间状态
另外dp[i][j]应该是保留i,j端点的[i,j]区间的最小值,而不应该仅仅代表是[i,j]区间的最小值
有了定义dp方程就很显然了
dp[i][j]=min(dp[i][k]+dp[k][j]+(a[i]+a[k]+a[j])*(a[i]+a[k]+a[j]))
AC代码:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e2 + 10;
int n;
int a[maxn];
int dp[maxn][maxn];
int cal(int x) {return x * x;}
int main() {
while (cin >> n) {
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int len = 3; len <= n; len++)
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
dp[l][r] = inf;
for (int k = l + 1; k < r; k++) {
dp[l][r] = min(dp[l][r], dp[l][k] + dp[k][r] + cal(a[l] + a[k] + a[r]));
}
}
cout << dp[1][n] << endl;
}
}