题目大意:给你N个数,要求你分成M个部分,每部分的得分为,假设该部分为i, j, k, 那么的分就为i * j + i * k + j * k
问划分后,至少能得多少分
解题思路:用dp[i][j]表死前j个数,分成了i个部分,所能得到的最小得分,则dp[i][j] = dp[i - 1][k] + w[k + 1][j]
w[k + 1][j]表示k+1个数到第j个数为一部分的得分
该式子满足四边形不等式(…具体现在我也不太懂,先mark)
#include <cstdio>
#include <cstring>
const int N = 1010;
typedef long long LL;
const LL INF = (1LL << 60);
LL dp[N][N], val[N][N], w[N][N], num[N];
int s[N][N];
int n, m;
void init() {
for (int i = 1; i <= n; i++)
scanf("%lld", &num[i]);
for (int i = 1; i <= n; i++)
val[i][i] = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
val[i][j] = val[i][j - 1] + num[i] * num[j];
for (int i = n - 1; i >= 1; i--)
for (int j = i + 1; j <= n; j++)
w[i][j] = w[i + 1][j] + val[i][j];
}
void solve() {
for (int i = 1; i <= n; i++) {
dp[0][i] = w[1][i];
s[0][i] = 0;
}
for (int i = 1; i <= m; i++) {
s[i][n + 1] = n;
for (int j = n; j > i; j--) {
dp[i][j] = INF;
for (int k = s[i-1][j] ; k <= s[i][j + 1]; k++) {
if (dp[i][j] > dp[i - 1][k] + w[k + 1][j]) {
dp[i][j] = dp[i - 1][k] + w[k + 1][j];
s[i][j] = k;
}
}
}
}
if (m >= n) dp[m][n] = 0;
printf("%lld\n", dp[m][n]);
}
int main() {
while (scanf("%d%d", &n, &m) != EOF && n + m) {
init();
solve();
}
return 0;
}