石子合并-区间dp
重点
1. 状态表示:f[i][j] 表示 将区间i-j合并的所有合法方案的集合,属性操作代价最小值
2. 集合划分:根据最后是由哪两部分合并的,来进行划分
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 310;
int f[N][N];
int s[N];
int a[N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
for (int len = 1; len <= n; len++) {
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
if (i == j) {
f[i][j] = 0;
continue;
}
f[i][j] = f[i][i] + f[i + 1][j] + s[j] - s[i - 1];
for (int k = i + 1; k <= j; k++) {
f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + s[j] - s[i - 1]);
}
}
}
cout << f[1][n] << endl;
return 0;
}